I am getting this error while running this vba code. its kind of syntax error. Please help me with that. here is the script.
What script doing is , its copying data from one column to another and selecting the copied column.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\pc\Report\report_20120912.xls")
Set ws = objWorkbook.Worksheets("Data")
Set SelRange = objExcel.Selection
ws.Range("F2:F1000").AdvancedFilter 2,,ws.Range("M2"),True
ws.Range("N1") = ws.Range("A1")
ws.Range("O1") = ws.Range("B1")
//Below line has the bug
ws.Range("M2").Select
selection=objExcel.Selection
col1=selection.End(xlDown) //showing error on this line
ws.Range(selection,col1).Select
n = ws.Range.Count
ws.Range("S2").Select
I see that you have tagged the question as Excel/Excel-VBA
If you are doing this in VBA Excel then you do not need to use
CreateObjectto create another instance of Excel. You can work with the current instance as wellSee this code
As mentioned in my comment, throw the
Selection/Selectout of your code. It not only slows down your code but can also give you runtime errors. See this link. I have commented the code below so that you will not have any problem understanding it. 🙂Your above code can also be written as (I have not tested the code)
If you notice that you actually do not need to use
.Selector work with theSelection