VBA copy column only when value is 1 in row 1

NickvdB

Board Regular
Joined
Apr 30, 2014
Messages
71
Hello,

Excel 2010:
I would like to use a VBA with the following code
Workbooks("AAA.xlsb").Worksheets("BBB").Range("VARIABLE").Copy Destination:=Workbooks("CCC.xlsb").Worksheets("BBB").Range("a1")
the red part should be based on the values of row 1. So as an example following cell values: A1=1 / B1=1 / D1=1 / G1=1
Then I would like to have rows A + B + D + G in the red part.
Hope this makes sense and somebody can help :)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hmm, abit unclear to me. Would you end result be like this?

Workbooks("AAA.xlsb").Worksheets("BBB").Range("ABCD").Copy

?
 
Upvote 0
I have misused "rows" in the last sentence :(
In the example I would like to have the complete columns A, B, D and G copied, perhaps displayed as followed:
Workbooks("AAA.xlsb").Worksheets("BBB").Range("A:A"&"B:B"&"D:D"&"G:G").Copy
 
Upvote 0
soo. Range("A:D","G:G") basicly, and you want whatever Column who has the value 1 in the first row to be copied.. Let me draw something up :)
 
Upvote 0
This is how I would do it, I look through row 1, for a value = 1, if that exists I move that column..

Code:
Sub sortTest()


LC = Workbooks("AAA.xlsb").Worksheets("BBB").Cells(1, Columns.Count).End(xlToLeft).Column


For i = 1 To LC
    If Cells(1, i).Value = 1 Then
        NC = Workbooks("AAA.xlsb").Worksheets("BBB").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
        Workbooks("CCC.xlsb").Worksheets("BBB").Columns(NC).Value = Workbooks("AAA.xlsb").Worksheets("BBB").Columns(i).Value
    End If
Next i
End Sub

It will not bring formats with it however, you could amend it like this to get that aswell.

Code:
Sub sortTest()


LC = Workbooks("AAA.xlsb").Worksheets("BBB").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LC
    If Cells(1, i).Value = 1 Then
        NC = Workbooks("AAA.xlsb").Worksheets("BBB").Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
        Workbooks("AAA.xlsb").Worksheets("BBB").Columns(i).Copy Workbooks("CCC.xlsb").Worksheets("BBB").Columns(NC)
    End If
Next i
End Sub

Test them out, and let me know what you think. Both should be quick.
 
Last edited:
Upvote 0
Thanks for the very quick solution, however excel is not happy with it :) VBA (your first solution) ran for minutes and I killed it.
Would it be more quicker to have the columns with value other than 1 removed?
For information the columns have somewhat like 1500 links to other files (already opened with VBA)
 
Upvote 0
Did you try debugging it? (running through it step by step).

so, basicly copy the sheet, then remove all columns without 1 in them? That could work. ;)
 
Upvote 0
And, should not take that much time, how many columns will you have to move? You example listed 4. That should not take much time.

And I would suggest using my second solution if you want it to move links to other workbooks. :)
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,803
Members
449,048
Latest member
greyangel23

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