VBA excel兼容模式

时间:2013-11-14 18:21:48

标签: vba excel-vba excel

我用你的一些帮助创建了第一个宏,它工作正常,直到我测试了来自Cyber​​query报告的实际源文件和唯一的选项.XLS所以当我在Excel 2010中打开源文件时,它在兼容性中打开模式和宏给我'应用 - 定义或对象定义的错误“。我该怎么做才能使它工作?

{.Range("A2", .Cells(.Cells(Rows.Count, "A").End(xlUp).Row, .Cells(1, Columns.Count).End(xlToLeft).Column)).SpecialCells(xlCellTypeVisible).Copy} 

My Macro从兼容模式下打开的源工作簿中复制数据,并将其粘贴到具有Macro的主Target工作簿,并在粘贴数据后关闭Source工作簿。这是宏

    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}

0 个答案:

没有答案