How to define a range based on the cell value

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
Hi,

I'd like to define a range and include only the entries that have a certain code in the cell next to them.

So my data looks like this:

A B C D E
4 x
3 y
2 x
1 y
5 x

So I need to define a range in vba and have the range be equal to the collection of cells in column A that have an "x" as their value in column B.

So my range would be ("A1", "A3", and "A5")

How can I put this into vba to say set the range = all cells where column B's value is "x", but offset (0,-1).

That would be a HUGE time saver and help!

Thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Maybe something like this...

Code:
Option Explicit

Sub test()

    Dim NewRng As Range
    Dim LastRow As Long
    Dim i As Long

    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = 1 To LastRow
        If LCase(Cells(i, "B").Value) = "x" Then
            If NewRng Is Nothing Then
                Set NewRng = Cells(i, "A")
            Else
                Set NewRng = Union(NewRng, Cells(i, "A"))
            End If
        End If
    Next i
    
    [COLOR="SeaGreen"]'Your code here[/COLOR]
                
End Sub
 
Upvote 0
Thanks so much for your help!

I was looking for the "union" idea that you demonstrated.

I adjusted it in my code so that I can use a Select Case structure, but I did not know how to join the different selections and Union did just the trick.

Thanks!
 
Upvote 0
This awesome code (with slight modification for my situation) helped reduce processing time from 3 min to 8 sec - thanks!!


Maybe something like this...

Code:
Option Explicit

Sub test()

    Dim NewRng As Range
    Dim LastRow As Long
    Dim i As Long

    LastRow = Cells(Rows.Count, "B").End(xlUp).Row
    
    For i = 1 To LastRow
        If LCase(Cells(i, "B").Value) = "x" Then
            If NewRng Is Nothing Then
                Set NewRng = Cells(i, "A")
            Else
                Set NewRng = Union(NewRng, Cells(i, "A"))
            End If
        End If
    Next i
    
    [COLOR=SeaGreen]'Your code here[/COLOR]
                
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

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