Excel VBAフォームから利用可能なワークシートを選択して1つのPDFドキュメントにエクスポートするルーチンを作成しようとしています。私はWordとPowerPointルーチンへのエクスポートでこれを使用する予定です。スタックオーバーフローのアイデアのいくつかを試してみたところ、これはうまくいったと言われています。私は運がなかった。私はまた、他の情報源から様々なアイデアを試しました。おそらく、私は明らかに目が見えません。VBAを使用して複数のワークシートを有効にする
私は配列を使ってみました(arrSheets、まだコード内にありますが、私はまだそれを使うことができると期待しています)。それはシートオブジェクトで作成されますが、動的配列とredimコマンドを使用して私のために働いていない。
「ActiveSheet.ExportAsFixedFormat ...」の行までは正常に動作するようです。その時点で私は
Private Sub cmdExport_Click()
'Find the selected documents from the form's checkboxes and send to the export routine
Dim intArrayCounter, intSelectionNum As Integer
Dim bolFound As Boolean
Dim ctrl As control
Dim arrSheets(1 To 6) As Variant ' the array to hold the worksheet objects...
intSelectionNum = 0 ' which checkbox is it
intArrayCounter = 1 ' array index
bolFound = False ' was a checked box found?
For Each ctrl In frmToPDF.Controls
If TypeName(ctrl) = "CheckBox" Then
intSelectionNum = intSelectionNum + 1 ' set the selection number
If ctrl.Value = True Then
bolFound = True ' found a selection set the flag to true
Set arrSheets(intArrayCounter) = Sheets(intSelectionNum)
ThisWorkbook.Sheets(intSelectionNum).Select
' increment the counter
intArrayCounter = intArrayCounter + 1
End If
End If
Next
'Sheets(arrSheets).Select <---remmed out cause this throws an error
If bolFound = False Then ' if there is Nothing selected send a message, or do the deal...
Call MsgBox("There is nothing selected to export!", vbOKOnly, "Nothing selected...")
Else
frmExport.Caption = "Processing the document...Please be patient!"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\test.pdf", Quality:=xlQualityStandard, IgnorePrintAreas:=False, DisplayFileAfterPublish:=True
'the above Activesheet routine throws "Application-defined or Object-defined error (Run-time error 1004)"
End If
ThisWorkbook.Sheets(intSheet).Select
End Sub
ASHのソリューションが動作するはずです:しかし、私は非常によく、非常に少ないコードで動作する他の場所StackOverflowの上でこの答えを見つけました。 'ReDim'ステートメントに関しては、新しいディメンションにサイズ変更する前に配列の既存のデータを保持するために' ReDim Preserve'を使用しましたか? – EEM
これを忘れてしまった:[ReDim声明](https://msdn.microsoft.com/en-us/library/office/gg251578(v = office.15).aspx) – EEM