I am getting the following error "Run-time error '1004' Application-defined or object defined error and highlighting the following line of code newb.sheets(shts(s)).Range("a1").Resize(, cols).Value = wb.sheets(shts(s)).Range(heds(s)).Value
When I skip the above line of code and move to the next one it is showing another error "Run-time error '9' script out of range" and highlighting the following line of code "newb.sheets(shts(s)).Range("a" & rw(s)).Resize(rws, cols).Value = wb.sheets(s).Range(Rng(s) & rws - 1).Value" Please help me what to do.
Can someone please help me to fix above mentioned errors?
Sub Consolidation()
Dim newb as workbook
Dim wb as workbook
Dim Shts
Dim rws as long
Dim rw(2) As Long
mypath = "C:\Consolidation\"
shts = Array("Total Consolidation", "State level Consolidation", "District Level Consolidation")
Set newb = Workbooks.Add
newb.Sheets(1).Name = shts(0)
newb.Sheets(2).Name = shts(1)
newb.Sheets(3).Name = shts(2)
rw(0) = 1
rw(1) = 1
rw(2) = 1
rng = Array("a6:k", "a2:g", "o2:aa")
cols = Array("11", "7", "13")
heds = Array("a1:k1", "a1:g1", "o1:aa1")
fname = Dir(mypath & "*.xls")
Do While Len(fname) > 0
Set Wb = Workbooks.Open(mypath & fname)
For s = 0 To 2
rws = Wb.Sheets(shts(s)).UsedRange.Rows.Count - 1
If s = 0 Then rws = rws - 6
If Not headsdone Then
newb.Sheets(shts(s)).Range("a1").Resize(, cols(s)).value = Wb.Sheets(shts(s)).Range(heds(s)).value
headsdone = True
End If
newb.Sheets(shts(s)).Range("a" & rw(s)).Resize(rws, cols(s)).value = Wb.Sheets(shts(s)).Range(rng(s) & rws).value
rw(s) = rw(s) + rws - 1
Next
fname = Dir
Loop
newb.SaveAs mypath & "COUNTRY LEVEL CONSOLIDATION.xls"
newb.Close
End Sub
rng(s) & rws - 1supposed to be doing? seems odd that you would be making a range address in this fashion.dim rightSide as vaiant, leftSide as vaiant: set rightSide = Wb.Sheets(shts(s)).Range(heds(s)): set leftSide = newb.Sheets(shts(s)).Range("a1").Resize(, cols). What areleftSideandrightSideequal to?