Solved

Excel VBA Find last column in range

Posted on 2013-01-06
4
832 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

739 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