Comparing variables in VBA

mikeymay

Well-known Member
Joined
Jan 17, 2006
Messages
1,632
Office Version
  1. 365
Platform
  1. Windows
I am comparing a db table field value from a record to a cell value
VBA Code:
If .Fields(strDbField) = Trim(Range(strRangeName)) Then

strDbField is the field name in the table
strRangeName is the cell containing the value to compare to

The Field value at the point of the comparison taking place is FALSE
The Cell value at the point of the comparison taking place is FALSE

Yet the check returns FALSE

Using the Immediates window I have testing each of the variables -
Debug.Print .Fields(strDbField) = Trim(Range(strRangeName))
False

Debug.Print .Fields(strDbField)
False

Debug.Print Trim(Range(strRangeName))
False

Unsure as to what is happening but in my world False = False = True!!!
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
in my world too, but trim(...) is string and the other boolean perhaps ?
what do you get with debug.print vartype(.fields(...)) and vartype(trim(...)))
 
Upvote 0
To get this to work I've assigned the Db and cell values to strings and compared those and it works as needed.
VBA Code:
         If IsNull(.Fields(strDbField)) Then
            strDbValue = ""
            Else
            strDbValue = .Fields(strDbField)
         End If
         
         strCellValue = Trim(Range(strRangeName))


Like you said, it's probably because I'm trying to compare different variables types.
 
Upvote 0
This might work for you:
(you only need the Trim if you it is likely you will have spaces in the cell that need to be removed)

VBA Code:
Debug.Print CBool(.Fields(strDbField)) = CBool(Trim(Range(strRangeName)))
 
Upvote 0
This might work for you:
(you only need the Trim if you it is likely you will have spaces in the cell that need to be removed)

VBA Code:
Debug.Print CBool(.Fields(strDbField)) = CBool(Trim(Range(strRangeName)))
Thanks, will give this a go.
Definitely need to trim so fingers crossed this works.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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