Solved

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

Posted on 2011-02-14
5
679 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
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.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now