VBA Cell Value

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to provide a sub where if it finds a specific text (for example "BookOne.xlsx") in Range("B:B") Then it will provide a MsgBox "Positive result" for all of its entries.

Likewise, if it does not find the text I'd like it to provide only one MsgBox "Negative result" pop-up and not a pop-up for every entry.



VBA Code:
Sub string_validation()

    Dim cel As Range

    For Each cel In Range("B:B")
        If cel.Value = "BookOne.xlsx" Then
            MsgBox "Positive result"
            
            Workbooks.Open cel.Offset(, 1)
              
        Else
            MsgBox "Negative result"
        End If
    Next cel

End Sub

Please lend a hand.

Thank you!
pinaceous
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Avoid using loops in that case. You can either use Application Match
Code:
Sub Test_Match()
    Const sTxt As String = "BookOne.xlsx", colToCheck As Long = 2
    Dim x
    With ActiveSheet
        x = Application.Match(sTxt, .Columns(colToCheck), 0)
        If Not IsError(x) Then
            MsgBox "Positive Result"
            Workbooks.Open .Cells(x, colToCheck + 1).Value
        Else
            MsgBox "Negative Result"
        End If
    End With
End Sub

Or you can use CountIf
Code:
Sub Test_CountIf()
    Const sTxt As String = "BookOne.xlsx", colToCheck As Long = 2
    Dim cnt As Long
    With ActiveSheet
        cnt = Application.WorksheetFunction.CountIf(.Columns(colToCheck), sTxt)
        If cnt > 0 Then
            MsgBox "Positive Result"
            Workbooks.Open .Cells(.Columns(colToCheck).Find(sTxt).Row, colToCheck + 1).Value
        Else
            MsgBox "Negative Result"
        End If
    End With
End Sub
 
Last edited:
Upvote 0
Hi pinaceois,

Here's my attempt:

VBA Code:
Option Explicit
Sub Macro1()

    Dim rngCell As Range
   
    Application.ScreenUpdating = False

    If Application.WorksheetFunction.CountIf(Range("B:B"), "BookOne.xlsx") = 0 Then
        MsgBox "Negative Result"
    Else
        For Each rngCell In Range("B1", Range("B" & Rows.Count).End(xlUp))
            If Len(rngCell) > 0 And Len(rngCell.Offset(0, 1) > 0) Then
                Workbooks.Open rngCell.Offset(0, 1)
            End If
        Next rngCell
        MsgBox "Positive Result"
    End If
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Good Day Trebor76,

Yes, I used a part of your sub and run it after the first sub determines if there is a positive result or not and it works well.

Thank you!
pinaceous
 
Upvote 0
Avoid using loops in that case. You can either use Application Match
Code:
Sub Test_Match()
    Const sTxt As String = "BookOne.xlsx", colToCheck As Long = 2
    Dim x
    With ActiveSheet
        x = Application.Match(sTxt, .Columns(colToCheck), 0)
        If Not IsError(x) Then
            MsgBox "Positive Result"
            Workbooks.Open .Cells(x, colToCheck + 1).Value
        Else
            MsgBox "Negative Result"
        End If
    End With
End Sub

Or you can use CountIf
Code:
Sub Test_CountIf()
    Const sTxt As String = "BookOne.xlsx", colToCheck As Long = 2
    Dim cnt As Long
    With ActiveSheet
        cnt = Application.WorksheetFunction.CountIf(.Columns(colToCheck), sTxt)
        If cnt > 0 Then
            MsgBox "Positive Result"
            Workbooks.Open .Cells(.Columns(colToCheck).Find(sTxt).Row, colToCheck + 1).Value
        Else
            MsgBox "Negative Result"
        End If
    End With
End Sub
Thank you

YasserKhalil !!!​

 
Upvote 0
Mine loops through all cells in Col. B and will open the workbook in Col. C if that helps.
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

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