Range with Cells Function (Runtime Error - 1004 --> Application-defined or object Defined error)

winz

New Member
Joined
Apr 29, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi am new to VBA, Could you please help on the below code.



I have 2 workbooks and i want to copy the columns from one to another with the matching header.



It shows an error in the following line as (Runtime Error - 1004 --> Application-defined or object Defined error)



Code:
tWB.sheets("Main").Range(Cells(TLRow, TCol)) = aWB.sheets(1).Range(Cells(2, SCol), Cells(SLRow, SCol))





VBA Code:
Sub Pull ()



Dim FileName() as Variant, nw as integer, i as integer

Dim tWB as Workbook, aWB as Workbook

Dim hcell as Range, Header as Range

Dim SCol as Integer, TCol as Integer, SLRow as Integer, TLRow as Integer, SIndex as Integer,

Dim TIndex as Integer



Set tWB = ThisWorkbook

FileName = Application.GetOpenFilename(FileFilter:="Excel Workbooks (*.xls; *.xlsm),*.xls;*.xlsm", MultiSelect:=True)

nw = UBound(FileName)



Set Header = tWB.sheets("List").Range("A1:A" & tWB.Sheets("List").Range("A" & Rows.Count).End(xlUp).Row)



For i = 1 to nw

     Workbooks.Open FileName(i)

     Set aWB = ActiveWorkbook

         For each hcell in Header

             SCol = Application.Match(hcell.value, aWB.Sheets(1).Rows(1),0)

             TCol = Application.Match(hcell.Offset(0,1).value, tWB.Sheets("Main").Rows(1),0)

             SLRow = aWB.Sheets(1).Cells(aWB.Sheets(1).Rows.Count, "A").End(xlUP).Row
             TLRow = tWB.Sheets("Main").Cells(tWB.Sheets(1).Rows.Count, TCol).End(xlUP).Row + 1
             SIndex = Split(aWB.sheets(1).cells(1,SCol).Address, "$")(1)

             TIndex = Split(tWB.sheets("Main").cells(1,TCol).Address, "$")(1)


             tWB.sheets("Main").Range(Cells(TLRow, TCol)) = aWB.sheets(1).Range(Cells(2, SCol), Cells(SLRow, SCol))
 
         Next hcell

Next i

End Sub



Another Method:

Also i have tried to convert the column index number to Letter but am not sure what the error is since the code shows no error but the output is not coming



VBA Code:
tWB.Sheets("Main").Range(TIndex & TLRow) = aWB.sheets(1).Range((SIndex & 2), Range(SIndex & SLRow))
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Upvote 0
HI Rory.. Thank you so much.. It Worked!!! Going Forward I Will post the links here..
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top