Results 1 to 4 of 4

Thread: Update Row on another sheet based on Cell Value

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Hello nathan1121,
    Welcome to ExcelFox

    I am slightly puzzled by you explanation, in particular this bit….
    Quote Originally Posted by nathan1121 View Post
    ... I have a button that runs the below macro and it update the row on sheet "Database" where the number matches the Ticket Number in column E.....
    As far as I can tell, the macro makes no attempt to do that. What the macro actually seems to do is , as you further say. ….
    Quote Originally Posted by nathan1121 View Post
    ....it creates new rows if you save the form multiple times instead of updating only the row with matching Ticket Number. ....
    I am not picking hairs, I am just trying to understand what you want. I may have missed the point due to my ignorance.
    It appears to me that you may be asking either of these two

    _Q1 … I have a macro that when I run it adds a new row in worksheet “Database”, for all the info from the Review form on worksheet “Ticket Rating”….
    … but if the ticket number already exists, then I want it to update the matching row instead.
    .


    _Q2 … I have a macro that when I run it adds a new row in worksheet “Database”, for all the info from the Review form on worksheet “Ticket Rating”….
    … I want a macro to update the row on sheet "Database" where the number matches the Ticket Number in column E on worksheet “Ticket Rating”.
    .


    Quote Originally Posted by nathan1121 View Post
    ... Can you tell me what's wrong with my code and how to prevent it from inserting new row when the number already exist in Database sheet? ....
    There is not really anything wrong with your code. Your code is not making any attempt to do what I think you may want. Once again, I am not picking hairs, I am just trying to understand what you want

    I am not 100% sure what you want, so for now I will assume you want like my suggested _Q1

    More fully, this is what I will give you, that is to say the answer to this question.
    Question:
    The macro adds a new row in worksheet “Database”, for all the info from the Review form on worksheet “Ticket Rating” if the Ticket number from F7 in worksheet “Ticket Rating” is not to be found in column E of worksheet “Database”
    That’s good. That stays. That’s what I want.

    But I want that if the Ticket number from F7 in worksheet “Ticket Rating” is already to be found in column E of worksheet “Database”, then the corresponding row in worksheet “Database” should be updated.


    Answer ( and explained )
    Currently the row number in which to put the data in worksheet “Database” is found by this
    iRow = database.Range("e" & Application.Rows.Count).End(xlUp).Row + 1
    What that does is give you the next free row in worksheet “Database” . That is fine if the Ticket number is a new one.
    What we need to do before that is to see if the Ticket number exists, and if it does then the variable iRow needs to be given the row number of the matched Ticket number in worksheet “Database”

    A simple way to achieve this is to use the Match function. In simple terms that returns us the position along a range or array, that it finds something. Pseudo like

    [Position along] = ( [What I am looking for] , [Inside this range or array], 0 )
    ( The option 0 just indicates we want an exact match, not the nearest one )

    I will use specifically one of the available Match functions which returns us something like an error text if it finds no match. That way I can use it at the same time to test for that error, to see if I do have a match. - an error text returned indicates in effect no match.
    If, on the otherhand it does find a match, it returns the position along, which will then be related to the row number I want. ( If I chose my range appropriately, it will be the actual row number I want. )


    So:
    Change this
    Code:
            iRow = database.Range("e" & Application.Rows.Count).End(xlUp).Row + 1
    to this
    Code:
         iRow = database.Range("e" & Application.Rows.Count).End(xlUp).Row + 1 ' This is the number of the next free row in worksheet "Database"
        Dim MtchRes As Variant ' I need variant as I may have  a number if I find a match,  or an error if i dont find one
         Let MtchRes = Application.Match(frm.Range("f7").Value, database.Range("E1:E" & iRow - 1 & ""), 0)   '  [Position along] = ( [What I am looking for] , [Inside this range or array], [0] )        '  [0] tells it to look for exact match     '  If no match is found an error is returned
            If Not IsError(MtchRes) Then
             Let iRow = MtchRes ' Effectively we are changing the row number we will use  to the row numbr where we found a match  
            Else
             ' we had no match. We don't need to do anything because iRow needs to be the nexzt free row, as it already is
            End If
     


    Alan
    Last edited by DocAElstein; 05-29-2022 at 01:23 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    KILL A MODERATOR!!

Similar Threads

  1. Replies: 4
    Last Post: 02-27-2014, 08:49 PM
  2. Replies: 30
    Last Post: 07-19-2013, 07:52 AM
  3. Replies: 2
    Last Post: 05-30-2013, 07:28 PM
  4. Replies: 4
    Last Post: 02-22-2013, 02:24 AM
  5. Autofill the data based on non blank cell in next row?
    By Rajesh Kr Joshi in forum Excel Help
    Replies: 3
    Last Post: 11-29-2012, 04:16 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
  •