0

I have 2 sheets with only 2 columns:

  • Old URL
  • New URL

One sheet (master) contains 20k rows of those two columns and I have another sheet (toImport) with about 400 rows of those same two columns.

Since it's for a redirect file sent to apache and I do this because of a big structure change in our web environment, before importing those 400 rows in the 20k rows sheet I want to evaluate some scenarios:

  • If the old and new pair from the toImport file is already in the master file: do nothing with it
  • If the old URL from the toImport file is in the new URL column from the master file, do the following:
  • Put the new URL value from toImport file in the new URL value of master (in order not to create a cascading redirection)
  • Also import the old/new pair from toImport, as-is in the master file (in order for users to access the new page if they knew the last active URL).

In other words, if I have, in the master file

a -> b

and in the toImport file

b -> c

I want to be able to do the following in the master file

a -> c

b -> c

What would be the best approach for this? VBA I suppose, or pivot table? Can someone gives me some ideas about how to address that. Knowing that 20k rows looped 400 times can be long, their must be a quick way to work this but I have no clue.

Thanks for your help.

7
  • How often do you need to do this? If you need to repeat this daily, than yeah, perhaps VBA. Otherwise, I wouldn't recommend it. I'd recommend just using some lookups, (ie MATCH) or such and verify things, then import them.
    – Ditto
    Mar 3, 2015 at 16:20
  • I always recommend pivot tables and/or formulas over VBA when possible
    – chancea
    Mar 3, 2015 at 16:34
  • @Ditto: Thanks, I don't have to run this often (just on big projects that comes about three times a year.) The rest of the time I only have to had 2-3 rows at a time and the "find" is much easier than this bulk task. Why not recommanding VBA, Mar 3, 2015 at 16:59
  • With my given example, would it be too much to ask for a real formula to test what I want. I really don't know how to do this either in pivot tables or formula and I would be thankfull to understand either fashion. Mar 3, 2015 at 17:01
  • 1
    VBA is more complex to write and maintain than just formulas (or as chancea said: pivot tables). WIth simpler formulas, you could set those up and done. You also have a chance to view the results prior to importing the data. VBA you'd likely have it just dump the items in as well .. and if it messed up, you'd have some rolling back to do ;)
    – Ditto
    Mar 3, 2015 at 17:11

2 Answers 2

1

Assuming that your Old URL and New URL are in columns A and B of the two sheets, you could do the following procedure:

  1. Sort master and toImport, each by column A.

  2. Add the following formula to column C of master:

    = IF(ISNA(VLOOKUP(A1, toImport!A:A, 1, FALSE)), IF(ISNA(VLOOKUP(B1, toImport!A:A, 1, FALSE)), B1, VLOOKUP(B1, toImport!A:B, 2, FALSE)), VLOOKUP(A1, toImport!A:B, 2, FALSE))

    This will give you the New URL from toImport for rows whose Old URL or New URL in master matches the Old URL in toImport and the New URL from master for rows that don't.

  3. Copy (or cut) Column C of master and Paste Values onto Column B.

    Now, you've handled the updates for everything on toImport that updates an existing URL on master.

  4. Add the following to columns C and D of toImport:

    = IF(ISNA(VLOOKUP(A1, master!A:A, 1, FALSE)), A1, "")
    = IF(ISNA(VLOOKUP(A1, master!A:A, 1, FALSE)), B1, "")

    This will give you nonblank values only when toImport has a new value for Old URL.

  5. Filter toImport for rows in which column C is not blank.

  6. Copy the block of nonblank cells in columns C and D of toImport, and Paste Values to the bottom of columns A and B of master.

You could automate this process by recording a macro of a manual performance of the steps above. It shouldn't take too much to make this macro work in general.

11
  • Thanks Isaac, I'll certainly test that. It seems to be the a really good and detailed answer. Let me get back to you afterward. Mar 3, 2015 at 17:04
  • 1
    Hello, I tested it and it doesn't seem to work as expected. Let say in master I have "def -> efg" and in toImport "efg -> yxw". I want the master line to be changed for "def -> yxw" and this doesn't seem to give me that opportunity. Mar 3, 2015 at 20:52
  • 1
    @EricThibeault, to describe issues particular to a single answer, the best thing to do is comment on that answer. You can link to screenshots in a comment, if you want. To add details to the question, you can edit the question. Mar 3, 2015 at 21:01
  • 1
    @EricThibeault, I updated Step 2 so that it will check both columns in master. Hopefully, this'll do the trick. Mar 3, 2015 at 21:10
  • 1
    @EricThibeault, I split out consideration of master!New into a nested IF. Mar 3, 2015 at 22:29
0

I would get both sets of data in VBA variant arrays, then loop to do the tests then put the arrays back. That would probably be fast enough. If its not fast enough then you could sort the data and use binary search.

The key trick is to get the entire range into an array using a single statement:

Dim vMaster as variant
vMaster=Worksheets("Master").Range("a1:B20400")

After updating the array you can write it back by reversing the assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.