+ Reply to Thread
Results 1 to 6 of 6

Looping through months VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    03-21-2016
    Location
    india
    MS-Off Ver
    2010
    Posts
    9

    Looping through months VBA code

    Hello,

    I am new here (and also for coding), please excuse me if i am not following the pattern.

    Issue: i am trying to write a code which fills the 'textbox' by picking data from worksheet(ex - Function - January, Function - February,..) and cell address constant through sheets (Q12)

    however, below thing isnt working and the user form doesnt show anything.
    Private Sub monthlist_Change()
    
    Dim myarray As Variant
    Dim x As Long
    
    myarray = Array(January, February, March, April, May, June, July, August, September, October, November, December)
    
    For x = LBound(myarray) To UBound(myarray) Step 1
    
    
    If Me.monthlist.Text = "x" Then Me.sdmteamcount.Value = Worksheets("Functions - &x").Range("q12").Value
    
    Next x
    End Sub

    Your help is greatly appreciated.
    Last edited by harin; 03-22-2016 at 11:07 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Looping through months VBA code

    Hi Harin,

    Welcome to the Forum!

    The month names need to be strings:

    myarray = Array("January"," February", etc
    BTW - Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Looping through months VBA code

    There is also an issue with your worksheet syntax. It should be

    Worksheets("Functions - " & myarray(x)).
    I'm interested in starting a career working with VBA, if anyone knows of any opportunities!

  4. #4
    Registered User
    Join Date
    03-21-2016
    Location
    india
    MS-Off Ver
    2010
    Posts
    9

    Re: Looping through months VBA code

    thank you so much for your response. i have made the changes as per your suggestion. However, now i am receiving 'subscript out of range' error.

    below is the current code

    Private Sub monthlist_Change()
    
    Dim myarray As Variant
    Dim x As Long
    
    myarray = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
    
    For x = LBound(myarray) To UBound(myarray)
    
    
    If Me.monthlist.Text = myarray(x) Then Me.sdmteamcount.Value = Worksheets("Functions -" & myarray(x)).Range("q12").Value
    
    
    Next x
    End Sub
    thanks again

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Looping through months VBA code

    Check this:

    "Functions -"
    your original post suggests there is a space after the dash, which would be:

    "Functions - "
    it is an important difference

  6. #6
    Registered User
    Join Date
    03-21-2016
    Location
    india
    MS-Off Ver
    2010
    Posts
    9

    Re: Looping through months VBA code

    Such a small yet important difference took 2 hours of time. Its working now!!!

    Thank you so much for your help..

+ 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. Code keeps looping through
    By mikey3580 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2014, 04:47 PM
  2. Help with looping of vba code
    By thaykhov in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-08-2014, 11:38 PM
  3. [SOLVED] Trying to change verbose looping code into more efficient (and shorter) code please
    By dawatcher in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-03-2013, 07:43 AM
  4. [SOLVED] Code for Looping a VBA
    By Jiptastic in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-13-2012, 08:38 PM
  5. Looping by weeks...by months
    By tmd2 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-08-2011, 10:45 PM
  6. looping code
    By Mayaleigh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-09-2009, 03:12 PM
  7. Looping Code
    By maxwell13 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-30-2007, 08:12 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