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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,223,911
Messages
6,175,326
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