3
votes

I create a first ever Macro with some of your help and it worked fine until I tested with actual source file that comes from Cyberquery report and the only option there .XLS so when I open Source file in Excel 2010 it open in Compatibility Mode and macro give me 'Aplication - defined or Object-defined error". What should I do to make it work?

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

My Macro Copies data from Source workbook that opens in Compatibility mode and pastes it to the main Target workbook that has Macro and it closes Source workbook after Data is pasted. Here is the Macro

    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
BIRDS VIEW: You are missing DOT before ROWS.COUNT and COLUMNS.COUNT.Siddharth Rout
Wow it worked! Thank you very much for your help!user2980669
This is the reason why I always advise using DOT before ROWS.COUNT and COLUMNS.COUNT. This is the classic scenario where the code will fail because the ROWS.COUNT return 65536 for xl2003 and earlier and 1048576 for xl2007+. Similarly COLUMNS.COUNT return 256 and 16384 respectivelySiddharth Rout