Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Please solve this VBA mystery!

Hello wise folks,

With WinXP Pro SP2 and Office 2003 SP1, I have written following code
that works (watch out for line wraps):

Option Explicit
Option Base 1

Sub Copy_Values_To_Billing_Sheet()

Dim firstCell As Range
Dim lastCell As Range
Dim i As Long
Dim myCounter As Long
Dim myCopyArray As Variant

myCounter = 3
i = 0

Application.ScreenUpdating = False

With Sheets("Client")
Set firstCell = Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Set lastCell = Range("C3")
For i = firstCell.Row To lastCell.Row Step -1
If Cells(i, firstCell.Column).Interior.ColorIndex = 15 Then
myCounter = myCounter + 2
With Cells(i, firstCell.Column)
myCopyArray = Array(.Offset(-1, 0), .Offset(-1, -1),
..Offset(0, 4), .Offset(0, 6))
End With
End If
Debug.Print myCopyArray(1), myCopyArray(2), myCopyArray(3),
myCopyArray(4)
Sheets("monthly billing").Cells(5, myCounter) = myCopyArray
(1)
Sheets("monthly billing").Cells(6, myCounter) = myCopyArray
(2)
Sheets("monthly billing").Cells(14, myCounter + 1) =
myCopyArray(3)
Sheets("monthly billing").Cells(27, myCounter) = myCopyArray
(4)
Next i
End With

Set firstCell = Nothing
Set lastCell = Nothing
Set myCopyArray = Nothing

Application.ScreenUpdating = True

End Sub


The code above works perfectly - when it does work, that is (and that is
the mystery). What happens is that suddenly, for no apparent reason, when
I try to simply re-run my code, it will throw up a Runtime 13 error
('Type mismatch') with the offending line in yellow being "Sheets
("monthly billing").Cells(5, myCounter) = myCopyArray(1)" above.

Through trial, I have found that if I now go and activate Sheets
("Client") and then simply click once anywhere in this sheet to *change*
the existing activecell on this sheet, and then go back to re-run my
code, it runs perfectly once again!! This solution has worked every
single time when this 'mystery' error occurs.

FYI, I have cleaned up the code detritus using the excellent Code Cleaner
add-in, but the fact is that even after doing this, this mystery error
still occurs suddenly for no apparent reason. Can anyone throw some light
on this behaviour please?

Thanks,
Amit

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,588
Default Please solve this VBA mystery!

Try fully qualifying your sheet references with the workbook.

Eg.

With Activeworkbook.Sheets

instead of

With Sheets

--
Tim Williams
Palo Alto, CA


"Amit Shanker" wrote in message ...
Hello wise folks,

With WinXP Pro SP2 and Office 2003 SP1, I have written following code
that works (watch out for line wraps):

Option Explicit
Option Base 1

Sub Copy_Values_To_Billing_Sheet()

Dim firstCell As Range
Dim lastCell As Range
Dim i As Long
Dim myCounter As Long
Dim myCopyArray As Variant

myCounter = 3
i = 0

Application.ScreenUpdating = False

With Sheets("Client")
Set firstCell = Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Set lastCell = Range("C3")
For i = firstCell.Row To lastCell.Row Step -1
If Cells(i, firstCell.Column).Interior.ColorIndex = 15 Then
myCounter = myCounter + 2
With Cells(i, firstCell.Column)
myCopyArray = Array(.Offset(-1, 0), .Offset(-1, -1),
.Offset(0, 4), .Offset(0, 6))
End With
End If
Debug.Print myCopyArray(1), myCopyArray(2), myCopyArray(3),
myCopyArray(4)
Sheets("monthly billing").Cells(5, myCounter) = myCopyArray
(1)
Sheets("monthly billing").Cells(6, myCounter) = myCopyArray
(2)
Sheets("monthly billing").Cells(14, myCounter + 1) =
myCopyArray(3)
Sheets("monthly billing").Cells(27, myCounter) = myCopyArray
(4)
Next i
End With

Set firstCell = Nothing
Set lastCell = Nothing
Set myCopyArray = Nothing

Application.ScreenUpdating = True

End Sub


The code above works perfectly - when it does work, that is (and that is
the mystery). What happens is that suddenly, for no apparent reason, when
I try to simply re-run my code, it will throw up a Runtime 13 error
('Type mismatch') with the offending line in yellow being "Sheets
("monthly billing").Cells(5, myCounter) = myCopyArray(1)" above.

Through trial, I have found that if I now go and activate Sheets
("Client") and then simply click once anywhere in this sheet to *change*
the existing activecell on this sheet, and then go back to re-run my
code, it runs perfectly once again!! This solution has worked every
single time when this 'mystery' error occurs.

FYI, I have cleaned up the code detritus using the excellent Code Cleaner
add-in, but the fact is that even after doing this, this mystery error
still occurs suddenly for no apparent reason. Can anyone throw some light
on this behaviour please?

Thanks,
Amit



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 770
Default Please solve this VBA mystery!

Amit,

To add to Tim, also qualify the references within your With statement, e.g,:

Set lastCell = .Range("C3") 'period before "Range"

