Column Combine VBA

masonbp2

New Member
Joined
Jul 27, 2017
Messages
15
Hello,

I am trying to combine several columns into one column, pasted on another sheet. I am getting an error of "Error 1004 Method 'Range' of object '_Worksheet' failed" when running my code. I am trying to use range.cells() so that I can reference columns in a for loop. At the top of each column from the copy sheet, I have a cell that has the numerical value for the last non blank cell in the column.

Sub Second_Round()


Dim targetworkbook As Workbook
Set targetworkbook = Application.ActiveWorkbook
Dim targetSheet As Worksheet
Set targetSheet = targetworkbook.Worksheets("Second Round Sample")
Dim copysheet As Worksheet
Set copysheet = targetworkbook.Worksheets("Second Round")
Dim pasterange As Range
Dim lastrowa As Integer




Application.Calculation = xlCalculationManual


Set pasterange = targetSheet.Range("A2")


For c = 2 To 50




lastrowa = copysheet.Cells(1, c).Value
copysheet.Range(Cells(3, c), Cells(lastrowa, c)).Copy
pasterange.PasteSpecial xlPasteValuesAndNumberFormats

pasterange = targetSheet.Range("A" & pasterange + lastrowa)


Next c

Application.Calculation = xlCalculationAutomatic


End Sub


I would appreciate any help.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
You need to qualify the Cells, as well as the range
Code:
copysheet.Range(copysheet.Cells(3, c), copysheet.Cells(lastrowa, c)).Copy
 
Upvote 0
If the active sheet is copysheet ("Second Round"), you should not have problems. But if you are on another sheet, then you must put the reference, as Fluff says.


This line also changes:
Code:
pasterange = targetSheet.Range("A" & pasterange + lastrowa)



For this:
Code:
Set pasterange = targetSheet.Range("A" & pasterange.Rows.Count + lastrowa)
 
Upvote 0
Thank you!! I have slightly changed the code now, as I was getting a mismatch error on pasterange = targetSheet.Range("A" & pasterange + lastrowa) as one was a integer and one was a range. This is my new code. I am getting an error of object variable or with block variable not set:

Sub Second_Round()


Dim targetworkbook As Workbook
Set targetworkbook = Application.ActiveWorkbook
Dim targetSheet As Worksheet
Set targetSheet = targetworkbook.Worksheets("Second Round Sample")
Dim copysheet As Worksheet
Set copysheet = targetworkbook.Worksheets("Second Round")
Dim pasterange As Range
Dim lastrowa As Integer
Dim pastenumber As Integer




Application.Calculation = xlCalculationManual


pastenumber = 2


For c = 2 To 50




lastrowa = copysheet.Cells(1, c).Value
copysheet.Range(copysheet.Cells(3, c), copysheet.Cells(lastrowa, c)).Copy
pasterange = targetSheet.Range("A" & pastenumber)
pasterange.PasteSpecial xlPasteValuesAndNumberFormats

pasterange = targetSheet.Range("A" & pastenumber + lastrowa)


Next c
Application.Calculation = xlCalculationAutomatic


End Sub
 
Upvote 0
You can read my answer in post #4 about the pasterange object
 
Upvote 0
Try
Code:
For c = 2 To 50
   lastrowa = copysheet.Cells(1, c).Value
   copysheet.Range(copysheet.Cells(3, c), copysheet.Cells(lastrowa, c)).Copy
   Set pasterange = targetSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
   pasterange.PasteSpecial xlPasteValuesAndNumberFormats
Next c
 
Upvote 0
Thank you guys so much!! I was able to complete my file after a very stressful day because of you.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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