Why does this logical test not work?

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
249
Office Version
  1. 365
Platform
  1. Windows
Here's the code:
VBA Code:
        With SHEET
            Last__Row = 0
            For Col = Col1 To Col2
                Temp = SHEET.Cells(.Rows.Count, Col).End(xlUp).Row + 0
               
                ' Test necessary if no data in Column "Col"
                If Temp > 0 Then
                    ' Actually Test bottom of Column.
                    Do While Trim(SHEET.Cells(Temp, Col)) = ""
                        Temp = Temp - 1
                    Loop
                End If
               
                Last__Row = Maximum(Last__Row, Temp)
            Next Col
        End With

This code finds the largest last row of the indicated columns..
If a column actually has no data in it, then TEMP calculates as 0.
However, the test in the "Do While ...." code doesn't work if "Temp" = 0.
So I need to test for "Temp > 0" to go around it to run that code.

My problem is the test "Temp > 0" is returning TRUE when "Temp" actually has a value of 0.
If I break on the test "Temp > 0" and continue, then the test does work. But that doesn't make sense.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I fixed it by rewriting the test line
VBA Code:
If Test Then

But that still doesn;t answer the question about why the code didn't work before.
 
Upvote 0
I fixed it by rewriting the code as
VBA Code:
If Test Then

But that still doesn't answer the question about why the code above wasn't working.
 
Upvote 0
And it's fixed.
I tried the "Temp > 0" test again, and now it works.

Sometimes Excel drives me crazy with stuff like this.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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