+ Reply to Thread
Results 1 to 12 of 12

VBA Macro to process duplicate phone numbers in 3 worksheets

  1. #1
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    VBA Macro to process duplicate phone numbers in 3 worksheets

    Hello Excel Forum Gurus,

    I am attempting to make my first VBA macro to make life simpler when I get phone number updates from Verizon.

    I have a list of the updated phone numbers, including whether the number is disconnected, or newly activated in a sheet called "VerizonUpdates"

    I also have two blank sheets to process these entries into, "Processed" and "NotFound".

    There are 4 possible outcomes for my information based on the two conditions:
    (1) whether the phone number has already been processed and
    (2) if it is a disconnect or newly activated line.

    I want to take the first row of "VerizonUpdates" and get the phone number.
    See if that phone number (column 11) matches any entries in "Processed"
    If there is a match in processed, and it is a new activated number (signified by an "I" in column 6 in "VerizonUpdates)......overwrite the entire row in "Processed" where the match was found.
    If there is a match in "Processed, and it is a disconnect number (signified by an "O" in column 6 of "VerizonUpdates")....delete the entire row in "Processed" where the match was found.

    If there is NOT a match in "Processed" for the phone number, and it is a newly activated number.....I want to insert that row from "VerizonUpdates" to the top of the "Processed" sheet, pushing all its current entries down
    If there is NOT a match in "Processed" for the phone number, and it is a disconnect number....I want to move the "VerizonUpdates" row to a new line on the "NotFound" sheet, signifying that the number was removed, but never on my Processed list


    Here is my code so far: I am having particular difficulty with the foundRow, signifying the row where the match of the phone number from the Updates occured on the Processed sheet

    Please Login or Register  to view this content.
    Thank you for your help,
    Adam

    Please see a copy of the above with working data in my attachment or at http://ifile.it/10e32ok/VerizonUpdatesMacro_01.xlsm

    I think that my answer lies in transferring Excel functions like Match Index Indirect Address into VBA functions like range. And also exploring the properties of the VBA objects.
    Attached Files Attached Files
    Last edited by delforum; 02-07-2011 at 05:39 PM. Reason: code tags required

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    your sample sheet includes zero data on the PROCESSED sheet, so a worthwhile test is impractical. Isn't the main point of this macro to update existing rows in the PROCESSED sheet?

    Please update your sample to a better example.

    You might also consider adding AFTER versions of the PROCESSED and NOT FOUND sheets so it's clear what you would expect done based on the sample data. You might find it easier to create a fully demonstrative sample workbook by reducing the VERIZONUPDATES sheet to about 25 rows.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    Thank you for letting me know I need more details JBeaucaire. The biggest problem I ran into, is I was using Excel functions and thinking that they would work in a VBA script. I got slapped in the face when I got the error "Sub or function not recognized" when trying to use indirect() and address() and match() while running my initial macro. I did some further digging and found these are not acceptable for macros....and that I must implement the Range() function and other object oriented commands (object.property) to manipulate the data.

    I will make a better sample data file.

  4. #4
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    Here is my NEW sample file. This should be a fun one for all to play with. I filled in the macro as much as possible....It definitely needs some tuning as unlike C++, you cannot define a variable as an array.

    Please take a look at the informative Pseudo-Code worksheet. This is a newly designed sample that still requires the same solution as my first post. Thank you for helping.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    I am working on cleaning up my code. I know there are a ton more clever ways to accomplish this result.

    Please Login or Register  to view this content.
    The thing I have the biggest problem with is the search to find the phone number in another tab.
    Last edited by delforum; 02-02-2011 at 01:48 PM. Reason: Highlight my trouble area.

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    This is how I would approach that:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 02-02-2011 at 11:06 PM. Reason: Added comments

  7. #7
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    Thank you for your work and support. I do have some questions about certain lines used. I am trying to figure out what functions return and the values stored when using definition statements. Your responses will help me tremendously in filling in the VBA concepts I still haven't grasped.


    Dim PhRNG As Range: Set PhRNG = wsUpd.Range("B:B").SpecialCells(xlConstants, xlNumbers)
    1. Does this mean that is only selects the constants and numbers in this B range so all the blank cells at the end of the sheet are ignored?

    Set RwFND = wsPrc.Cells.Find(PhNum, LookIn:=xlValues, LookAt:=xlWhole)
    2. What values will this Find return to RwFND, address of the cell the the match is in? If it doesn't find a match will it return "Nothing" as either a string or a system null indicator?

    Dim PhNum As Range
    For Each PhNum In PhRNG
    3. What does PhNum initialize to, does it start out at one or zero, or is it an address to a cell? Is it just a counter, and if so where does it start? You used it in a For Each statement.

    4. That is about it for code. I would like some suggestions on how to best troubleshoot with the VBA script editor....I still don't feel comfortable printing or msgboxing to see what my code is doing. I would like to be able to see the progression of my variables as it goes through a loop. I spent most of my time programming in C with Matlab, so all the previous values could be easily stored and displayed in a table from the GUI. I don't have that same feel for the VB script editor yet.


    JBeaucaire, thank you for your help! I spent the last 3 days at the computer learning about VBA and trying to get this macro in place to process these updates. Verizon just sent me a new list today.

    -Adam

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    All variables that are dimmed as RANGE are just that, a range of cells, from one cell to a full array.

    I tend to name my variables in a way that indicate what they are. PhNum is the only one a little oblique, but it truly does mean "Phone Number". Basically it means "For each Phone Number currently stored in the range of cells defined as PhRNG"...

    Check out the code above again, I added comments to clarify what each line of code does, though you may still have to examine carefully to see how it's doing that exact thing.

  9. #9
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    JBeaucaire,

    Your comments cleared things up nicely.

    I am still curious about the assumed states of variables of PhNum and RwFND if match is not found.

    Why didn't you initialize PhNum, before sending it to loop with something like
    Please Login or Register  to view this content.
    ? What value with PhNum start out at if it is not initialized like this?

    Also, am I alright to assume if the Cells.Find command doesn't find anything, that the RwFND will be clear of all objects and return Nothing to this variable?

    I was wondering why the
    Please Login or Register  to view this content.
    was outside of the loop, rather than directly above the find statement that could possibly cause the error. Is On Error a global variable that works within the whole Sub() if an Error occurs?

    My next post will include my final solution (thanks to your help!), and revelations I learned along the way.


    You are very kind,
    Adam
    Last edited by delforum; 02-03-2011 at 01:20 AM. Reason: Further VBA Questions Arose

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    Quote Originally Posted by delforum View Post
    Why didn't you initialize PhNum, before sending it to loop with something like
    Please Login or Register  to view this content.
    ? What value with PhNum start out at if it is not initialized like this?
    PhNum is a Range. In the context of the FOR/NEXT, it means "take each cell in the PhRNG and process them one at a time". So the first time through the loop PhNum is given the value of B2 since that's the first "cell" in the PhRNG. The next time through the loop it is automatically given the value of B3. The FOR/NEXT cycles through the cells in order automatically. That's what it does. That's why we used it.


    Also, am I alright to assume if the Cells.Find command doesn't find anything, that the RwFND will be clear of all objects and return Nothing to this variable?
    Correct. In the context of the .FIND, the search for PhNum automatically inserts the value from the PhNum (which is actually a cell - B2, B3, etc.).

    On Error holds its state until you change it to something else. It is often used directly above a single troublesome line of code, then changed to something else after it. If turned it on and left it on because the error code is occurring repeatedly inside the loop. No need to keep turning it on/off.
    I was wondering why the On Error Resume Next was outside of the loop...Is On Error a global variable that works within the whole Sub() if an Error occurs?
    Correct. It's above the loop so that anytime an error occurs in the .FIND method the code will continue to the "IS NOTHING" tests and proceed. Mostly that will result in items being added to the other sheets with nothing deleted.
    Last edited by JBeaucaire; 02-03-2011 at 12:03 PM.

  11. #11
    Registered User
    Join Date
    02-01-2011
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    34

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    JBeaucaire,

    You have solved my problem, and allowed me to grasp a deeper insight into the common ways manipulating rows of data amongst different worksheets can be accomplished. I can't thank you enough....those updates keep on coming, and now I am ready for them. My eyes thank you for their reduced strain, and my mind thanks you for the relief of a weeks work.

    Attached is my final implementation of the code. I put the "VerizonUpdates" into different containers so I know where every piece that was added or changed went to. I just added a couple PhNum or RwFND .copy statements. I feel powerful now. Thank you JBeaucaire.

    Please Login or Register  to view this content.
    Please mark my post as solved. I look forward to learning from more of your work (Index/Match statements, you're next)!

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA Macro to process duplicate phone numbers in 3 worksheets

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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