Columns not copying over

Tdeg88

New Member
Joined
Oct 13, 2022
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
All,

The below code is suppose to copy columns from 3 sheets into a final sheet (sheet "All"). The first two columns are the same so I've just copy pasted them into the final sheet. The error flags up when it tries to run the following "ws.Range("C" & LC).Copy sh.Cells(1, CC)". I'm guessing the range I've selected if cant be selected and that's why the code brings up an error.

Thank you for the help in advance.


VBA Code:
Sub CompileAll()
Dim LC As Long, CC As Long
Dim ws As Worksheet, sh As Worksheet

'LC - Last column
'CC - Last compile column

Sheets("All").Delete
Sheets.Add(After:=Sheets("sheet3")).Name = "All"

Sheets("sheet1").Range("A:B").Copy
' Paste Special
Sheets("All").Range("A:B").PasteSpecial xlValues
Sheets("All").Range("A:B").PasteSpecial xlFormats

Set sh = Worksheets("All")

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "All" Then
    
        CC = sh.Cells(3, sh.Columns.Count).End(xlToLeft).Column + 1
        LC = ws.Range("C" & ws.Columns.Count).End(xlToRight).Column
        
        
        If CC = 2 Then CC = 1
        ws.Range("C" & LC).Copy sh.Cells(1, CC)
    End If
Next ws
    
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This two lines look very weird to me:
VBA Code:
LC = ws.Range("C" & ws.Columns.Count).End(xlToRight).Column
and
VBA Code:
ws.Range("C" & LC).Copy sh.Cells(1, CC)

So LC is returning a COLUMN number in that first line.
However, you are using it in the ROW place of the second line:
Rich (BB code):
Range("C" & LC)
You are already specificying column "C", then using the LC variable as a row number.

Is that really what you are intending to do?

Also, what is the exact error message you get?
 
Upvote 0
Hi Joe4,

Thank you for replying.

Is that really what you are intending to do?

What i intended was I thought i was selecting the range in the first 3 worksheets from column C to the column value given by LC from the worksheet and copying over that section to the last worksheet (worksheet - All) and pasting it after the last column.

Also, what is the exact error message you get?
The error message is a run-time error 1004 - Select method of range class failed.
 
Upvote 0
I think it would be beneficial for us to see some sample data and your expected output, so we can see exactly what it is you are trying to do.
It is a little confusing/unclear from your code and explanation. I think a visual example should help clear things up.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hi Joe,

So basically i have an input sheet that will input a new columns to sheets 1 to 3 i.e. new tasks and new people (that part of the code is working fine). below is a example of what sheet 1 to sheet 3 contain. The code that i posted above was suppose to copy columns C to the last column (the last column isn't constant) and to paste the data from sheet 1 to 3 in the All sheet (second table).

Book1
ABCDEFG
1Sheet 1
2
3LeadBenDavidJohnBenGeorge
4TaskTask 1Task 2Task 3Task 4Task 5
5Date
601/03/2023Week1Research
708/03/2023Week2Field work
815/03/2023Week3
922/03/2023Week4
1029/03/2023Week5work 2Research
1105/04/2023Week6
1212/04/2023Week7
1319/04/2023Week8
1426/04/2023Week9Work 1
1503/05/2023Week10
1610/05/2023Week11
1717/05/2023Week12work 3
1824/05/2023Week13
1931/05/2023Week14
2007/06/2023Week15
2114/06/2023Week16
2221/06/2023Week17
2328/06/2023Week18
2405/07/2023Week19
2512/07/2023Week20
2619/07/2023Week21
2726/07/2023Week22
Sheet1


Book1
ABCDEFGHIJKLMNOPQRS
1Sheet 1Sheet 2Sheet 3
2
3LeadBenDavidJohnBenGeorgeBenDavidJohnBenGeorgeBenDavidJohnBenGeorge
4TaskTask 1Task 2Task 3Task 4Task 5Task 1Task 2Task 3Task 4Task 5Task 1Task 2Task 3Task 4Task 5
5Date
601/03/2023Week1ResearchResearchResearch
708/03/2023Week2Field workField workField work
815/03/2023Week3
922/03/2023Week4
1029/03/2023Week5work 2Researchwork 2Researchwork 2Research
1105/04/2023Week6
1212/04/2023Week7
1319/04/2023Week8
1426/04/2023Week9Work 1Work 1Work 1
1503/05/2023Week10
1610/05/2023Week11
1717/05/2023Week12work 3work 3work 3
1824/05/2023Week13
1931/05/2023Week14
2007/06/2023Week15
2114/06/2023Week16
2221/06/2023Week17
2328/06/2023Week18
2405/07/2023Week19
2512/07/2023Week20
2619/07/2023Week21
2726/07/2023Week22
All
 
Upvote 0
OK, you had a few logic errors in there. Probably the biggest thing was your column calculations. Note that ".Column" will always return the index number of the column, not the column letter (i.e. column "E" would return 5). So you cannot combine the result of that with a column letter to get a column range.

When working with columns like this, instead of using "Range", I like to use "Cells".
The format of Cells is: Cells(row, column)
The advantage to this is in your column reference, you can use the column letter (enclosed in double-quotes), or the column index number.

So the following three range references are equivalent and all refer to cell E8:
Range("E8")
Cells(8, "E")
Cells(8, 5)


You also have a problem with your first copy, in that you are trying to use row 3 to find the last column with data, but the issue is that after you have copied over just columns A and B, there is nothing in cell B3, so it thinks column A is the last column with data instead of column B.

I think I have fixed up your code. Give this a try:
VBA Code:
Sub CompileAll()

Dim LC As Long, CC As Long, LR As Long
Dim ws As Worksheet, sh As Worksheet

'LC - Last column
'CC - Last compile column
'LR - Last row of data to copy

Sheets("All").Delete
Sheets.Add(After:=Sheets("sheet3")).Name = "All"

Sheets("sheet1").Range("A:B").Copy
' Paste Special
Sheets("All").Range("A:B").PasteSpecial xlValues
Sheets("All").Range("A:B").PasteSpecial xlFormats

Set sh = Worksheets("All")

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name <> "All" Then
   
        CC = sh.Cells(3, sh.Columns.Count).End(xlToLeft).Column + 1
        LC = ws.Cells(3, ws.Columns.Count).End(xlToLeft).Column
        LR = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
              
        If CC <= 2 Then CC = 3
        ws.Range(ws.Cells(1, 3), ws.Cells(LR, LC)).Copy sh.Cells(1, CC)
    End If
Next ws
   
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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