hth,

Doug

"Tim Williams" <timjwilliams at gmail dot com wrote in message
...
Try fully qualifying your sheet references with the workbook.

Eg.

With Activeworkbook.Sheets

instead of

With Sheets

--
Tim Williams
Palo Alto, CA


"Amit Shanker" wrote in message
...
Hello wise folks,

With WinXP Pro SP2 and Office 2003 SP1, I have written following code
that works (watch out for line wraps):

Option Explicit
Option Base 1

Sub Copy_Values_To_Billing_Sheet()

Dim firstCell As Range
Dim lastCell As Range
Dim i As Long
Dim myCounter As Long
Dim myCopyArray As Variant

myCounter = 3
i = 0

Application.ScreenUpdating = False

With Sheets("Client")
Set firstCell = Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Set lastCell = Range("C3")
For i = firstCell.Row To lastCell.Row Step -1
If Cells(i, firstCell.Column).Interior.ColorIndex = 15 Then
myCounter = myCounter + 2
With Cells(i, firstCell.Column)
myCopyArray = Array(.Offset(-1, 0), .Offset(-1, -1),
.Offset(0, 4), .Offset(0, 6))
End With
End If
Debug.Print myCopyArray(1), myCopyArray(2), myCopyArray(3),
myCopyArray(4)
Sheets("monthly billing").Cells(5, myCounter) = myCopyArray
(1)
Sheets("monthly billing").Cells(6, myCounter) = myCopyArray
(2)
Sheets("monthly billing").Cells(14, myCounter + 1) =
myCopyArray(3)
Sheets("monthly billing").Cells(27, myCounter) = myCopyArray
(4)
Next i
End With

Set firstCell = Nothing
Set lastCell = Nothing
Set myCopyArray = Nothing

Application.ScreenUpdating = True

End Sub


The code above works perfectly - when it does work, that is (and that is
the mystery). What happens is that suddenly, for no apparent reason, when
I try to simply re-run my code, it will throw up a Runtime 13 error
('Type mismatch') with the offending line in yellow being "Sheets
("monthly billing").Cells(5, myCounter) = myCopyArray(1)" above.

Through trial, I have found that if I now go and activate Sheets
("Client") and then simply click once anywhere in this sheet to *change*
the existing activecell on this sheet, and then go back to re-run my
code, it runs perfectly once again!! This solution has worked every
single time when this 'mystery' error occurs.

FYI, I have cleaned up the code detritus using the excellent Code Cleaner
add-in, but the fact is that even after doing this, this mystery error
still occurs suddenly for no apparent reason. Can anyone throw some light
on this behaviour please?

Thanks,
Amit





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Please solve this VBA mystery!

Amit,
I think you are going to have your best luck by moving "End If"
from above the Debug statement
to just before "Next i"
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Amit Shanker" wrote in message
Hello wise folks,
With WinXP Pro SP2 and Office 2003 SP1, I have written following code
that works (watch out for line wraps):

Option Explicit
Option Base 1

Sub Copy_Values_To_Billing_Sheet()

Dim firstCell As Range
Dim lastCell As Range
Dim i As Long
Dim myCounter As Long
Dim myCopyArray As Variant

myCounter = 3
i = 0

Application.ScreenUpdating = False

With Sheets("Client")
Set firstCell = Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
Set lastCell = Range("C3")
For i = firstCell.Row To lastCell.Row Step -1
If Cells(i, firstCell.Column).Interior.ColorIndex = 15 Then
myCounter = myCounter + 2
With Cells(i, firstCell.Column)
myCopyArray = Array(.Offset(-1, 0), .Offset(-1, -1), .Offset(0, 4), .Offset(0, 6))
End With
End If
Debug.Print myCopyArray(1), myCopyArray(2), myCopyArray(3),myCopyArray4)
Sheets("monthly billing").Cells(5, myCounter) = myCopyArray(1)
Sheets("monthly billing").Cells(6, myCounter) = myCopyArray(2)
Sheets("monthly billing").Cells(14, myCounter + 1) = myCopyArray(3)
Sheets("monthly billing").Cells(27, myCounter) = myCopyArray(4)
Next i
End With

Set firstCell = Nothing
Set lastCell = Nothing
Set myCopyArray = Nothing
Application.ScreenUpdating = True

End Sub

Thanks,
Amit

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Please solve this VBA mystery!

Thanks all for your suggestions.

Code is now working fine - no more mysterious errors. I think qualifying my
sheets fully and adding the 'dot' before the objects hit the target.

Regards,
Amit


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mystery object John in Wembley Excel Discussion (Misc queries) 5 August 27th 07 08:40 PM
MYSTERY!! WhytheQ Excel Discussion (Misc queries) 2 June 20th 06 11:00 AM
VLOOKUP Mystery RHubbard Excel Worksheet Functions 3 September 20th 05 06:43 PM
Can anyone solve this mystery? JK Excel Programming 2 November 21st 04 08:59 PM
Mystery Formats Tod Excel Programming 1 September 8th 04 04:13 AM


All times are GMT +1. The time now is 02:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"