excel vba loop through tables and copy columns

Cothalup

New Member
Joined
Jun 1, 2021
Messages
5
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hello!
I have a problem, I am completely green in programming and I need to write vba code which will copy data from one sheet to another. The problem, however, is that I have to copy the second column with a maximum of 17 tables (the number of tables and rows in the table varies depending on the demand) and then paste them to another sheet, but this time so that they are next to each other. And every time I get the error '1004' Method 'Range' of object '_Global' failed
Legend to code:
WN - A worksheet that specifies how many tables are to be created
WT - Worksheet with tables
WO - sheet to which the data will be copied
c + 14 - a space between the columns to be copied
d + 1 - column spacing after pasting
Below I am pasting my code along with an alternative attempt to do what I needed.

VBA Code:
Dim wsl As Worksheet
Dim wsl1 As Worksheet
Dim i As Byte
i = Range("WN!A8").End(xlDown)
Set wsl = Worksheets("WT")
Set ws11 = Worksheets("WO")
c = 2 ' First column in source worksheet 
d = 2 ' first column in destination worksheet 
For e = 1 To i
        Range(Cells(4, c), Cells(54, c)).Copy Destination:=Sheets("WO").Range(Cells(4, d))
        d = d + 1
        c = c + 14
        'Range(.Cells(4, c), .Cells(54, c)).Copy Destination:=wsl1.Range("A5") 'wsl1.Range(.Cells(4, d + 1), .Cells(54, d + 1)) ' alternate solution
Debug.Print c, d
Next e
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Welcome to the Board!

When you hit "debug", what line of code does it highlight?

What are you trying to do with this line of code?
VBA Code:
i = Range("WN!A8").End(xlDown)
Are you trying to get the last row with data in column A, or are you trying to get the value from the last cell in column A?
What row number is the last one in column A?
What is the value in that last cell in column A?
 
Upvote 0
It highlight this line
VBA Code:
 Range(Cells(4, c), Cells(54, c)).Copy Destination:=Sheets("WO").Range(Cells(4, d))

VBA Code:
i = Range("WN!A8").End(xlDown)
his line represents the number of row from A8. And this is used to determine the number of tables in WT, and in the case of this macro, how many times the loop with column copying is to be performed, since the tables have variable names.
 
Upvote 0
VBA Code:
i = Range("WN!A8").End(xlDown)
That does NOT return a row number. That returns the VALUE from the last cell in column A with data.
If you want the row number, it needs to look like:
VBA Code:
i = Range("WN!A8").End(xlDown).Row

I would also recommend changing your variable declaration from:
VBA Code:
Dim i as Byte
to
VBA Code:
Dim i as Long

Otherwise, if your last row number is after 256, you will get errors.

Try making those two corrections and run your code again and see if your still get errors.
 
Upvote 0
Udało mi się to zrobić, poprzez, standardowa formę kopiuj wklej.
VBA Code:
For e = 1 To i
        Sheets("WT").Select
        Range(Cells(4, c), Cells(54, c)).Select
        Selection.Copy
        Sheets("WO").Select
        Range(Cells(4, d), Cells(54, d)).Select
        ActiveSheet.Paste
        d = d + 1
        c = c + 14
Next e
 
Upvote 0
I was able to do this by using the standard copy paste form.
VBA Code:
For e = 1 To i
        Sheets("WT").Select
        Range(Cells(4, c), Cells(54, c)).Select
        Selection.Copy
        Sheets("WO").Select
        Range(Cells(4, d), Cells(54, d)).Select
        ActiveSheet.Paste
        d = d + 1
        c = c + 14
Next e
 
Upvote 0
Solution

Forum statistics

Threads
1,224,820
Messages
6,181,157
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