VBA - Find last used row number within certain columns please

harrycosh

New Member
Joined
Feb 27, 2009
Messages
25
How can I find the last used range within certain columns. I know how to find the last used row number within a whole worksheet or a whole column, but I was looking for the last row number within columns G:L. The problem is I have data within columns A:F that I would like to ignore for this Macro.

Thanks
 

shyrath

Active Member
Joined
Jun 28, 2005
Messages
483
Maybe not the most elegant solution. But for 3 columns it´s not to much code.

Find the last row in G, H, I then find the biggest number of the three. And build the range out of that.

Code:
Sub test()

Dim cntG As Long
Dim cntH As Long
Dim cntI As Long
Dim cntJ As Long
Dim cntK As Long
Dim cntL As Long

Dim rng As Range


cntG = Worksheets("Sheet2").Range("G65536").End(xlUp).Row
cntH = Worksheets("Sheet2").Range("H65536").End(xlUp).Row
cntI = Worksheets("Sheet2").Range("I65536").End(xlUp).Row
cntJ = Worksheets("Sheet2").Range("J65536").End(xlUp).Row
cntK = Worksheets("Sheet2").Range("K65536").End(xlUp).Row
cntL = Worksheets("Sheet2").Range("L65536").End(xlUp).Row

maxr = Application.Max(cntG, cntH, cntI, cntJ, cntJ, cntK, cntL)
Set rng = Worksheets("Sheet2").Range("G2:L" & maxr)

rng.Select

End Sub
 
Last edited:

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
Or perhaps

Code:
Dim LR As Long, x As Long, i As Integer
For i = 7 To 12
    x = Cells(Rows.Count, i).End(xlUp).Row
    If x > LR Then LR = x
Next i
 

shyrath

Active Member
Joined
Jun 28, 2005
Messages
483
Or perhaps

Code:
Dim LR As Long, x As Long, i As Integer
For i = 7 To 12
    x = Cells(Rows.Count, i).End(xlUp).Row
    If x > LR Then LR = x
Next i
There´s always a shorter way :) Gotta love that!!
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
Hi

A non-looping alternative:

Code:
Dim r As Range

With ActiveSheet.Range("G:L")
  Set r = .Find(What:="*",After:=.Cells(1,1), SearchDirection:=xlPrevious, SearchOrder:=xlByRows, Lookin:=xlFormulas)
End With

If Not r Is Nothing Then MsgBox "Last Row in G:L is row " & r.Row
 

harrycosh

New Member
Joined
Feb 27, 2009
Messages
25
Thanks everyone for your input

The different ways of doing things all provide the desired result.

Thanks again
 

Forum statistics

Threads
1,081,793
Messages
5,361,325
Members
400,627
Latest member
Mcomeaux

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top