在您的帮助下,我创建了第一个宏,它运行良好,直到我使用来自 Cyberquery 报告的实际源文件和唯一的选项 .XLS 进行测试,所以当我在 Excel 2010 中打开源文件时,它以兼容模式和宏打开给我“应用程序定义或对象定义的错误”。我应该怎么做才能让它工作?
.Range("A2", .Cells(.Cells(Rows.Count, "A").End(xlUp).Row, .Cells(1, Columns.Count).End(xlToLeft).Column)).SpecialCells(xlCellTypeVisible).Copy
我的宏从以兼容模式打开的源工作簿复制数据并将其粘贴到具有宏的主目标工作簿,并在粘贴数据后关闭源工作簿。这是宏
Function GetSalesOrderWb() As Excel.Workbook
Dim wb As Excel.Workbook
For Each wb In Application.Workbooks
If Left(wb.Name, 10) = "SalesOrder" Then
Set GetSalesOrderWb = wb
Exit Function
End If
Next
End Function
Private Sub CommandButton1_Click()
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim wsTool As Worksheet
Dim wBook As Workbook
Set wBook = GetSalesOrderWb
If wBook Is Nothing Then
MsgBox "Please open SaleOrderRMTOOL file"
Exit Sub
End If
Set wsSource = GetSalesOrderWb.Sheets("SalesOrderRMTOOL")
Set wsTarget = Workbooks("RMORDERTOOL.xlsm").Sheets("Sales Order")
Application.ScreenUpdating = False
Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("i7:i1003").Value = ""
Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("l7:l1003").Value = ""
Workbooks("RMORDERTOOL.xlsm").Sheets("Tool").Range("o7:o1003").Value = ""
wsTarget.Cells.Clear
' Copy header row to Target sheet if target is empty
If IsEmpty(wsTarget.Range("A1")) Then wsSource.Rows(1).Copy Destination:=wsTarget.Range("A1")
' Define visible filterd cells on source worksheet and copy
With wsSource
.Range("A2", .Cells(.Cells(Rows.Count, "A").End(xlUp).Row, .Cells(1, Columns.Count).End(xlToLeft).Column)).SpecialCells(xlCellTypeVisible).Copy**
End With
' Paste to target sheet
wsTarget.Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial xlPasteValues, xlPasteSpecialOperationNone, False, False
Application.CutCopyMode = True
Application.ScreenUpdating = True
GetSalesOrderWb.Close 0
End Sub