|
大神们好
我写了一个代码,主要是把一个文件中的2个sheet里的内容分别抓取到我目前的表里的2个sheet里,代码如下:
但运行的时候总是出现automation error问题,请大神们指点,谢了
Private Sub CommandButton1_Click()
Dim sourceselect As Variant
Dim targetsheet As Worksheet
Dim sourcesheet As Worksheet
Dim sourceworkbook As Workbook
Set targetsheet = Sheets("VWAG Handover (MM EXT) ")
Dim targetsheetFX As Worksheet
Dim sourcesheetFX As Worksheet
Set targetsheetFX = Sheets("FX")
'select file
sourceselect = Application.GetOpenFilename("allfiles(*.*),*.*", , , , False)
Set sourceworkbook = Application.Workbooks.Open(sourceselect)
Set sourcesheet = sourceworkbook.Worksheets(1)
Set sourcesheetFX = sourceworkbook.Worksheets(2)
'clear history
targetsheet.Range("a3:x100").Clear
targetsheetFX.Range("a3:x100").Clear
'read data
targetlines = 3
For i = 1 To 600
If sourcesheet.Range("a" & i).Value = targetsheet.Range("a1").Value Then
targetsheet.Range("a" & targetlines) = sourcesheet.Range("a" & i).Value
targetlines = targetlines + 1
End If
Next i
targetlinesFX = 3
For j = 1 To 600
If sourcesheetFX.Range("a" & i).Value = targetsheet.Range("a1").Value Then
targetsheetFX.Range("a" & targetlinesFX) = sourcesheetFX.Range("a" & i).Value
targetlinesFX = targetlinesFX + 1
End If
Next j
sourceworkbook.Close
End Sub
|
|