Forum Discussion

Excel's avatar
Excel
Iron Contributor
Aug 02, 2022
Solved

Query relates to VBA code

Hello Everyone, 

 

I have tried VBA code with the help of IF statement with Range. 

But it shows error. 

What should i write ?

 

Please Help

 

Here is a attached file

  • Excel 

    Public Sub FunWithLogic()
    
      Dim rg As Range
      Dim cell As Range
      
      Set rg = Range("B2:B11")
        
        For Each cell In rg
        
        If cell.Value >= 90 Then
        
        
            MsgBox ("User is 90 or older")
            
        ElseIf cell.Value >= 20 Then
        
            MsgBox ("User is UnderAge")
            
        Else
        
            MsgBox ("Not Allowed!")
            
        End If
        
        Next cell
        
    End Sub

    Maybe with this code.

3 Replies

  • Excel 

    Public Sub FunWithLogic()
    
      Dim rg As Range
      Dim cell As Range
      
      Set rg = Range("B2:B11")
        
        For Each cell In rg
        
        If cell.Value >= 90 Then
        
        
            MsgBox ("User is 90 or older")
            
        ElseIf cell.Value >= 20 Then
        
            MsgBox ("User is UnderAge")
            
        Else
        
            MsgBox ("Not Allowed!")
            
        End If
        
        Next cell
        
    End Sub

    Maybe with this code.

Resources