Consulting

Results 1 to 8 of 8

Thread: Solved: Offset to 1st & 2nd cell in row

  1. #1
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location

    Solved: Offset to 1st & 2nd cell in row

    Hello

    I use this offset procedure in one of my projects. Is there a way I can make the offset to the first and second cell in the row.

    [vba]
    .Value = Sh.Cells(3, Fnd.Column - 0) & "-" & Fnd.Offset(, -1) & "-" & Fnd.Offset(, -2)
    [/vba]
    Gil

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Excel requires the following logic for the offset function

    OFFSET(reference,rows,cols,height,width)

    In your code, what is the reference cell?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Hello Aussiebear

    Thanks for the reply. I appreciate the points you make. When that line of code is in the project it all runs ok. What I am asking is instead of the offset just looking at -1 or -2 it actually refers back to the 1st & 2nd cells in that row i.e. if on row 7 then it looks at columns A7 and B7.

    Gil

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Where you have omited the Row indicator in Fnd.Offset(, -1)...etc Excel will always assume that the row is the Target or Activecell row. What do you want to acheive?

    I think you may want this:
    [VBA].Offset(, -ActiveCell.Column + 1)
    .Offset(, -ActiveCell.Column + 2)[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi Gil,

    I see you're online right now.

    "...the 1st & 2nd cells in that row i.e. if on row 7 then it looks at columns A7 and B7."

    I take it 1st and second cells in that row means 1st and second Columns in that row?
    [VBA]

    With sh

    Range("somecell") = .Cells(3, fnd.Column) & "-" & .Cells(3,1) & "-" & .Cells(3,2)

    End With

    [/VBA]

    If the row number was variable 'vRow' it would be .Cells(vRow,1) or .cells(vRow,"A") for Col A , etc

    To make it a formula add a "=" & at the beginning...

    Standing by.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  6. #6
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Hello all
    Thank you for the replies. I have tried the first from Simon with some result but it isn't working correctly. I have attached an example worksheet mod1 is how it currently works and mod2 is with Simons input.
    Mod2 finds the first result ok but then seems to put a different offset in. I think it has something to do with this line With c.Offset(, 6)
    Thank you rbrhodes, not sure if your suggestion is what I am after.

    This is the ammended code
    [vba]
    Private Sub Anotherexample1_Click()
    Dim lngLastRow As Long
    ' This loop runs the following code in column A
    Dim Sh As Worksheet
    Dim Fnd As Range
    Dim c As Range
    Dim FirstAddress As String
    Set Sh = Sheets("Sheet2")

    Columns("F:H").ClearContents 'This line is temp to clear old results

    Set c = Cells.Find(What:="1/", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)
    If Not c Is Nothing Then FirstAddress = c.Address
    Do
    Set Fnd = Sh.Cells.Find((Split(c)(1)), LookAt:=xlWhole)
    If Not Fnd Is Nothing Then
    With c.Offset(, 6)
    ' This line is where I applied the suggestion from Simon
    .Value = Sh.Cells(3, Fnd.Column - 0) & "-" & Fnd.Offset(, -ActiveCell.Column - 1) & "-" & Fnd.Offset(, -ActiveCell.Column + 0)
    .Font.Bold = True
    .Font.Color = -16776961
    End With

    Else
    With c.Offset(, 6)
    .Value = "Not found"
    .Font.Bold = True
    .Font.Color = -16776961
    End With
    End If
    Set c = Cells.Find(What:="1/", After:=c, LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False)
    Loop While Not c Is Nothing And c.Address <> FirstAddress

    Columns("F:F").Select
    End Sub
    [/vba]

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is this what you're after?
    [VBA].Value = Sh.Cells(3, Fnd.Column) & "-" & Sh.Cells(Fnd.Row, 2) & "-" & Sh.Cells(Fnd.Row, 1)[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Tutor
    Joined
    Jul 2009
    Posts
    207
    Location
    Hello mdmackillop
    Absolutely spot. Works perfectly for me.
    Many thanks
    Gil

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •