VBA Copy cells from non-contiguous ranges to paste on another sheet

RuiFlora

Board Regular
Joined
Feb 28, 2014
Messages
58
Hello, I am trying to build a code that copy cells from non-contiguous ranges and paste the data on another sheet where the cells are contiguous. The ranges have more than one column but I just want to copy the data from one column (for example the first column). This is the code I have for now, but it gives me an error:

Sub CarCopy_Click()


Dim BrandCopy As Range
Set BrandCopy = Worksheets("Cars").Range("BrandCopy")
Dim ModelCopy As Range
Set ModelCopy = Worksheets("Cars").Range("ModelCopy")


Dim myRange As Range
With Sheet13
Set myRange = Union(.Range("BrandCopy"), .Range("ModelCopy"))
End With

With myRange
.Column(1).Select
End With

Selection.Copy
Sheets("Test").Select
Range("D11").Select
ActiveSheet.Paste


End Sub

Or, is there any way to copy the cells from a unique non-contiguous range? (ex: =Cars!$D$5:$AB$9,PC!$D$15:$AB$20) and paste them continuously in another sheet?

Hope someone can help me, i am really stuck with that
Many thanks in advance
 
OK , I understand now.

Change this:
Code:
Resize(, 1).Value
to be
Code:
Offset(0,number_of_columns_to_offset).Resize(, 1).Value
... where you calculate number_of_columns_to_offset depending on your required column ( offset by 0 to get the first column ).

Does that work?
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
OK , I understand now.

Change this:
Code:
Resize(, 1).Value
to be
Code:
Offset(0,number_of_columns_to_offset).Resize(, 1).Value
... where you calculate number_of_columns_to_offset depending on your required column ( offset by 0 to get the first column ).

Does that work?

Like that?
For Each copy In HistCopy.Areas
targetcell.Resize(copy.Rows.Count).Value = copy.Offset(0, colNum).Resize(, 1).Value
Set targetcell = targetcell.Offset(copy.Rows.Count)
Next.

It didn't work. it is still copying the first and it isn't copying to the first empty in "test"
 
Upvote 0
Show your full code. Explain exactly how it's supposed to work. I can see bits, and can see how it might be going wrong, but it's not easy to debug when you can't see it all.
 
Upvote 0
Show your full code. Explain exactly how it's supposed to work. I can see bits, and can see how it might be going wrong, but it's not easy to debug when you can't see it all.

The full code of the button is the following:

Private Sub CommandButton2_Click()


Dim wsTest As Worksheet
Set wsTest = Worksheets("Test")

Dim lColumn As Integer
Dim Model As String

Dim copy As Range
Dim HistCopy As Range
Set HistCopy = Worksheets("BMW").Range("HistCopy")

'Get the value of the model (in BMW sheet) i want to copy
Model = ComboBox2.Value

'Search for the column of the model selected in "BMW" sheet
colNum = WorksheetFunction.Match(Deliver, ActiveWorkbook.Sheets("BMW").Range("D3:S3"), 0)

'get the colum number of the the first empty cell in "Test"
lColumn = wsTest.Range("C14:CC14").Cells.Find(What:="", SearchOrder:=xlColumns, _
SearchDirection:=xlNext, LookIn:=xlValues).Column - 3

'set the destination in "Test" sheet
Set targetcell = Worksheets("Test").Cells(11, lColumn + 3)

'Copy data from all areas in HistCopy range and paste in "Test" sheet
For Each copy In HistCopy.Areas
targetcell.Resize(copy.Rows.Count).Value = copy.Resize(, 1).Value
Set targetcell = targetcell.Offset(copy.Rows.Count)
Next

End Sub

I put in comment, what i am trying to do with each line.
Thanks
 
Upvote 0
What is Deliver in:
Code:
'Search for the column of the model selected in "BMW" sheet
colNum = WorksheetFunction.Match(Deliver, ActiveWorkbook.Sheets("BMW").Range("D3:S3"), 0)
... where does that get set?
 
Upvote 0
What is Deliver in:
Code:
'Search for the column of the model selected in "BMW" sheet
colNum = WorksheetFunction.Match(Deliver, ActiveWorkbook.Sheets("BMW").Range("D3:S3"), 0)
... where does that get set?

Deliver is the Model. It is what i choose in combobox2. (Serie 1, Serie 3, Series 5, etc).
The colNum gets the column number my choice in BMW sheet.
 
Upvote 0
Deliver is the Model. It is what i choose in combobox2. (Serie 1, Serie 3, Series 5, etc).
The colNum gets the column number my choice in BMW sheet.

No it isn't ... from what I'm reading you have set a variable of Model as being the Combobox2 value. Or am I mistaken???
 
Upvote 0
So, did you change it and try again? If you've changed your code, please re-post it.
 
Upvote 0
hello good people.I am new here learning VBA. My challenge is closely related to this one.

I have 2 non contiguous ranges in the same sheet.
I need a way of copying the resultant Union range and pasting it in a different sheet in the same workbook.

Set unionrange = Union(Rnga, Rngb)
 
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,317
Members
449,218
Latest member
Excel Master

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