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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Nick, this is the revised code that I Tried to post a few minutes ago.

Code:
Option Explicit
Sub copy1()
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
Dim c As Range, rng As Range
With Workbooks("AAA.xlsb").Sheets("BBB")
    For Each c In Intersect(.Range("1:1"), .UsedRange)
        If c.Value = 1 Then
            If Not rng Is Nothing Then
                Set rng = Union(rng, Intersect(c.EntireColumn, .UsedRange))
            Else
                Set rng = Intersect(c.EntireColumn, .UsedRange)
            End If
        End If
    Next
End With
If Not rng Is Nothing Then rng.Copy Workbooks("CCC.xlsb").Sheets("BBB").Range("A1")
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub
 
Last edited:
Upvote 0
Nick, this is the revised code that I Tried to post a few minutes ago.

Code:
Option Explicit
Sub copy1()
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
Dim c As Range, rng As Range
With Workbooks("AAA.xlsb").Sheets("BBB")
    For Each c In Intersect(.Range("1:1"), .UsedRange)
        If c.Value = 1 Then
            If Not rng Is Nothing Then
                Set rng = Union(rng, Intersect(c.EntireColumn, .UsedRange))
            Else
                Set rng = Intersect(c.EntireColumn, .UsedRange)
            End If
        End If
    Next
End With
If Not rng Is Nothing Then rng.Copy Workbooks("CCC.xlsb").Sheets("BBB").Range("A1")
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub

Looks very promising! Could you help me with two perfections:
1) How to paste this as values?
2) How to have not the complete column copied but from row 5 till 2000?
Thanks so far!
 
Upvote 0
Try this version,

Code:
Option Explicit
Sub copy1()
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
Dim c As Range, rng As Range
With Workbooks("AAA.xlsb").Sheets("BBB")
    For Each c In Intersect(.Range("1:1"), .UsedRange)
        If c.Value = 1 Then
            If Not rng Is Nothing Then
                Set rng = Union(rng, Intersect(c.EntireColumn, .Range("5:2000")))
            Else
                Set rng = Intersect(c.EntireColumn, .Range("5:2000"))
            End If
        End If
    Next
End With
If Not rng Is Nothing Then rng.Copy
    Workbooks("CCC.xlsb").Sheets("BBB").Range("A1").PasteSpecial xlValues
    .CutCopyMode = False
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub

Do you need any formatting copied as well?
 
Upvote 0
Try this version,

Code:
Option Explicit
Sub copy1()
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
Dim c As Range, rng As Range
With Workbooks("AAA.xlsb").Sheets("BBB")
    For Each c In Intersect(.Range("1:1"), .UsedRange)
        If c.Value = 1 Then
            If Not rng Is Nothing Then
                Set rng = Union(rng, Intersect(c.EntireColumn, .Range("5:2000")))
            Else
                Set rng = Intersect(c.EntireColumn, .Range("5:2000"))
            End If
        End If
    Next
End With
If Not rng Is Nothing Then rng.Copy
    Workbooks("CCC.xlsb").Sheets("BBB").Range("A1").PasteSpecial xlValues
    .CutCopyMode = False
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
End With
End Sub

Do you need any formatting copied as well?

Tested and works like a charm!!!!! Thanks
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,525
Members
449,088
Latest member
RandomExceller01

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