我尝试运行代码,但是在尝试将变量存储在sht1和sht2中以基于索引获取工作表时遇到运行时错误
ive试图将数组“ lasSheets(i)和dataPull(i)”设置为sht1和sht2,以便它只能选择索引i处的数组值,但出现“编译器错误:期望的Sub,Function或Property”所以我没有了下一步的想法
Private Sub CommandButton1_Click()
Dim source As Workbook
Dim sht1 As Worksheet
Dim destination As Workbook
Dim sht2 As Worksheet
Dim tmp As String
Dim startCell As Range
Dim lastRow As Long
Dim lastColumn As Long
Dim i As Integer
Dim mapDest As String
Dim mapSrc As String
setFile = Application.GetOpenFilename 'used to open the browser window
tmp = setFile 'store the selected file in variable tmp
Application.ScreenUpdating = False 'preventing long runtimes
If Dir(tmp) <> "" Then
Set destination = ThisWorkbook 'workbook b1 is declared as the current worksheet opened
Set source = Workbooks.Open(tmp) 'the file the user selected is opened in excel
Dim lasSheets(1 To 9) As String 'array to list the sheet names
lasSheets(1) = "L1 OVERVIEW"
lasSheets(2) = "LAS EFFL RELEASE PARAMS"
lasSheets(3) = "L1 EAL PARAMS rev4"
lasSheets(4) = "L1 EAL PARAMS"
lasSheets(5) = "L1 RAD STATUS"
lasSheets(6) = "L1 PLANT STATUS"
lasSheets(7) = "L1 CDAM"
lasSheets(8) = "L1 ERDS"
lasSheets(9) = "LAS STATE UPDATES"
Dim dataPull(1 To 9) As String
dataPull(1) = "Overview Paste"
dataPull(2) = "Eff Release Para Paste"
dataPull(3) = "EAL Rev4 Paste"
dataPull(4) = "EAL Para Paste"
dataPull(5) = "Radiological Stat Paste"
dataPull(6) = "Plant Status Paste"
dataPull(7) = "CDAM Paste"
dataPull(8) = "ERDS Paste" ' blank sheet
dataPull(9) = "State Updates Paste" 'blank sheet
For i = 1 To 9
mapSrc = lasSheets(i)
mapDest = dataPull(i)
i = i + 1
Set sht1 = source.Sheet(mapSrc) '<-- errors here set sht1 and sht2 to the source and destination worksheets
Set sht2 = destination.Sheet(mapDest)
Set startCell = sht1.Range("B2")
'find last row and last column
lastRow = sht1.Cells(sht1.Rows.Count, startCell.Column).End(xlUp).Row
lastColumn = sht1.Cells(startCell.Row, sht1.Columns.Count).End(xlToLeft).Column
sht2.Range(startCell, sht1.Cells(lastRow, lastColumn).Address).Copy destination:=sht1.Range("D5")
Application.CutCopyMode = False
Next i
destination.Close True
Else 'used to prevent a error message from popping up when the user choose to cancel selecting a file
End If
End Sub
我希望for循环在两个数组中循环并根据两个数组中的索引号输出excel工作表