+ Reply to Thread
Results 1 to 6 of 6

Shortening VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-19-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    137

    Shortening VBA

    In my worksheet I have multiple values being replaced via VBA. Below is a piece of what I'm using. Out of curiosity can this be shortened?


    If Cells(i, "B") = "600017334" Then Cells(i, "B") = "16273441"
    If Cells(i, "B") = "600017340" Then Cells(i, "B") = "16273441"
    If Cells(i, "B") = "600016206" Then Cells(i, "B") = "16273441"
    If Cells(i, "B") = "600016252" Then Cells(i, "B") = "16273441"
    If Cells(i, "B") = "600017302" Then Cells(i, "B") = "16273441"

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Shortening VBA

    You could use Select Case.
    Select Case Cells(i, "B").Value
        Case "600017334", "600017340", "600016206", "600016252", "600017302" 
            Cells(i, "B") = "16273441"
    End Select
    Note, more cases could be added to this if required.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    11-19-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    137

    Re: Shortening VBA

    Thank! but if I were to continue this with another value is this what it would look like??

    Select Case Cells(i, "B").Value
    Case "600017334", "600017340", "600016206", "600016252", "600017302"
    Cells(i, "B") = "16273441"
    Case "600017334", "1234", "12345", "123456", "1234567"
    Cells(i, "B") = "123456789"

    End Select

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Shortening VBA

    Assuming that you don't have something like "600015000" in Column B range.

    You can do something like...

    If Cells(i, "B") Like "60001????" Then Cells(I, "B") = "16273441"
    "?" is used as wild card character to represent single character. Depending on your data, you can replace with single "*".

    But without knowing range of values that can occur in Column B (and other criteria if any), it would be difficult to give you exact answer.
    Last edited by CK76; 12-06-2016 at 02:24 PM. Reason: Grammer

  5. #5
    Forum Contributor
    Join Date
    11-19-2014
    Location
    United States
    MS-Off Ver
    2010
    Posts
    137

    Re: Shortening VBA

    Apologies I missed that value. This is what I meant to ask if I was to continue your example with another set of values I'd like to change

    Select Case Cells(i, "B").Value
    Case "600017334", "600017340", "600016206", "600016252", "600017302"
    Cells(i, "B") = "16273441"
    Case "123", "1234", "12345", "123456", "1234567"
    Cells(i, "B") = "123456789"

    End Select

  6. #6
    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: Shortening VBA

    yuenk,

    Please take a few minutes to re-read the forum rules, and then edit all of your posts to add CODE tags.

    Thanks.
    Entia non sunt multiplicanda sine necessitate

+ 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] Help with macro shortening
    By M4RSH in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-12-2014, 07:07 AM
  2. Formula Shortening
    By KlawdStrife in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-14-2014, 11:55 AM
  3. Shortening values with ...
    By nickmadd in forum Excel General
    Replies: 1
    Last Post: 03-20-2014, 02:21 PM
  4. Help with shortening code
    By CJ-22 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2008, 05:27 PM
  5. Shortening text with MID
    By theblade24 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-02-2008, 11:59 AM
  6. Shortening up Code
    By Kr4ftw3rk in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2008, 11:38 AM
  7. shortening a URL
    By Mikeda2 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 06-06-2007, 09:36 PM

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