Home > database >  VBA Compile error: syntax error in code- How can i fix this?
VBA Compile error: syntax error in code- How can i fix this?

Time:02-26

I keep getting "Compile error: syntax error" when I try to run this code in VBA:

Sub Quantity_sold()
    
    Dim sales As Range
    Dim sold As Double
    Dim lastrow As Integer, lastcol As Long
    Dim i As Integer, n As Integer
    Dim count As Integer
   
    lastrow = Cells(Rows.count, 1).End(xlUp).Row
    lastcol = Cells(1, Columns.count).End(xlToLeft).Column

    For i = 1 To lastcol
        For n = 1 To lastrow
            If Cells(n, i).Value >= sold Then
                count = count   1
            End If
        Next n
        
    ' ### Error occurs in the following line ###
    If MsgBox("Sales Were above, etc... (This will take a moment" ).", vbOKCancel) = vbCancel Then
        count = count   1
        MsgBox "You pressed cancel"
        Exit Sub
    End If
    Next i
        
End Sub

CodePudding user response:

You just need to remove the " after moment and it works OK.

Sub Quantity_sold()
    
    Dim sales As Range
    Dim sold As Double
    Dim lastrow As Integer, lastcol As Long
    Dim i As Integer, n As Integer
    Dim count As Integer
   
    lastrow = Cells(Rows.count, 1).End(xlUp).Row
    lastcol = Cells(1, Columns.count).End(xlToLeft).Column

    For i = 1 To lastcol
        For n = 1 To lastrow
            If Cells(n, i).Value >= sold Then
                count = count   1
            End If
        Next n
        
    ' ### Error occurs in the following line ### when there was a " after moment
    If MsgBox("Sales Were above, etc... (This will take a moment ).", vbOKCancel) = vbCancel Then
        count = count   1
        MsgBox "You pressed cancel"
        Exit Sub
    End If
    Next i
        
End Sub

CodePudding user response:

Looks like " is causing the error. look at this line (This will take a moment" )

  •  Tags:  
  • vba
  • Related