+ Reply to Thread
Results 1 to 4 of 4

Errors on If Statements

Hybrid View

  1. #1
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Errors on If Statements

    I'm trying to write code for henro8
    http://www.excelforum.com/showthread.php?t=651731

    and I get the following error with both nested if statements
    Compile error: End if without block if
    I tried with the Elseif and with separate if's but got the same error. What am I doing wrong?
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngCell As Range
    Dim DrpDown As String
    
    If Target.Column = 1 And Target.Row = 5 Then
        DrpDown = Target.Value
        Set RngCell = Cells(2, 5)
        
        If DrpDown = "$/square foot" Then
        With RngCell
            .NumberFormat = "$#,##0.00"
      
        ElseIf DrpDown = "%/construction cost" Then
        With RngCell
            .NumberFormat = "0.0%"
        End If
        
    End If
    End Sub
    Thanks in advance!

    ChemistB

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You're missing an "End With", but the With statements don't contribute anything here, Chemist.

    Maybe just
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) = "A5" Then
            Select Case Target.Value
                Case "$/square foot"
                    Range("E2").NumberFormat = "$#,##0.00"
                Case "%/construction cost"
                    Range("E2").NumberFormat = "0.0%"
            End Select
        End If
    End Sub
    Last edited by shg; 08-04-2008 at 01:30 PM.

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    Try:

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim RngCell As Range
    Dim DrpDown As String
    
    If Target.Column = 1 And Target.Row = 5 Then
        DrpDown = Target.Value
        Set RngCell = Cells(2, 5)
    End If
        
        If DrpDown = "$/square foot" Then
        With RngCell
            .NumberFormat = "$#,##0.00"
      
        ElseIf DrpDown = "%/construction cost" Then
        With RngCell
            .NumberFormat = "0.0%"
        End If
        
    End Sub
    EDIT: You also didn't "End With" in either case.

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582
    Thanks to both of you! I knew about End With's, just had a duh moment. I like your more concise code, shg.

    ChemistB
    back to the drawing board.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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