Macro to copy based on cell value in column A and row 1

NickvdB

Board Regular
Joined
Apr 30, 2014
Messages
71
Hello,

I've already have a VBA running which is only selecting data when in row 1 c.Value=1 is given, but I would like to enhance this VBA to also look in Column A if there is a 1 and only take the cells with 1 in the column and in the row. In the table below only the once with Yes should be copied.

101
0NoNoNo
1YesNoYes
1YesNoYes
0NoNoNo

<tbody>
</tbody>

Already running VBA is the following, can anybody help me with this one?

For Each c In Intersect(.Range("1:1"), .UsedRange)
If c.Value = 1 Then
If Not rnga Is Nothing Then
Set rnga = Union(rnga, Intersect(c.EntireColumn, .Range("2:500")))
Else
Set rnga = Intersect(c.EntireColumn, .Range("2:500"))
End If
End If
Next
End With
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try modifying this
Code:
If c.Value = 1 Then
To This
Code:
If c.Value = 1 And InterSect(c.EntireColumn, Range("2:500")).End(xlToLeft).Value = 1 Then
This assumes no blank cells in the rows.
 
Last edited:
Upvote 0
Try modifying this
Code:
If c.Value = 1 Then
To This
Code:
If c.Value = 1 And InterSect(c.EntireColumn, Range("2:500")).End(xlToLeft).Value = 1 Then
This assumes no blank cells in the rows.

Ok, there are empty cells in the row, so afraid it is not working.
 
Upvote 0
disregard, working
 
Last edited:
Upvote 0
You could be isolating several rows of data in each iteration of the loop to build the Union range. So checking a single row will not satisfy the requirement. I think maybe a filter might do what you want.
Code:
For Each c In Intersect(.Range("1:1"), .UsedRange)
    If c.Value = 1 Then
        .UsedRange.AutoFilter 1, "1"
        If Not rnga Is Nothing Then
            Set rnga = Union(rnga, Intersect(c.EntireColumn, .Range("2:500")).SpecialCells(xlCellTypeVisible))
        Else
            Set rnga = Intersect(c.EntireColumn, .Range("2:500")).SpecialCells(xlCellTypeVisible)
        End If
    End If
Next
.AutoFilterMode = False

This will get your Union range set to the variable 'rnga' for only those items which have both a 1 in row 1 and a corresponding 1 in column A.
 
Upvote 0
After some more playing around with this, I think if you incorporate the modification below, you will get better results.

Code:
For Each c In Intersect(.Range("1:1"), .UsedRange)
    If c.Value = 1 Then
        .[COLOR=#FFA07A]UsedRange.A[/COLOR]utoFilter 1, "1"
        If Not rnga Is Nothing Then
            Set rnga = Union(rnga, Intersect(c.EntireColumn, .[COLOR=#FFA07A]UsedRange[/COLOR]).SpecialCells(xlCellTypeVisible))
        Else
            Set rnga = Intersect(c.EntireColumn, .[COLOR=#FFA07A]UsedRange).[/COLOR]SpecialCells(xlCellTypeVisible)
        End If
    End If
Next
.AutoFilterMode = False

This prevents the irregular Union range and allows the rnga variable to use basic methods in VBA.
 
Upvote 0
After some more playing around with this, I think if you incorporate the modification below, you will get better results.

Code:
For Each c In Intersect(.Range("1:1"), .UsedRange)
    If c.Value = 1 Then
        .[COLOR=#ffa07a]UsedRange.A[/COLOR]utoFilter 1, "1"
        If Not rnga Is Nothing Then
            Set rnga = Union(rnga, Intersect(c.EntireColumn, .[COLOR=#ffa07a]UsedRange[/COLOR]).SpecialCells(xlCellTypeVisible))
        Else
            Set rnga = Intersect(c.EntireColumn, .[COLOR=#ffa07a]UsedRange).[/COLOR]SpecialCells(xlCellTypeVisible)
        End If
    End If
Next
.AutoFilterMode = False

This prevents the irregular Union range and allows the rnga variable to use basic methods in VBA.

You're great!!! Working perfectly
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,255
Members
448,556
Latest member
peterhess2002

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