Link to home
Start Free TrialLog in
Avatar of donohara1
donohara1

asked on

identifying last value in a row.

I have a VBA app (Excel), in which I need to identy the column notation (A, B,...) of the last value in a row. (I need to then insert that value in a subsiquent formula.) I already know which row to test on.

I would go to specific row and then determione the column notation value (lacall).
Then I would do a copy&paste  operation as such:

    SourceSh.Range(lacall & (958 + ioffset) & "." & lacall & (963 + ioffset)).Copy
                        r.Offset(0, 63).PasteSpecial xlPasteValues, , , True

Curently I retrieve LACALL from the user's workbook entry.  Would be better If I figure it out.

Thanks
Don OHara
Avatar of andrewssd3
andrewssd3
Flag of United Kingdom of Great Britain and Northern Ireland image

This function will get the column letter for the last cell in the row you pass to it:
Public Function GetLastColLetter(ByRef rngRow As Excel.Range) As String

    Dim c As Excel.Range
    Dim strRange As String
    
    Set c = rngRow.EntireRow.Cells(1, rngRow.Worksheet.Columns.Count)
    Set c = c.End(xlToLeft)
    
    strRange = c.Address(False, False)
    GetLastColLetter = Replace(strRange, CStr(c.Row), "")
    
End Function

Open in new window

Is the range operator in your location '.'? - if not you may need to change it to a ':' in the example copy you give.
@andrewssd3 - is there a partcular reason you declare as Excel.Range as opposed to just Range?
Yes - it's really just a habit, but I have done a lot of developing of cross-product applications, where I had references say to Word and Excel in the same project.  Then it's sometimes essential to differentiate - Range would be one of those, as both Word and Excel have Range objects.  I just tend to do it all the time now, as it saves trouble later if the code need to be used in a cross-product context.
Avatar of donohara1
donohara1

ASKER

DEMO Code

Dim THisRow As Range
Dim GetLastColLetter As String
  ...
Set Sourcewb = Workbooks.Open(filepath & Filename, False)      (This works - DOH)
   ...  
 Set SourceSh = Sourcewb.Sheets("Agg Perf History")
                       THisRow = SourceSh.Range("a61:aaa61")
                       lacall = GetLastColLetter(THisRow)
                       MsgBox "lacall is: " & lacall

------------

When I run this, I get 'Compile Error'

AS you can see, I am looking at the row a61 to ea61 and want to find the column ID for the latest value.   Is there a simple code to select this row and call the function so I can use the column ID in a number of copy&paste commands.


Thanks,
Don
SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to both of you.