Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Return Row - Column for cell addresses

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Smile Return Row - Column for cell addresses

    Hello, I need the code below to return not only the row for where the data is found but the column, but have it where it shows the Row number. then the "-" then the Letter for example: (232540-J)

    Sub mySearch() For Each gCell In Range("AN1", Range("AN5000").End(xlUp))
    If IsEmpty(Cells(gCell.Row, 8)) Then
    For Each c In Range("E1:AL10000")
    i = Application.Match(gCell.Value, Range(c, Cells(325000, c.Column)), 0)
    If Not IsError(i) Then
    Cells(gCell.Row, 8) = i
    Exit For
    End If
    Next
    If IsEmpty(Cells(gCell.Row, 8)) Then
    Cells(gCell.Row, 8) = "#N/A"
    End If
    End If
    Next
    End Sub
    Thank you very much in advnace for help on this!!!

    P.S. I had attached a file to view the example.
    Attached Files Attached Files

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    How long have you been here? And you still haven't learned how to use the # Icon?
    Sub mySearch()    
    For Each gCell In Range("AN1", Range("AN5000").End(xlUp))
            If IsEmpty(Cells(gCell.Row, 8)) Then
                For Each c In Range("E1:AL10000")
                    i = Application.Match(gCell.Value, Range(c, Cells(325000, c.Column)), 0)
                    If Not IsError(i) Then
                        Cells(gCell.Row, 8) = i
                        Exit For
                    End If
                Next
    
                If IsEmpty(Cells(gCell.Row, 8)) Then
                    Cells(gCell.Row, 8) = "#N/A"
                End If
            End If
        Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    So I refactored your code, meaning I did not change what it does, just made it more "Best Practice" so I could understand it

    Sub mySearch()    
    Dim gCell As Range
    Dim c As Range
    Dim i As Variant
    Dim r as Long
    
    For r = 1 to Cells(Rows.Count, "AN").End(xlUp.Row
    
              If IsEmpty(Cells(r, "H")) Then
                   For Each c In Range(Range("E1"), Cells(Rows.Count, "AL").End(xlUp))
    'Holy Loops Batman! For every cell in 34 columns, Find a Match in the rest of the column below that cell! Of course that only happens when there is no matching value.
    'You are lucky that Excel goes across, then down
                       i = Application.Match(Cells(r, "AN").Value, Range(c, c.End(xlDown)), 0)
                       If Not IsError(i) Then 
                            Cells(r, "H") = i
     'i = the number of rows below c.Row
    'the Column = c.Column
                            Exit For
                       End If
                  Next
             End If
    
             If IsEmpty(Cells(r, "H")) Then Cells(r, "H") = "#N/A"
    Next
    End Sub
    Last edited by SamT; 07-24-2017 at 06:13 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    What do you mean about the hash tag? above in post 1, I
    used the "-" for a dash in between the row number and column letter.

    I under stand the where you put the H for the 8, I meant to change that to 41 so that would be AO.
    is there a way to get add to macro to get the row number with the column?
    Last edited by estatefinds; 07-24-2017 at 06:20 PM.

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    This is how I would do it. (complied but not tested)
    Option Explicit
    
    Sub mySearch()
    Dim colH As Range
    Dim colAN As Range
    Dim DataTable As Range
    Dim Found As Range
    Dim Location As Variant
    Dim rw As Long
    
    Set colH = Range("H:H")
    Set colAN = Range("AN:AN")
    Set DataTable = Range(Range("E1"), Cells(Rows.Count, "AL").End(xlUp))
    
    For rw = 1 To Cells(Rows.Count, "AN").End(xlUp).Row
      With colH
        If IsEmpty(.Cells(rw)) Then
          Set Found = DataTable.Find(colAN.Cells(rw))
          If Found Is Nothing Then
            .Cells(rw) = "#NA"
          Else
            Location = Split(Found.Address, "$")
            .Cells(rw) = Location(0) & "-" & Location(1)
          End If
        End If
      End With
      Set Found = Nothing
    Next
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I ran it and it only returns the

    -E
    -E
    -O
    -X
    -E
    -E
    -E
    -X
    in the AO column.
    just need row in cluded before the Dash



    Sub mySearch()
    Dim colAO As Range
    Dim colAN As Range
    Dim DataTable As Range
    Dim Found As Range
    Dim Location As Variant
    Dim rw As Long
         
    Set colAO = Range("AO:AO")
    Set colAN = Range("AN:AN")
    Set DataTable = Range(Range("E1"), Cells(Rows.Count, "AL").End(xlUp))
         
    For rw = 1 To Cells(Rows.Count, "AN").End(xlUp).Row
    With colH
    If IsEmpty(.Cells(rw)) Then
    Set Found = DataTable.Find(colAN.Cells(rw))
    If Found Is Nothing Then
    .Cells(rw) = "#NA"
    Else
    Location = Split(Found.Address, "$")
    .Cells(rw) = Location(0) & "-" & Location(1)
    End If
    End If
    End With
    Set Found = Nothing
    Next
    End Sub

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    .Cells(rw) = Location(2) & "-" & Location(1)
    See? I selected that line, then clicked the # icon on the menu.

    Try it, then click the A/A Icon to view the Source of your post

    My bad...
    The first "slot" of Location Array is empty
    Array = Split ("$A$1", "$")
    Array(0) =character(s) before first $
    Array(1) = Character(s) after first $
    Array(2) = character(s) after next $
    Last edited by SamT; 07-24-2017 at 07:36 PM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I'm kinda stuck where I put post #7

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Please read this on posting code
    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'

  10. #10
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Sub mySearch()
        Dim colAO As Range
        Dim colAN As Range
        Dim DataTable As Range
        Dim Found As Range
        Dim Location As Variant
        Dim rw As Long
    
        Set colAO = Range("AO:AO")
        Set colAN = Range("AN:AN")
        Set DataTable = Range(Range("E1"), Cells(Rows.Count, "AL").End(xlUp))
    
        For rw = 1 To Cells(Rows.Count, "AN").End(xlUp).Row
            With colH
                If IsEmpty(.Cells(rw)) Then
                    Set Found = DataTable.Find(colAN.Cells(rw))
                    If Found Is Nothing Then
                        .Cells(rw) = "#NA"
                    Else
                        Location = Split(Found.Address, "$")
                        .Cells(rw) = Location(0) & "-" & Location(1)
                    End If
                End If
            End With
            Set Found = Nothing
        Next
     End Sub

  11. #11
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I went ahead and did the # and I now see what you mean, Thank you . Im stuck on where to place the code below to to get the row location placed before the dash that is before the Column Letter. for example: (232540-J) or (5-E) etc.

    My bad...
    The first "slot" of Location Array is empty
     Array = Split ("$A$1", "$")
     Array(0) =character(s) before first $
     Array(1) = Character(s) after first $
     Array(2) = character(s) after next $
    in the code.
    Last edited by estatefinds; 07-25-2017 at 06:57 AM.

  12. #12
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Quote Originally Posted by estatefinds View Post
    I'm kinda stuck where I put post #7
    Replace the line that looks almost exactly like it with it.

    Replace the line that you said wasn't working right with it

    Read the code that isn't working right and it will be obvious.

    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  13. #13
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Quote Originally Posted by estatefinds View Post
    I went ahead and did the # and I now see what you mean, Thank you . Im stuck on where to place the code below to to get the row location placed before the dash that is before the Column Letter. for example: (232540-J) or (5-E) etc.

    My bad...
    The first "slot" of Location Array is empty
     Array = Split ("$A$1", "$")
     Array(0) =character(s) before first $
     Array(1) = Character(s) after first $
     Array(2) = character(s) after next $
    in the code.
    That is a HELP snippet, to HELP you understand the use of "Split" and Arrays.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  14. #14
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Can someone please ask the TS to change the title of this thread into something meaningful (for present and coming visitors) ?

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by snb View Post
    Can someone please ask the TS to change the title of this thread into something meaningful (for present and coming visitors) ?
    Changed as requested.
    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'

  16. #16
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Ok, I'm working on it, thank you!

  17. #17
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I dont know why Im having such trouble

    Else
                        Array = Split(Found.Adress "$A$1", "$")
                        .Cells(rw) = Location(0) & "-" & Location(1)
                        Next
                    End If
    Im getting syntax on the Array= Split

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    typo: Address
    should "Location" be "Array"?
    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'

  19. #19
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Else
                        Array = Split (Found.Address"$A$1", "$")
                        .Cells(rw) = Array(0) & "-" & Array(1)
                    End If

    so Im adding taking away and getting same errors

    I dont understand this concept, this array

    i looked in my book, programming with microsoft visual basic 2015 and cant find anything in book that could help me figure this out.


    need some insight on this , im just having trouble putting it together
    Last edited by estatefinds; 07-25-2017 at 04:15 PM.

  20. #20
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    
    Sub mySearch() 
        Dim colAO As Range 
        Dim colAN As Range 
        Dim DataTable As Range 
        Dim Found As Range 
        Dim Location As Variant 
        Dim rw As Long 
         
        Set colAO = Range("AO:AO") 
        Set colAN = Range("AN:AN") 
        Set DataTable = Range(Range("E1"), Cells(Rows.Count, "AL").End(xlUp)) 
         
        For rw = 1 To Cells(Rows.Count, "AN").End(xlUp).Row 
            With colH 
                If IsEmpty(.Cells(rw)) Then 
                    Set Found = DataTable.Find(colAN.Cells(rw)) 
                    If Found Is Nothing Then 
                        .Cells(rw) = "#NA" 
                    Else 
                        Location = Split(Found.Address, "$") 
                        .Cells(rw) = Location(0) & "-" & Location(1) 
                    End If 
                End If 
            End With 
            Set Found = Nothing 
        Next 
    End Sub
    Last edited by estatefinds; 07-25-2017 at 05:29 PM.

Posting Permissions

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