ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code works 1st time then Type Mismatch when checking for not blank cells (https://www.excelbanter.com/excel-programming/290466-code-works-1st-time-then-type-mismatch-when-checking-not-blank-cells.html)

Colleyville Alan

Code works 1st time then Type Mismatch when checking for not blank cells
 
I have some VBA code writing info to a spreadsheet. At one point, I loop
from row 200 backwards looking for a row with text so that I can indicate
that this is the last row.
After I have written a bunch of text, this runs and everything is fine. But
then I add some more text and I get a "Type Mismatch" error.


For n = 200 To 1 Step -1
If oxlWks.Worksheets("Sheet1").Range("B" & n) Or
oxlWks.Worksheets("Sheet1").Range("A" & n) < "" Then
ilastrow = n
Exit For
End If
Next n

As I say, it goes thru fine but the next time I run it, it crashes on the
line that checks for a not empty condition. The text is bold but that is
the only difference between the type of text that was there the first time
through. How in the world do I get a "Type Mismatch"? I have looked at a
watch window and it shows just what I see on the spreadsheet. I know a
crude work-around, but this is bugging me.

Thanks

p.s. if there is a lastrow function in Excel, fine. But I still want to
know why this does not work.



mudraker[_145_]

Code works 1st time then Type Mismatch when checking for not blank cells
 
Why not do away with your loop and use

ilastrow = oxlWks.Worksheets("Sheet1").Range("A65536").End(xl Up).Row


this gets the last row with an entry in column a



Can not help you as to why your code fail

--
Message posted from http://www.ExcelForum.com


Colleyville Alan

Code works 1st time then Type Mismatch when checking for not blank cells
 
"mudraker " wrote in message
...
Why not do away with your loop and use

ilastrow = oxlWks.Worksheets("Sheet1").Range("A65536").End(xl Up).Row


this gets the last row with an entry in column a



Can not help you as to why your code fails


Thanks - that seems faster and more compact. The code crashing still bugs
me! But the goal is to get the thing working.

Thanks again.



acw[_2_]

Code works 1st time then Type Mismatch when checking for not blank cells
 
Ala

The code seems to be crashing a
Worksheets("Sheet1").Range("B" & n

As you are trying to obtain a boolean response the way the code is structured this item does not provide a boolean. The only boolean response is if column A is longer (or equal to) column B. If B is longer than A then that part of the code will evaluate to the contents of B rather than a boolean. Hence the crash

The code should be (using your structure
If Worksheets("Sheet1").Range("B" & n)< "" Or Worksheets("Sheet1").Range("A" & n) < "" The

Ton


----- Colleyville Alan wrote: ----

"mudraker " wrote in messag
..
Why not do away with your loop and us
ilastrow = oxlWks.Worksheets("Sheet1").Range("A65536").End(xl Up).Ro
this gets the last row with an entry in column
Can not help you as to why your code fail


Thanks - that seems faster and more compact. The code crashing still bug
me! But the goal is to get the thing working

Thanks again





All times are GMT +1. The time now is 11:37 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com