Consulting

Results 1 to 8 of 8

Thread: VBA - matching and adding value with conditions - please help

  1. #1

    VBA - matching and adding value with conditions - please help

    I have a single cell reference and I am trying to search another sheet to find the first match in column c and add 1 to the value in column m. However if column m matches the value in column p, I want to find the next match and add one with the same conditions. If I find a match and add 1 to the value in column m, I want the macro to stop. If all matches produce a matched value between column p and m, I want to still add one to the first match.


    Can someone help. I currently have something created that finds the first match and adds one to the value in column M, but I am stuck on how to proceed

    Note: tried to add a sample of what I have so for, but it will not upload from my phone.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Welcome to VBAX Ella63112. Can you upload a file to explain which column on which sheet matches what value etc.

    At this stage you have stated you would like
    a) Find a First Match in Column C on another sheet,
    b) If Found then add 1 to Column M value, and then
    c) if Column M.Value then matches Column P Value, then
    d) move to the next match in Column C.

    However, you also want (as written),
    e) If I find a Match and Add 1 to Column M Value you want the macro to stop....

    Then to complicate it a little further,
    f) If all the Matches produce a matched value between Columns P & M, I still want to add 1 to the First Match.

    Seriously a sample workbook would be extremely helpful.
    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
    Quote Originally Posted by Aussiebear View Post
    Welcome to VBAX Ella63112. Can you upload a file to explain which column on which sheet matches what value etc.

    At this stage you have stated you would like
    a) Find a First Match in Column C on another sheet,
    b) If Found then add 1 to Column M value, and then
    c) if Column M.Value then matches Column P Value, then
    d) move to the next match in Column C.

    However, you also want (as written),
    e) If I find a Match and Add 1 to Column M Value you want the macro to stop....

    Then to complicate it a little further,
    f) If all the Matches produce a matched value between Columns P & M, I still want to add 1 to the First Match.

    Seriously a sample workbook would be extremely helpful.
    I've attached a sample workbook
    Attached Files Attached Files

  4. #4
    Quote Originally Posted by Ella63112 View Post
    I've attached a sample workbook
    Correction - referenced cell needs to match column B

    Not C!

  5. #5
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Concept only.
    Sub FindValues()
    Dim lookUpSheet As Worksheet, updateSheet As Worksheet
    Dim valueToSearch As String
    Dim i As Integer, t As Integer
    Dim cKNum As Integer
    Dim shpNum As Integer
    Set lookUpSheet = Worksheets("Scanned")
    Set updateSheet = Worksheets("Shipped")
    Set cKNum = updateSheetSheet(t, 12)
    Set sHipNum = updateSheet(t, 16)
    'get the number of the last row with data in Scanned and in Shtpped
    lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowUpdate = updateSheet.Cells(Rows.Count, "B").End(xlUp).Row
    'for every value in column A of Scanned
    For i = 1 To lastRowUpdate
         valueToSearch = updateSheet.Cells(i, 1)
         'look for the match in column B of Shipped
         For t = 2 To lastRowLookup
            'if found a match, add I to cKNum value and proceed to the next value
            If lookUpSheet.Cells(t, 2) = valueToSearch Then
                cKNum = cKNum +1
                If cKNum = shipNum Then
                      ‘Look for next match
                      Next t
                Else
                      Msgbox “Only 1 Match Found”
                End If
                Exit For
            End If
         Next t
    Next i
     End Sub
    I'm sure someone will tell me I've got it wrong.
    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

  6. #6
    Quote Originally Posted by Aussiebear View Post
    Concept only.
    Sub FindValues()
    Dim lookUpSheet As Worksheet, updateSheet As Worksheet
    Dim valueToSearch As String
    Dim i As Integer, t As Integer
    Dim cKNum As Integer
    Dim shpNum As Integer
    Set lookUpSheet = Worksheets("Scanned")
    Set updateSheet = Worksheets("Shipped")
    Set cKNum = updateSheetSheet(t, 12)
    Set sHipNum = updateSheet(t, 16)
    'get the number of the last row with data in Scanned and in Shtpped
    lastRowLookup = lookUpSheet.Cells(Rows.Count, "A").End(xlUp).Row
    lastRowUpdate = updateSheet.Cells(Rows.Count, "B").End(xlUp).Row
    'for every value in column A of Scanned
    For i = 1 To lastRowUpdate
         valueToSearch = updateSheet.Cells(i, 1)
         'look for the match in column B of Shipped
         For t = 2 To lastRowLookup
            'if found a match, add I to cKNum value and proceed to the next value
            If lookUpSheet.Cells(t, 2) = valueToSearch Then
                cKNum = cKNum +1
                If cKNum = shipNum Then
                      ‘Look for next match
                      Next t
                Else
                      Msgbox “Only 1 Match Found”
                End If
                Exit For
            End If
         Next t
    Next i
     End Sub
    I'm sure someone will tell me I've got it wrong.
    I can't get it to work at all. Is there commands here that are depenant on the version I'm using?

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Where does it error out?
    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

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Be as you wish to seem

Posting Permissions

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