VBA Value of text

Giordano Bruno

Well-known Member
Joined
Jan 7, 2007
Messages
1,356
I have the following code line which I am using to distinguish between similar files.

If Workbooks(s).Sheets("Accounts").Range("A1").Value > 1 Then

A marker for the two file types is contained in a cell on the "Accounts" tab. This will be either the text "Password" or the value 2.

When the code find the correct workbook, it checks the contents of A1 and will find either "Password" or 2 depending upon which file it has found.

If it finds "Password", it should skip to the 'Else' code lines, but instead it runs the next line of code after 'Then'. Does this mean that the text string "Password" is greater than the value 2

Given that the values in the cells are either the word "Password" or the value 2, how should I distinguish between them if I cannot use > 2
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Code:
Sub G()
    With Workbooks(s).Sheets("Accounts").Range("A1")
        If .Value = "Password" Then
            ' Process
        ElseIf .Value = "2" Then
            ' Process
        End If
    End With
End Sub
 
Upvote 0
Check this:
Code:
Sub Check()
    MsgBox """Password"" > 1: " & ("Password" > "1") & vbNewLine & _
           "2 > 1: " & (2 > 1)
End Sub

P.S. When comparing "Password" and 1, I used 1 in quotes, 'cause this same thing VBA does. If you omit quotes, you'll get error.
 
Upvote 0
Thanks for that Sektor.

Unfortunately I've peppered my code with that test. Now it looks like I have to start again.
 
Upvote 0
I apologize, what does "peppered" mean? Abused? Just can't apply correct translation to this word. :(
 
Upvote 0
Unfortunately I've peppered my code with that test. Now it looks like I have to start again.

In that case, as you're faced with the task of revisiting every occurrence of that code segment anyway, take the opportunity of replacing them with a call to a function, then write a function to return TRUE or FALSE based on that same test.

That way you'll only have one place to edit the code if you need to tweak it in the future.
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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