Copying non-adjacent columns to other sheet

Maraua

New Member
Joined
Mar 28, 2019
Messages
4
Hi,
I have the following code that copies filtered data from one sheet to another. I use the copied data to populate a listbox on a userform:
Code:
    With ws        .AutoFilterMode = False
        
        Set rng = ws.Range("B8:R8" & .Cells(Rows.Count, "B").End(xlUp).row)
        rng.Copy ws1.Range("B7")
        rngdest = "Sheet2!" & .Range("B8").CurrentRegion.Address
    End With
    With ListBox1
        .RowSource = rngdest

        .ColumnCount = 17
        .ColumnWidths = "0;60;90;0;24;40;20;130;24;42;42;55;20;120;130;110;30"
    End With
    Set rng = Nothing
I have been trying to adjust this to copy multiple, non-adjacent columns.
When I use
Code:
 Set rng = ws.Range("B8:R" & ", T8:Z" & .Cells(Rows.Count, "B").End(xlUp).row)I get a runtime error 1004.

Any thoughts on how to accomplish this?
Thanks.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
Hi & welcome to MrExcel
Try
Code:
Set Rng = Ws.Range("B8:R" & .Cells(Rows.Count, "B").End(xlUp).Row & ", T8:Z" & .Cells(Rows.Count, "B").End(xlUp).Row)
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,790
Office Version
2007
Platform
Windows
Try this

Code:
dim lr as long
lr = ws.Cells(Rows.Count, "B").End(xlUp).Row
Set Rng = ws.Range("B8:R" & lr & ",T8:Z" & lr)
 

Maraua

New Member
Joined
Mar 28, 2019
Messages
4
Thanks guys,
Both work great.
I prefer Dante's. I will have 15-20 ranges to copy.
So I will end up with less code, and when I tried both the codes.

I had the impression that Dante's was a bit faster. Or is this just my imagination?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
I wouldn't expect any noticeable difference in time, although Dante's code only calculates the last row once, rather than twice
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,151
Office Version
365
Platform
Windows
Not particularly, although I would tend to go with Dante's code as it's more compact & if you need to copy other ranges you can just keep using "lr", rather than recaculating it.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,790
Office Version
2007
Platform
Windows
Sorry, I was eating.


Nor do I see differences, neither in time nor in advantages, pro's: it could be that the code is more compact and less risk of making mistakes when using it several times
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,790
Office Version
2007
Platform
Windows
I'm glad to help you. Thanks for the feedback.
 

Forum statistics

Threads
1,081,860
Messages
5,361,737
Members
400,653
Latest member
ProParadox

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top