VBA Code check

Alisya

Well-known Member
Joined
Nov 27, 2008
Messages
532
Hi does any one know why this could does not work. I am not getting any errors, it does nothing when i run. I am trying to get the code to look in Col B for N/A, if found then let the user know why msgbox

Code:
Sub Test()
    Dim LR1 As Long
    
    LR1 = Range("C" & Rows.count).End(xlUp).Row
    
    If LR1 < 5 Then LR1 = 5
    If UCase$(Trim$(Sheets(1).Range("B" & LR1).Value)) = "N/A" Then
            MsgBox "There is a new a/c"
    End If

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Your code is looking specifically in the last used row in column B, I think you're looking for:
Code:
Sub Test()
    Dim LR1 As Long
    Dim c As Range
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
    
    If LR1 < 5 Then LR1 = 5
    For Each c In Sheets(1).Range("B5:B" & LR1)
    If UCase(Trim(c.Value)) = "N/A" Then
            MsgBox "There is a new a/c"
    End If
    Next c

End Sub
 
Upvote 0
Hi, if i have multiple N/A i have to press OK multiple times, is there a way to only limit this to only once?

Your code is looking specifically in the last used row in column B, I think you're looking for:
Code:
Sub Test()
    Dim LR1 As Long
    Dim c As Range
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
 
    If LR1 < 5 Then LR1 = 5
    For Each c In Sheets(1).Range("B5:B" & LR1)
    If UCase(Trim(c.Value)) = "N/A" Then
            MsgBox "There is a new a/c"
    End If
    Next c
 
End Sub
 
Upvote 0
Anyone? How do i limit the msgbox to only pop up once rather than multiple times?
 
Upvote 0
Hello Alisya,

This will exit the loop when a match is made. The message will then appear only once.
Code:
Sub Test()
    Dim LR1 As Long
    Dim c As Range
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
 
    If LR1 < 5 Then LR1 = 5
    For Each c In Sheets(1).Range("B5:B" & LR1)
    If UCase(Trim(c.Value)) = "N/A" Then
            MsgBox "There is a new a/c"
            Exit For
    End If
    Next c
 
End Sub
Sincerely,
Leith Ross
 
Upvote 0
Try, untested
Code:
Sub Test2()
    Dim LR1 As Long, n As Integer
    Dim c As Range
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
    If LR1 < 5 Then LR1 = 5
    n = 0
    For Each c In Sheets(1).Range("B5:B" & LR1)
    If UCase(Trim(c.Value)) = "N/A" Then n = n + 1
    Next c
If n > 0 Then MsgBox "No of new a/c's is " & n
End Sub
 
Upvote 0
Ok that works. Now if i want to Exit Sub if an N/A is found then i tried doing the below but the code failed. What have i done wrong?

Code:
Sub test3()
Dim LR1 As Long, n As Integer
LR1 = Sheets("Securities").Range("C" & Rows.Count).End(xlUp).Row
    If UCase(Trim(Sheets("Securities").Range("B5:B" & LR1).Value)) = "N/A" Then
Exit Sub
End If
Else
'Continue with code
End Sub




Try, untested
Code:
Sub Test2()
    Dim LR1 As Long, n As Integer
    Dim c As Range
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
    If LR1 < 5 Then LR1 = 5
    n = 0
    For Each c In Sheets(1).Range("B5:B" & LR1)
    If UCase(Trim(c.Value)) = "N/A" Then n = n + 1
    Next c
If n > 0 Then MsgBox "No of new a/c's is " & n
End Sub
 
Upvote 0
This?
Code:
[COLOR="Blue"]Sub[/COLOR] Test()
    
    [COLOR="Blue"]Dim[/COLOR] LR1 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    
    LR1 = Range("C" & Rows.Count).End(xlUp).Row
    
    [COLOR="Blue"]If[/COLOR] LR1 < 5 [COLOR="Blue"]Then[/COLOR] LR1 = 5
    
    [COLOR="Blue"]If[/COLOR] IsError(Sheets(1).Range("B" & LR1)) [COLOR="Blue"]Then[/COLOR]
            MsgBox "There is a new a/c"
    [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]

[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,224,513
Messages
6,179,214
Members
452,895
Latest member
BILLING GUY

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top