+ Reply to Thread
Results 1 to 4 of 4

If statement not pulling values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-31-2014
    Location
    England
    MS-Off Ver
    Excel 2011 for mac
    Posts
    13

    If statement not pulling values

    I have 3 sheets in my excel document, one sheet is a pre-defined set of dates, on is a control page with selectable drop downs and buttons, and the third sheet shows the output of what im trying to do. On the control sheet you can select a month from a cell with a validate list, depending on what month you choose depends on which cell is selected on my date sheet. When i debug the script i get that my CurrentMonth string is returning as 0, rather than the string defined as below. Have i missed something blatantly obvious? I hope this makes some sort of sence!

    Dim Month As String
    Month = Sheets("Home").Range("B3").Value
    
    Dim CurrentMonth As String
    
    If Month = January Then
    CurrentMonth = "B2"
    
    ElseIf Month = February Then
    CurrentMonth = "C2"
    
    ElseIf Month = March Then
    CurrentMonth = "D2"
    
    ElseIf Month = April Then
    CurrentMonth = "E2"
    
    End If

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: If statement not pulling values

    Hi, if you do not put the string in a quote it will be consider it a variable. As you have not defined January/February etc the variable values will be 0.

    Try:
    Dim Month As String
    Month = Sheets("Home").Range("B3").Value
    
    Dim CurrentMonth As String
    
    If Month = "January" Then
    CurrentMonth = "B2"
    
    ElseIf Month = "February" Then
    CurrentMonth = "C2"
    
    ElseIf Month = "March" Then
    CurrentMonth = "D2"
    
    ElseIf Month = "April" Then
    CurrentMonth = "E2"
    
    End If
    Note - I would recommend writing code with "Option Explicit" -this will pick up on issues like this.

  3. #3
    Registered User
    Join Date
    03-31-2014
    Location
    England
    MS-Off Ver
    Excel 2011 for mac
    Posts
    13

    Re: If statement not pulling values

    Quote Originally Posted by ragulduy View Post
    Hi, if you do not put the string in a quote it will be consider it a variable. As you have not defined January/February etc the variable values will be 0.

    Try:
    Dim Month As String
    Month = Sheets("Home").Range("B3").Value
    
    Dim CurrentMonth As String
    
    If Month = "January" Then
    CurrentMonth = "B2"
    
    ElseIf Month = "February" Then
    CurrentMonth = "C2"
    
    ElseIf Month = "March" Then
    CurrentMonth = "D2"
    
    ElseIf Month = "April" Then
    CurrentMonth = "E2"
    
    End If
    Note - I would recommend writing code with "Option Explicit" -this will pick up on issues like this.
    Cheers man, still in a steep learning curve with VBA! Great help though so thanks!

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: If statement not pulling values

    Maybe?

    Sub timwilks13()
    Dim Month As String
    Month = Sheets("Home").Range("B3").Value
    
    Dim CurrentMonth As String
    
    If Month = "January" Then
    CurrentMonth = Range("B2").Value
    
    ElseIf Month = "February" Then
    CurrentMonth = Range("C2").Value
    
    ElseIf Month = "March" Then
    CurrentMonth = Range("D2").Value
    
    ElseIf Month = "April" Then
    CurrentMonth = Range("E2").Value
    
    End If
    End Sub

+ 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. Replies: 2
    Last Post: 09-26-2012, 09:48 AM
  2. Pulling in values from array; always zero or one.
    By ath1337 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-04-2012, 12:11 PM
  3. Replies: 2
    Last Post: 10-21-2011, 01:42 PM
  4. pulling 2 values for sum product
    By jenmarie in forum Excel General
    Replies: 2
    Last Post: 02-04-2010, 04:49 AM
  5. Pulling values from sheet that changes
    By jrhertel in forum Excel General
    Replies: 6
    Last Post: 03-11-2007, 01:08 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