+ Reply to Thread
Results 1 to 10 of 10

VBA loop with variables

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    VBA loop with variables

    Hi all,

    I have to run number od macros in which only 1 word is changing. The macros are relatively long. In order to make the code shorter, and easier to maintain (since there will be change to some of the phrases in the future) I like to make a loop which will go through the phrases and replace them in the code.

    To each letter from "a" to "h" is dedicated phrase. The code should run loop from a to h and in message box to show the dedicated phrase.

    Below are simplified codes for what I like to achieve.

    Below is code which shows how the finished code should behave.

    Please Login or Register  to view this content.


    This is my attempt above code to make with loop. It goes through the letters but is not displaying the phrases, rather it displays the letters.

    Please Login or Register  to view this content.
    can anyone help me with this.

    Thank you in advance for your help.

    Igor

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop with variables

    maybe something like this
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA loop with variables

    FWIW, the array will start at 0 not 1. So you will miss out on "Country:". Usually the way to ensure this doesn't happen is to specify LBound(var) to UBound(var)
    If you are happy with my response please click the * in the lower left of my post.

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: VBA loop with variables

    Quote Originally Posted by stnkynts View Post
    the array will start at 0 not 1
    oops, of course
    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    863

    Re: VBA loop with variables

    I don't know how to do this other than with the use of the collections:
    Please Login or Register  to view this content.
    give it a try, tell me what you think!
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: VBA loop with variables

    Instead of using:

    var = Array("Country:", "Site:", "Requester:", "E-mail:", "Telephone:", "Cost center", "Project number", "Run hours")

    I like this better:

    VarStr = "Country:,Site:,Requester:,E-mail:,Telephone:,Cost center,Project number,Run hours"
    Var = Split(VarStr,",")
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  7. #7
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    863

    Re: VBA loop with variables

    I'm sorry but from my understanding (language barrier) is that the OP wants to use strings as variables...
    The OP wishes to use "a", "b" etc. (from the Chr(asc("a")) as already initiated variables a, b...
    something like MsgBox "a" should result Country:

  8. #8
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: VBA loop with variables

    hi nilem, stnkynts and MarvinP
    your solution works perfectly.
    thank you a lot.
    Igor

  9. #9
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    863

    Re: VBA loop with variables

    oh well... nevermind...

  10. #10
    Forum Contributor
    Join Date
    01-18-2013
    Location
    Prague
    MS-Off Ver
    Excel 2013
    Posts
    161

    Re: VBA loop with variables

    scuze bulina.
    Am ai uitat

+ 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. Loops with saved variables for each loop to be compared after loop is finished?
    By flabb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2016, 07:48 AM
  2. Loop with variables
    By djmiller in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-22-2015, 08:05 AM
  3. Loop Variables
    By cocacrave in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-18-2015, 08:10 AM
  4. [SOLVED] Multiple Variables in For Each Loop
    By Excel Guy 123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-16-2014, 11:47 PM
  5. [SOLVED] loop, do while, with variables - help
    By fredderf81 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-21-2014, 03:56 PM
  6. Incrementing variables in a Loop
    By sungsam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2011, 11:32 AM
  7. [SOLVED] Is it possible to create variables in a loop..
    By shishi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-03-2006, 03:25 PM

Tags for this Thread

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