I’ve the following code that works fine. It will update all the “brand” files that is located under the “brand” range in macro1.xls. The problem is recently I’ve added another name under the brand range in macro1.xls and I changed the following line in the vba code : from Do Until z = 20 to Do Until z = 21 and now the macro gives me the following error:
Run Time Error 1004 Method ‘Range’ of object ‘_Global’ failed
It will update all the “Brand” files except the new file that I created. What should I do?
CODE
Dim brand, city as string
Dim z as integer
z = 4
Workbooks.Open (C:\temp\workbook2.xls), UpdateLinks:="0"
Do Until z = 20
a1 = 0
a2 = 0
brand = Workbooks("Macro1.xls").Sheets("Sheet1"). _
Range("Brand").Offset(z, 0)
city = Workbooks("Macro1.xls ").Sheets("Sheet1"). _
Range("City").Offset(z, 0)
Windows("workbook2.xls").Activate
ActiveWorkbook.Worksheets("Sheet2").Select
Range("b3").Select
Cells.Find(what:=Brand, after:=ActiveCell).Activate
a1 = ActiveCell.Offset(5, 1).Value
a2 = ActiveCell.Offset(6, 2).Value
Range(brand).Copy
Windows(brand.xls).Activate
ActiveWorkbook.Worksheets("Sheet3").Select
Range("e5").Value = a1
Range("e6").Value = a2
z = z + 1
Loop
Msgbox.Done
Dim z as integer
z = 4
Workbooks.Open (C:\temp\workbook2.xls), UpdateLinks:="0"
Do Until z = 20
a1 = 0
a2 = 0
brand = Workbooks("Macro1.xls").Sheets("Sheet1"). _
Range("Brand").Offset(z, 0)
city = Workbooks("Macro1.xls ").Sheets("Sheet1"). _
Range("City").Offset(z, 0)
Windows("workbook2.xls").Activate
ActiveWorkbook.Worksheets("Sheet2").Select
Range("b3").Select
Cells.Find(what:=Brand, after:=ActiveCell).Activate
a1 = ActiveCell.Offset(5, 1).Value
a2 = ActiveCell.Offset(6, 2).Value
Range(brand).Copy
Windows(brand.xls).Activate
ActiveWorkbook.Worksheets("Sheet3").Select
Range("e5").Value = a1
Range("e6").Value = a2
z = z + 1
Loop
Msgbox.Done