How to set a Variying Range in vba Excel.

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello all,

How do I set special range in the below vba. Currently the range is set to entire column B.

VBA Code:
   Set Fnd = Range("B:B").Find("b*", , xlValues, xlWhole, , , False, False)

How do I set the range to be column B but the row should begin from first data cell after blank cell availabe at the top direction from
the active row and til the first blank cell at the bottom direction from the same active row.

1607175684146.png


To explain if my current active row is 10 and column B5 to B15 are all data cells but B4 and B16 are blank cells , then
I want the range to be B5 to B15. And if my active row of the same sheet is 20 and B17 to B30 are all data cells,
except B16 like before is blank and so is B31, hence my range this time should be B17 to B30.

How do I achieve this please??

Will appreciate a lot and thanks.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Does this help?
VBA Code:
Sub test()
    MsgBox Range("B" & ActiveCell.Row).CurrentRegion.Address
End Sub
 
Upvote 0
Will column A be totally blank or will there be data in there?
 
Upvote 0
@Fluff
Thanks for picking up on that. I guess both columns A and C will have to be blank in order for my suggestion to work.
 
Upvote 0
Will column A be totally blank or will there be data in there?
although what substitue is for this if the column A and perhaps other columns are not entirely empty?
 
Upvote 0
How about
VBA Code:
   Dim Rng As Range
   Set Rng = Range("B" & ActiveCell.Row).CurrentRegion.Columns("B")
But if col A is blank this will not work.
 
Upvote 0
This should work regardless
VBA Code:
   Dim Rng As Range
   Set Rng = Range("B" & ActiveCell.Row).CurrentRegion
   If Split(Rng.Address(1, 0), "$")(0) = "A" Then
      Set Rng = Rng.Columns(2)
   Else
      Set Rng = Rng.Columns(1)
   End If
 
Upvote 0
Solution
How about
VBA Code:
   Dim Rng As Range
   Set Rng = Range("B" & ActiveCell.Row).CurrentRegion.Columns("B")
But if col A is blank this will not work.
Yes, now I have the option to go with either of the vba depending on the situation.

Thanks again both for yours valuable input..
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,546
Members
449,038
Latest member
Guest1337

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