+ Reply to Thread
Results 1 to 13 of 13

Help combining macros to delete rows on conditions and add data into cells on conditions

  1. #1
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Help combining macros to delete rows on conditions and add data into cells on conditions

    Hi Everyone,

    I have 2 really great macros I adapted to my needs from other posts but really want to combine them into one macro so that users only have to use one button to complete both processes. One compares column data between sheets and adds to the next empty row in sheet 1 if data from sheet 2 is not present. The other deletes rows from sheet 1 if data exists there that does not exist in sheet 2. This is basically an update to data that will be completed every month when new raw data is pasted into sheet 2. The reason I want to combine them is to make it a one button process for our end users.

    Below is my code from both Subs - I do know that the references to sheets and ranges will need to change but I can't seem to work out which to do first and which references can be changed and which can't.

    Any help would be very much appreciated!

    This is the deleterows sub:
    Please Login or Register  to view this content.
    And the UpdateAddData sub is below:

    Please Login or Register  to view this content.
    Thanks in advance!

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context.

    Show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    Rather than interpreting your code and trying to work out what it's intended to do we generally like to know what the overall aim of the task is so that we can work from first principles. Your macros may not be the most efficient or elegant solutions.

    For instance you're using a looping macro to delete rows. You should always avoid loops for this sort of thing if possible since loops are inherently slow, particularly when they have to jump back and forth between VBA and the Excel App - there's a large time overhead there. The fastest way I know of doing that sort of stuff is to use an Autofilter to display the rows you want to delete and then use a .SpecialCells(xlCellTypeVisible) syntax to delete the whol block of rows with one instruction.

    Neither is it clear from your macro or description whether there is any precedence to which macro should run first and whether there are any constraints. An overall statement of what the system is expected to do and how it's used would help.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Hi Richard,

    Thank you for your response. I have attached both a sample excel file, with reduced data for confidentiality, and a word doc that contains before and after screenshots of the excel data.

    There is no constraint around which macro runs first as they do almost opposing things and the results of one will not affect the running or results of the other.

    Basically what I want the macro to do is to check whether a value (in this case a first and last name) in column A of sheet 2 (7807 Data) exists in column A of sheet 1 (7807) and if not add the value of the missing column A data in sheet 2 to the first empty cell in Column A on sheet 1 (under existing data without changing or moving anything above it). Secondly, I need to check whether a value (again the first and last name) in column A of sheet 1 (7807) exists in column A of sheet 2 (7807 Data) and if not delete the entire row from sheet 1. Basically I want to have deleted staff that don't appear in the most recent raw data paste into sheet 2 be removed from the master display list in sheet 1 and any new staff be added to the raw data paste into sheet 2 to the bottom of the existing staff data in sheet 1. I hope that makes sense!

    Raw data is pasted into sheet 2 (7807 Data) on a regular basis (say weekly). I then have INDEX/MATCH functions in use on sheet 1 (7807) that display the contents of the columns/rows that correspond to Column A in sheet 1 (based on first name/last name).

    I sure hope I've covered everything... and I thank you for your assistance!
    Attached Files Attached Files

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Hi JayJayGC

    Let me see if I understand your requirement...
    One code to do following:
    1.Check Sheets("7807 Data") and if info exists there but not on Sheets("7807") then to add to Sheets("7807")
    2. Check Sheets("7807") and if info exists there but not on Sheets("7807 data") then to delete from Sheets("7807")

    If so....
    Please Login or Register  to view this content.
    Last edited by sintek; 10-03-2017 at 08:42 AM.
    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 star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Hi again Sintek, and thank you! After yesterday I got up the courage to try some other things with my sheets

    Yes that's precisely what I'm after... but I'm getting a runtime error on the delete portion - its stops here:

    Please Login or Register  to view this content.
    Actually this could be because I've protected the sheet... yes? I do have an "OpenWorksheet" unprotect macro in place for "UserInterfaceOnly:=True" but that won't allow the delete will it? ... Hmmm... I'll unprotect it and see if it works before I try to tackle the protect problem again

  6. #6
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Hi again, so it's nice to see I'm not a complete idiot - at least I realised what the problem was... it worked perfectly once I unprotected the sheet - you're an absolute legend sintek!!

    But how do I protect all the cells that have my INDEX/MATCH data and still allow a delete row? Can I unprotect inside the macro and then protect again at the end? I'm sure I've read that's tricky and dangerous... any pointers you can give me would be fantastic - again

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    See amended code in Post 4
    Change "Your Password" to the password you are using...

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Hi,

    Maybe something like the following (also see attached)
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Richard Buttrey; 10-03-2017 at 09:02 AM.

  9. #9
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Thank you! It works perfectly - I'm indebted to you again

    Many thanks for helping me finish this project - it's been a tough week trying to get my amateur attempts at coding to work, and I would never have got there without your help. I learnt a lot from you, I appreciate that most of all.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    @ Richard

    Absolutely love your code, so much more efficient.
    One question though...
    Should This line of code
    Please Login or Register  to view this content.
    perhaps be
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-24-2017
    Location
    Gold Coast, Australia
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Gosh Richard, thank you too! I'm quite proud that I understand (more or less) what your code is doing and why... last week I would have struggled with that

    Yours is definitely more efficient, sintek is quite correct on that - so thanks again, I appreciate the help very much (particularly since I've now learnt something else, winning!)

    You both deserve medals for helping newbies learn

  12. #12
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Mmm
    Interesting Q.

    I suppose it all depends on whether there is a row of column labels that will always be present.

    Must admit I've always felt the CurrentRegion property could be much enhanced by having a parameter that indicated whether column labels should be excluded. I often find myself working round this, particularly when an autofilter doesn't return any values making it a nuisance to bypass any code that needs to do stuff if there are filtered values.

  13. #13
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: Help combining macros to delete rows on conditions and add data into cells on conditio

    Points to ponder...
    I always just use...
    Please Login or Register  to view this content.

+ 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. [SOLVED] Delete rows with conditions
    By stberger in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-27-2017, 01:50 AM
  2. [SOLVED] Delete rows with conditions
    By yukioh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2016, 10:42 PM
  3. Replies: 2
    Last Post: 01-14-2016, 03:53 PM
  4. [SOLVED] Trying to delete some rows if conditions met or do nothing if not met
    By rreifs68 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-21-2015, 10:16 PM
  5. Combining Cells for specific conditions
    By rize1159 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-04-2014, 12:59 PM
  6. Delete rows with conditions
    By mridzuani in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-13-2008, 02:52 AM
  7. Delete Rows & Column Per Conditions
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-29-2007, 01:25 PM

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