+ Reply to Thread
Results 1 to 15 of 15

How to replace values in a table based on a cell using VBA

  1. #1
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    How to replace values in a table based on a cell using VBA

    I'm having a really hard time finding a light (computacional wise) way to find a value in a cell and replacing all values on the row this value appear. I tried to use the match function, but to no avail, as I'm a begginer in VBA. I use the following code to create a new row and fill all the collumns needed (shortened, as the full code is really long):

    "Private Sub UpdateButton_Click()

    Dim myRow As ListRow
    Dim intRows As Integer

    intRows = ActiveWorkbook.Worksheets("Data").ListObjects("DataBase").ListRows.Count
    Set myRow = ActiveWorkbook.Worksheets("Data").ListObjects("DataBase").ListRows.Add(intRows)


    'Header filling
    myRow.Range(1) = Range("C8")
    myRow.Range(12) = Range("C7")
    myRow.Range(13) = Range("H8")"


    "Database" is a Table and Data, as the formatting suggests, a sheet. The number inside "myRow.Range" are the columns I'm adding the values to and the cell reference are cell in a form.

    I'm trying to adapt this code to run the function I'm intending, but I'm not finding a way to make it work.

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    This makes no sense...Can you please upload a sample file and explain in detail what it is you are actually wanting to accomplish...See top yellow banner...
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    Here's the Sample. The first sheet we have some field that is intended to be user input, after inpunting the data, there's 2 options, the first add a new line in the table in the "Data" sheet, the second one is meant to overwrite the data, searching for the Row that have the PJ value the same entered by the user in the cell on the right side of the cell named "Search for PJ".


    So, I'm trying to write a code that when the "Update Data" is clicked, to search for the value inside the cell F3 inside the first colum of the "DataBase" table and then overwrite all information with the data entered in the rows beside the button. The same code of the first button, but instead of creating a new line, it will overwrite the columns.
    Attached Files Attached Files

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    Please Login or Register  to view this content.
    Change your add snippet to this...
    Please Login or Register  to view this content.
    PS...Does not check to see if already exists...
    Or you can make use of one code for both buttons...Let me know if this is something you might consider...

    Please Login or Register  to view this content.
    Last edited by Sintek; 10-03-2022 at 10:06 AM.

  5. #5
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    I must thank you for the support. But there some problems that I didn't address, the input columns of the original worksheet i'm working on isn't stacked like the sample, the input cells are all over the place, thats why I did the add method cell by cell plus some ranges. This was my mistake to not specify it.

    So a thing to consider is, I can't see how to "translate" the Update code from the range B1:B11 to cell by cell basis.

    PS.: I really liked the Add code, but it suffers from the same problem described above, but still, the code will help me to find new ways to do the code i'm looking for.

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    Well that was a pointless exercise and a waste of time...
    Can only assist if you upload a sample file depicting your actual file setup...
    Will probably be something like this...

    Please Login or Register  to view this content.
    Last edited by Sintek; 10-03-2022 at 10:35 AM.

  7. #7
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    The structure is the same, but changed the values to reflect the last example. But both structures coexist in the same sheet, the stacked inputs and the forms that i added below.
    Attached Files Attached Files

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    So are you entering data on [Add and Update] sheet in the cells as per below...and then data gets added|edited to database...

    Untitled.png


    Tx for rep added...This below though...really?

    You gave incorrect data setup...

    Untitled.png
    Last edited by Sintek; 10-03-2022 at 10:56 AM.

  9. #9
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    Yes, this is exactly what I'm doing, and it's working fine when clicking on the add button, my problem is that, when I try to to the exact same funtion, but instead of creating a new line, search for the value on the F3 cell on the second sheet, and then adding the info to the same line, I can't find a way to do this efficiently (and working).

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    This uses C16 as the search criteria...Have no idea why you have another sheet?
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    It works really well, I'll try to adapt to some needs I have, like not changing to blank, when not inputing anything on the "to update" fields.

    I tried using the Match function, but the documentation was not that well written, would you have a good documentation to name?

    And thank you so much for the support and patience.

    About the last question, I'm trying to make a Menu base workbook to register some forms in a lighter way and way more interactive than the manual input, signing, and register of the documents I use. So I'm using the sheets as diferent pages with multiple functions, as I need to make it with Excel because of compatibility.

  12. #12
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    like not changing to blank, when not inputing anything on the "to update" fields
    You will need to build in some checks to your code...Such as...
    If one of the ranges are blank then message Box...Please Complete All Boxes etc etc ...
    I tried using the Match function, but the documentation was not that well written, would you have a good documentation to name?
    Have no idea what this means...
    I'm trying to make a Menu base workbook to register some forms in a lighter way and way more interactive than the manual input, signing, and register of the documents I use. So I'm using the sheets as diferent pages with multiple functions, as I need to make it with Excel because of compatibility.
    I suggest looking into UserForms Interface...There are hundreds of database examples on this site...

  13. #13
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    Quote Originally Posted by sintek View Post
    So are you entering data on [Add and Update] sheet in the cells as per below...and then data gets added|edited to database...

    Attachment 798906


    Tx for rep added...This below though...really?

    You gave incorrect data setup...

    Attachment 798910
    About this bit. I'm not a native english speaker so I might get misunderstood, what I meant was that you give me the tools to do what I wanted without giving me the final answer, allowing me to force myself to understand the principles and learn a little bit more.

  14. #14
    Registered User
    Join Date
    09-06-2022
    Location
    Brazil
    MS-Off Ver
    1808
    Posts
    11

    Re: How to replace values in a table based on a cell using VBA

    Quote Originally Posted by sintek View Post
    I suggest looking into UserForms Interface...There are hundreds of database examples on this site...
    About this, I tried it at first, but we already had the form in excel format, so I spent a lot of time searching for a way to "convert" from excel format to userform, the best sollutions i found involved adding a print of the form on the background and adding the textboxes over the form, which gave a awful result, so I just decided it was better to copy and paste the info in a separate table.

  15. #15
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: How to replace values in a table based on a cell using VBA

    Is there anything else you need assistance with...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Find and Replace Formula (VLOOKUP Maybe?) Based on Cell Values
    By jimmythegentleman in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-28-2022, 08:31 PM
  2. Using a data table to replace time values with points values.
    By RobAMG in forum Excel Formulas & Functions
    Replies: 17
    Last Post: 02-04-2022, 05:54 AM
  3. Return value from array in table based on cell values in separate table
    By CapRavOr in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-08-2021, 11:04 PM
  4. Replies: 3
    Last Post: 01-18-2021, 11:35 AM
  5. Table Resizing (based on two cell values outside of table)
    By jrabelo in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 02-24-2020, 10:38 AM
  6. [SOLVED] Replace values in worksheet based on cell value
    By DScho in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2018, 07:47 AM
  7. [SOLVED] Macro to Find/Replace Record in One Table Based on Records in Another Table
    By DavidFC in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-01-2018, 04:41 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1