+ Reply to Thread
Results 1 to 12 of 12

Need help with a workseet_change event macro

  1. #1
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Need help with a workseet_change event macro

    Hi Folks!

    Not much of a programmer.

    I need some help on a worksheet_change event macro.

    I want to type in the first few letters of a NFL city name and when I hit enter the macro will replace what I've typed with the full city name.

    For example, if I type the letters pit then the macro will replace that with Pittsburgh. If I type dal then the macro will replace that with Dallas. If I type ne then the macro will replace that with New England.

    I am aware that this could be done using AutoCorrect settings but that is a global change (even across Office applications) and I only want this to be a local change in a specific file on a specific sheet in a specific range.

    I even tried this using drop down lists but that is a very tedious task and I think what I want to try with the macro will be much faster.

    I need to do this for all 32 NFL cities but if you show me how to do the first few I should be able to add the others.

    The cell range will be A4:C305

    Here's the first few names...

    ari = Arizona
    atl = Atlanta
    bal = Baltimore
    buf = Buffalo

    Appreciate any help you can offer.
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need help with a workseet_change event macro

    Add your cities to the array in alphabetical order.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Need help with a workseet_change event macro

    Another approach:
    Please Login or Register  to view this content.
    This uses a 2 column Named Range on another sheet. Abbreviations in Col 1, Full name in Col 2.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Need help with a workseet_change event macro

    It is always better to handle multiple input at a time.
    Please Login or Register  to view this content.

  5. #5
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need help with a workseet_change event macro

    Thanks to everyone for their efforts.

    I will try each suggestion and let you know how I make out.

  6. #6
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need help with a workseet_change event macro

    Tried each of the suggestions and they all do the job.

    Thanks to everyone for their help!



    Don't pick the Steelers to win the Super Bowl!

  7. #7
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need help with a workseet_change event macro

    Quote Originally Posted by AlphaFrog View Post
    Add your cities to the array in alphabetical order.

    arrCities = Array("Arizona", "Atlanta", "Baltimore", "Buffalo", _
    "Charlotte", "Chicago", "Cincinnati", "Cleveland")
    This worked so good that I regret not having asked for this years ago!

    This is a true time saver and I can see benefits of using it in more applications.

    Is there a limit to the number of items in the array? Can you make the array a named range on the worksheet? Would it be better to make the array a worksheet range of cells?

  8. #8
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need help with a workseet_change event macro

    Quote Originally Posted by Tony Valko View Post
    This worked so good that I regret not having asked for this years ago!

    This is a true time saver and I can see benefits of using it in more applications.

    Is there a limit to the number of items in the array? Can you make the array a named range on the worksheet? Would it be better to make the array a worksheet range of cells?
    The array limit is memory dependent. In this case, it's not a factor.

    You could read a range into an array (named or not).
    Dim arrMyArray as Variant
    arrMyArray = Range("B2:B100").Value

    The result array has the same two dimensions as the range. In this example; (1 to 99 by 1 to 1)

  9. #9
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need help with a workseet_change event macro

    So then this line of code:

    Please Login or Register  to view this content.
    Would become?

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Need help with a workseet_change event macro

    Sorry no.

    Something like this. Change the sheet name and range to suit.

    Please Login or Register  to view this content.

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Need help with a workseet_change event macro

    Tony,

    There's an add-in that will do this automatically for cells with list validation tied to a range.

    The add-in is at https://app.box.com/shared/ono8ni9xvr, there's a readme at
    https://app.box.com/shared/4qxybepxxr, and an example workbook at https://app.box.com/shared/e8dbbple34
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Need help with a workseet_change event macro

    Ok, I'll play with this over the weekend.

    Thanks once again!

+ 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] Calling a macro from an event macro. Using nest loops with if statements
    By c0d3crunch3r in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-12-2012, 08:31 AM
  2. Replies: 4
    Last Post: 11-07-2012, 04:02 PM
  3. Difference between an event driven macro and a module macro
    By RamblinWreck in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-18-2007, 03:42 PM
  4. [SOLVED] On event macro
    By markvdh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2006, 07:40 AM
  5. Event Macro
    By Quaisne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2005, 07:55 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