Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA Find last column in range

Posted on 2013-01-06
4
Medium Priority
?
837 Views
Last Modified: 2013-01-06
Ugh!!

Hell Experts,

I need help to find the last used cell column number in a set range.
My range is "B4:H4".  As long as there is something in one of those cells - that is my column number I need.

I have data in A4 & I4 so I can not use the typical examples I found on-line.

Once I find the column number - I can finish my code.

Any assistance would be great,
Thanks
0
Comment
Question by:mike637
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 10

Expert Comment

by:etech0
ID: 38749106
Will the range always be B4:H4?
0
 

Author Comment

by:mike637
ID: 38749109
Yes it will.
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 2000 total points
ID: 38749115
Try this function:
Option Explicit

Public Sub Test()

    Dim s As Worksheet
    Dim myRange As Range
    
    Set s = ActiveSheet
    Set myRange = s.Range("B4:H4")

    Debug.Print GetLastColNum(myRange)

End Sub

Public Function GetLastColNum(ByRef rngToCheck As Range) As Long

    Dim i As Long
    
    ' return 0 if nothing is found
    GetLastColNum = 0
    
    For i = rngToCheck.Cells.Count To 1 Step -1
        If Not IsEmpty(rngToCheck.Cells(i).Value) Then
            GetLastColNum = rngToCheck.Cells(i).Column
            Exit For
        End If
    Next i

End Function

Open in new window

0
 

Author Closing Comment

by:mike637
ID: 38749137
Thank you Andrew!

I was about to give up and go get a drink. :)

Also - thanks for taking this on a Sunday - you are awesome.

Michael
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question