Consulting

Results 1 to 2 of 2

Thread: VBA dilemma - Passing Variable from Input box

  1. #1
    VBAX Newbie
    Joined
    Oct 2012
    Posts
    2
    Location

    VBA dilemma - Passing Variable from Input box

    Before a For Next loop I have an input box for the user to pick they year they would like info on:

    YearToInput = InputBox("Enter the first year you want to extract to a new sheet")

    YearToInput is Dim a Variant

    The loop is quite simple, but the value entered into the inputbox is not passed to inside the loop,
    am at a loss

    I can hard code in a year 2014, or 2015 in place of YearToInput and it works fine.

    For Each Cell In Wks.Range("D2" & TheLastRow)
    If Cell.Value = YearToInput Then

    r = Cell.Row

    Wks.Range("A" & r & ":" & "C" & r).Copy Worksheets("2014").Range("A" & x)

    x = x + 1

    End If

    Next Cell

    Thanks for any suggestions.
    Mort in Dallas

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Mort,

    The value from the InputBox is a Text String. You will need to convert it to a number to use it as the year.

    Dim YearToInput As Variant
    
    YearToInput = InputBox("Enter the first year you want to extract to a new sheet")
    
    If YearToInput = "" Then Exit Sub
    
    YearInput = CInt(YearToInput)
    
    For Each Cell In Wks.Range("D2" & TheLastRow)
    If Cell.Value = YearToInput Then
    
    r = Cell.Row
    
    Wks.Range("A" & r & ":" & "C" & r).Copy Worksheets("2014").Range("A" & x)
    
    x = x + 1
    
    End If
    
    Next Cell

    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •