Vlookup、列の削除などの操作を行うマクロプログラムがあります。更新するファイルにはいくつかのワークシートが含まれており、毎回ワークシートの名前と順序が異なる場合があります。したがって、マクロを使用するたびに必要なワークシートを選択できるようにしたいと考えています。しかし、私は成功していません....
これがマクロです。mySheet を可変にしたかったのです。理想的には、その wbSource 内で必要なワークシートを選択するように促すことができます..しかし、エラーが発生しています。誰も私がそれを行う方法を知っていますか?
前もって感謝します!
Sub Macro1()
Dim file1 As String
Dim file2 As String
Dim wbSource As Workbook
Dim wbLookup As Workbook
Dim startRange As Range
Dim mySheet As Worksheet
Dim col As Range
Dim Del As Range
file1 = Application.GetOpenFilename(Title:="Select the file to update")
If Len(Dir(file1)) = 0 Then Exit Sub
file2 = Application.GetOpenFilename(Title:="Select the LOOKUP file")
If Len(Dir(file2)) = 0 Then Exit Sub
Set wbLookup = Workbooks.Open(file2)
Set wbSource = Workbooks.Open(file1)
Set mySheet = wbSource.Sheets(ActiveSheet.Name)
On Error Resume Next
Application.DisplayAlerts = False
Set col = Application.InputBox _
(Prompt:="Select Column.", _
Title:="Where do you want to insert the columns?", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
col.Resize(, 5).EntireColumn.Insert
On Error Resume Next
Application.DisplayAlerts = False
Set Del = Application.InputBox _
(Prompt:="Select Column.", _
Title:="Which column to delimit?", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
Del.EntireColumn.Select '** ERROR HERE!!
Selection.TextToColumns _
Destination:=Del, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="-"
Del.Offset(0, 2).Delete
Del.Offset(0, 1).Delete
On Error Resume Next
Set startRange = Application.InputBox("Select the first cell for the formula", "Autofill VLOOKUP", Type:=8)
On Error GoTo 0
If Not startRange Is Nothing Then
Application.Goto startRange
startRange.FormulaR1C1 = "=VLOOKUP('[" & wbSource.Name & "]" & mySheet.Name & "'!RC[-1],'[" & wbLookup.Name & "]NON SLL'!C1:C3,3,FALSE)"
End If
End Sub