Solved

Excel VBA to get value from last column of a certain row

Posted on 2011-02-14
5
673 Views
Last Modified: 2012-05-11
The below part of a code gets the cell value of the cell six columns to the right in the row where the string "Orange" appears in column A. How can this be changed to get the value of the last column in that respective row (with "Orange") as it might change frequently and is not fixed anymore. Thanks for the input and help.

(So change from currently:
aCell.Offset(, 6).Value

Open in new window

Set aCell = wb2.Sheets(1).Columns(1).Find(What:="Orange", LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        wb1.Sheets(1).Range("FetchedValue").Value = _
        aCell.Offset(, 6).Value
    Else
        MsgBox "Orange sales not found"
    End If

Open in new window

0
Comment
Question by:stmoritz
  • 3
5 Comments
 
LVL 2

Expert Comment

by:geoffkk
ID: 34893390
Try this
dim maxcol=wb2.Sheets(1).Columns.count
dim col as integer
with wb2.Sheets(1)
Set aCell = .Columns(1).Find(What:="Orange", LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
   
    If Not aCell Is Nothing Then
        for col=cols to 2 step -1
          if not isemptyaCell.Offset(0, col-1) then
            .Range("FetchedValue").Value = _
                    aCell.Offset(, col).Value
            exit for
           end if
        next col
    Else
        MsgBox "Orange sales not found"
    End If
end with
0
 
LVL 41

Expert Comment

by:dlmille
ID: 34893399
aCell.offset(,255).end(xltoleft)  is one way.  Depending on Excel version, the 255 could change.  The approach is to address something at the far right and use the special function.end(xlToLeft) to find that last cell on the right.


Dave
0
 
LVL 41

Expert Comment

by:dlmille
ID: 34893423
Correction - cells(activecell.Row,255).end(xltoleft).address gives you the last rightmost column of the active cell.

so if you want to get the right most column value from aCell, you would replace:

aCell.Offset(,6).value with

Cells(acell.row,255).end(xlToLeft).value


Just tested and it works.  No need for looping.

Dave
0
 
LVL 41

Accepted Solution

by:
dlmille earned 125 total points
ID: 34893446
Ahh it took me a minute - I knew there was a VBA variable for your version of excel...

HEre's my final answer and your SOLUTION:

replace aCell.Offset(,6).value with

Cells(aCell.Row, Cells.Columns.Count).End(xlToLeft).value

Cheers,

Dave
0
 

Author Closing Comment

by:stmoritz
ID: 34895180
Thank you very much! Easy to apply, efficient, works perfect!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now