+ Reply to Thread
Results 1 to 7 of 7

Custom Function to read and write

  1. #1
    Registered User
    Join Date
    05-20-2009
    Location
    Washington, DC
    MS-Off Ver
    Excel 2003
    Posts
    4

    Custom Function to read and write

    Hello,

    I'm very new to VBA and need some help.

    I'm trying to create a custom function that will read a value in one cell and then write a different value in an adjacent cell.

    More specifically, I have a list of 50 countries in one column, and I want to write a corresponding global region in the cell next to it. So if the if cell A1 is "USA" then the function would write "North America" in cell B1. Cell A2 might be "China" and the function would write "Asia" in cell B2.

    I think I need to use If and Elseif, but I don't know how to put it together. How can I do this? Thanks for the help.

  2. #2
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Custom Function to read and write

    ditzer252,

    Welcome to the Excel Forum.

    If you have more than 7 countries, you will not be able to use "If".

    Please post your workbook - scroll down and see "Manage Attachments".
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  3. #3
    Registered User
    Join Date
    05-20-2009
    Location
    Washington, DC
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Custom Function to read and write

    I won't post my workbook because it contains confidential data, sorry. Still, you can visualize a column with 50 unique countries running down. How can I place a regional label for each country in the next column? You said that I can't use if more than 7 times. I thought a custom function or user defined function was a way to working around the limitation of the nesting limit. Is there a different approach that I should take?

  4. #4
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Custom Function to read and write

    ditzer252,

    you can visualize a column with 50 unique countries running down. How can I place a regional label for each country in the next column?
    I need to see the list of the 50 unique coutries and their respective regional labels.

    You could use the VLOOKUP function with a list of 50 unique coutries and their respective regional labels.

    Please post a sample of your workbook (without confidential information) - scroll down and see "Manage Attachments".
    Last edited by stanleydgromjr; 05-20-2009 at 05:41 PM.

  5. #5
    Registered User
    Join Date
    05-20-2009
    Location
    Washington, DC
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Custom Function to read and write

    Stan,

    Thanks for your help! I'm attaching a sample of my workbook, very basic. I want to be able to populate column B with the following 9 regions:

    North America
    South America
    Central America
    Caribbean
    Europe
    Africa
    Asia
    Oceania
    Middle East

    What do you think? How can I use VLOOKUP to read what is in column A and then write the correct region in column B?
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Custom Function to read and write

    ditzer252,

    Solved with using VLOOKUP.

    See the attached workbook "countrylistsample(1).xls".

    I created in Sheet2 a unique list of Countires, by using Data, Filter, AdvancedFilter, Unique.

    After you manually complete/enter the Regions in Sheet2, in Sheet1 copy the formula in cell B2, down in column B, to the end of the data in column A.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-20-2009
    Location
    Washington, DC
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Custom Function to read and write

    Stan, you're the man! Thanks so much!

+ 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