type mismatch in do loop

spinkung

Registered User.
Local time
Today, 14:42
Joined
Dec 4, 2006
Messages
267
Hi All,

I'm trying to loop through an excel range of dates from access vba. I want the loop to end when the date in my range matches the the current year and month. Problem is that not every cell is in date format so i could have...

01/01/09, 01/02/09, total 09, etc....

when it get to the total 09 cell the loop fails and says type mismatch. Is there a way to handle this??


Code:
Do
  
Loop Until Month(xlsht.cells(iRow, iColCount).Value) = Month(Now()) And _
                                Year(xlsht.cells(iRow, iColCount).Value) = Year(Now())


many thanks, spin.
 
try something like so:
Code:
Do
  if left(yoursell,5) = 'Total' then exit loop
  if Month(xlsht.cells(iRow, iColCount).Value) = Month(Now()) And _
                                Year(xlsht.cells(iRow, iColCount).Value) = Year(Now()) then exit loop
Loop

If you just want to continue the loop, use an if .. .then else structure like so:
Code:
Do
  if left(yoursell,5) = 'Total' then 
  else
    if Month(xlsht.cells(iRow, iColCount).Value) = Month(Now()) And _
                                  Year(xlsht.cells(iRow, iColCount).Value) = Year(Now()) then exit loop
  end if 
Loop
 
Thanks for your reply. It has worked a treat.

Code:
Do
If xlsht.cells(iRow, iColCount).Value Like "To*" Then
          iColCount = iColCount + 1
End If

Loop Until Month(xlsht.cells(iRow, iColCount).Value) + 1 = Month(Now()) + 1 And _
Year(xlsht.cells(iRow, iColCount).Value) = Year(Now())

spin :)
 

Users who are viewing this thread

Back
Top Bottom