+ Reply to Thread
Results 1 to 7 of 7

Create a macro for mapping

  1. #1
    Registered User
    Join Date
    01-14-2014
    Location
    Victoria, Canada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Create a macro for mapping

    Hi guys,

    I am very new to VBA and have a question about creating a macro to do mapping. For example if I have the following list in column A:

    Apple
    Volvo
    Hyundai
    Orange
    Grape
    Christmas
    Thanksgiving

    Now, I want to convert the items in column A based on the following mapping,
    Apple, Orange, Grape --> Fruit
    Volvo, Hyundai --> Car
    Christmas, Thanksgiving --> Holiday

    This is just a simple example, and I am hoping that I can get some help on the start so that I can expand on the macro.

    Thank you!
    Leon

  2. #2
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Create a macro for mapping

    Hi -

    Create a table for fruits,car,holiday,etc and search the list on that table then get the corresponding header name of the tables.

    event

  3. #3
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Create a macro for mapping

    buttnana, do you specifically want a macro solution? And are you sure you want to overwrite you existing data? This can easily be done, but you will, somewhere, have to create a mapping table. One way would be to use VLOOKUP. Let's say you have the list you indicated in column A above on sheet1 in range A1:A7. Now, on sheet2, in range A1:B7 you have your mapping, i.e. A1="Apple" and B1="Fruit", A2="Volvo" and B2="Car", and so forth for your entire list. If you wanted to use VLOOKUP, and don't need to overwrite your column A, you could put the following formula on sheet1 B1, then drag down...
    Please Login or Register  to view this content.
    Now, if you want a macro solution, you are still going to need the mapping table, either as a range on a sheet as I described above, or as a hard-coded case statement in your macro code. I would recommend using a range on a sheet, so you don't have to mess with your code every time you need to add items. Which ever method you choose, the macro would be easy if that's what you want/need. If so, let us know, and the code will be posted.

    Greg
    Just a guy trying to make work stuff easier.

  4. #4
    Registered User
    Join Date
    01-14-2014
    Location
    Victoria, Canada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Create a macro for mapping

    Hi Greg,

    Thank you in advance. I am looking to replace the existing data. And I think hard-coded would be my preference because the mapping is a standard map and I don't want the end user to accidentally alter the mapping table.

    Thanks!
    L

  5. #5
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Create a macro for mapping

    Ok, so this should do it for you. I will caveat the first sub below with the statement that you need to take care to ensure you type in your mapping values correctly. I took the liberty of also writting a sub to do the mapping the way I would do it, that is, by looking to a sheet range to get the mapping values, but you can decide which you think is better.

    First the sub where you hard-code the values in the sub...
    Please Login or Register  to view this content.
    And now, my choice, the one where you type your mapping values in a range as I described in my first post. This code assumes your list of initial items will be on sheet "List", and the lookup values will be in a range on sheet "Map".
    Please Login or Register  to view this content.
    Let us know if you have any questions.

    Greg

  6. #6
    Registered User
    Join Date
    01-14-2014
    Location
    Victoria, Canada
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: Create a macro for mapping

    Hi Greg,

    Thank you very much! I've tried both, and I agree that your suggest method is much more convenient. As I am new to VBA, is there an online tutorial or a good book that you would recommend to me?

    Thanks again!
    L

  7. #7
    Forum Contributor gsnidow's Avatar
    Join Date
    07-22-2010
    Location
    Richmond, VA
    MS-Off Ver
    Excel 2007
    Posts
    150

    Re: Create a macro for mapping

    Well, I'm afraid I can't recommend any books, because I am 100% self taught through googling, trial and error, and most importantly, following a good forum, and it looks like you found one. As a reference website, you won't find a more comprehensive one than Chip Pearson's, http://www.cpearson.com/Excel/MainPage.aspx I have never needed to do something with VBA for which he has not had a fully explained and commented example; you should definitely bookmark it. I would also say that if you want to get good, try to solve problems you see here, as they are often scenarios you would never encounter in your normal day. Anyhow, I'm glad my solutions worked for you, and I wish you the best in your quest for VBA improvement.

    Greg

+ 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. Mapping the TEXT, Using MACRO
    By amiable in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2013, 04:49 AM
  2. To create defect mapping in macro
    By nesh91 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-27-2013, 03:11 AM
  3. Macro for mapping
    By louisjeorge75 in forum Excel General
    Replies: 11
    Last Post: 12-16-2011, 02:03 PM
  4. Field lookup and mapping to create URL
    By RAuten in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-16-2010, 04:10 PM
  5. Replies: 0
    Last Post: 07-02-2006, 11:50 AM

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