Nenne
Nenne

Reputation: 160

How to dynamically select a range VBA

My question is very similar to the following:

Excel VBA code to select non empty cells However, I have some empty cells in between. In particular, I'm trying to define a range where the first row needs to be excluded, since it's a header. I also have to exclude the empty cells that follow. I was trying something like

Dim wb as workbook, ws as worksheet
Dim myrange as range

'Defined reference wb and ws
myrange=ws.range("B2",range("B2"),end(xldown))

But this only works if there are not empty cells in between. So, is there a fast and simple way to dynamically select a range that includes non-empty cells, excepted the header?

Upvotes: 0

Views: 6720

Answers (4)

ASH
ASH

Reputation: 20352

I have found this link to be very useful on MANY occasions.

https://www.thespreadsheetguru.com/blog/2014/7/7/5-different-ways-to-find-the-last-row-or-last-column-using-vba

Ways To Find The Last Row

Sub FindingLastRow()
'PURPOSE: Different ways to find the last row number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ActiveSheet

'Using Find Function (Provided by Bob Ulmas)
  LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

'Using SpecialCells Function
  LastRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row

'Ctrl + Shift + End
  LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastRow = sht.UsedRange.Rows(sht.UsedRange.Rows.Count).Row

'Using Table Range
  LastRow = sht.ListObjects("Table1").Range.Rows.Count

'Using Named Range
  LastRow = sht.Range("MyNamedRange").Rows.Count

'Ctrl + Shift + Down (Range should be first cell in data set)
  LastRow = sht.Range("A1").CurrentRegion.Rows.Count

End Sub

Ways To Find The Last Column

Sub FindingLastColumn()

'PURPOSE: Different ways to find the last column number of a range
'SOURCE: www.TheSpreadsheetGuru.com

Dim sht As Worksheet
Dim LastColumn As Long

Set sht = ThisWorkbook.Worksheets("Sheet1")

'Ctrl + Shift + End
  LastColumn = sht.Cells(7, sht.Columns.Count).End(xlToLeft).Column

'Using UsedRange
  sht.UsedRange 'Refresh UsedRange
  LastColumn = sht.UsedRange.Columns(sht.UsedRange.Columns.Count).Column

'Using Table Range
  LastColumn = sht.ListObjects("Table1").Range.Columns.Count

'Using Named Range
  LastColumn = sht.Range("MyNamedRange").Columns.Count

'Ctrl + Shift + Right (Range should be first cell in data set)
  LastColumn = sht.Range("A1").CurrentRegion.Columns.Count

End Sub

Upvotes: 1

iDevlop
iDevlop

Reputation: 25272

If it suits your case, I like to use CurrentRegion with Intersect to eliminate the title row.

with ws.range("b2")
   set myRange = intersect(.currentregion, .currentregion.offset(1,0))
end with
debug.print myRange.address

Upvotes: 0

Super Symmetry
Super Symmetry

Reputation: 2875

Try this

'Defined reference wb and ws
Set myrange = ws.range("B2", ws.Range("B" & Rows.Count).End(xlUp))

Don't forget to use the Set keyword

Upvotes: 2

FaneDuru
FaneDuru

Reputation: 42256

Try the next way, please:

Sub testDefineRange()
 Dim ws As Worksheet, lastRow As Long, myrange As Range

 Set ws = ActiveSheet 'use here what you need
 lastRow = ws.Range("B" & ws.rows.count).End(xlUp).row
 'Defined reference ws
 Set myrange = ws.Range("B2:B" & lastRow)
 myrange.Select
End Sub

Upvotes: 2

Related Questions