Macro that selects a dynamic range until the cell value turns 0

raku

New Member
Joined
Oct 6, 2011
Messages
4
Hi

I need a macro that would do a range selection based on the call value.

my starting cell is A240. I have to select all the cells in column A until it sees a cell value 0. As soon as the cell value 0 is encountered the selection should only select until the previous cell. And then it should select up to the H column. For eg if the cell value in say A260 is zero then then I should be able to select the range A240:H259.

Thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this

Code:
Sub selRange()
    Dim rngFound As Range
    
    Set rngFound = Range(Range("A240"), Range("A240:A" & Cells(Rows.Count, "A") _
    .End(xlUp).Row).Find(What:=0, LookAt:=xlWhole).Offset(-1, 7))
    
    rngFound.Select
    
End Sub

HTH

M.
 
Upvote 0
Thank you so much for the solution

I got this following error:

Run-time error '91':


Object variable or With block variable not set
 
Upvote 0
When there is no 0 (zero) in column A this error-message appears

Try this new version

Code:
Sub selRange()
    Dim rngFound As Range
    
    Set rngFound = Range("A240:A" & Cells(Rows.Count, "A") _
    .End(xlUp).Row).Find(What:=0, LookAt:=xlWhole)
    
    If Not rngFound Is Nothing Then
        Set rngFound = Range(Range("A240"), rngFound.Offset(-1, 7))
        rngFound.Select
    Else
        MsgBox "Not found 0 in column A"
    End If
    
End Sub
 
Upvote 0
hi there, your code worked like a charm!

I have another issue of handling the zero in some sheets. The cells that had the value 0 typed in had no issued in being detected. However some of the cells reference to another cell for values. Like the cell value in A263 is 0 which is obtained by referencing to cell A115 (I have the formula =A115 typed in cell A263). So i tried what:="0" instead of just what:=0 in your code. But it still doesnt detects and goes in to the else loop.

Thanks!
 
Upvote 0
Try changing this code-line
Code:
Set rngFound = Range("A240:A" & Cells(Rows.Count, "A") _
    .End(xlUp).Row).Find(What:=0, LookAt:=xlWhole)

To
Code:
Set rngFound = Range("A240:A" & Cells(Rows.Count, "A") _
    .End(xlUp).Row).Find(What:=0, LookAt:=xlWhole, [B]LookIn:=xlValues[/B])

M.
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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