PC Review


Reply
Thread Tools Rate Thread

this code crashes excel. How come?

 
 
=?Utf-8?B?Tm9sYXVnaG10cg==?=
Guest
Posts: n/a
 
      13th Sep 2007
Sub copy_1()
Dim SourceRange As Range, DestRange As Range
Dim DestSheet As Worksheet, Lr As Long

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

'fill in the Source Sheet and range
Set SourceRange = Sheets("WE 9-8-07").Range("F2:G63")

'Fill in the destination sheet and call the LastRow
'function to find the last row
Set DestSheet = Sheets("DIE STATUS")
Lr = DestSheet.Cells(Rows.count, "A").End(xlUp).Row

'With the information from the LastRow function we can
'create a destination cell and copy/paste the source range
Set DestRange = DestSheet.Range("A" & Lr + 1)
SourceRange.COPY DestRange

With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Worksheets("DIE STATUS").Activate
CELLCount = 2
With Worksheets("Die status")
Do While Cells(CELLCount, "A") <> ""

Number = Val(Cells(CELLCount, "A"))
Text = Cells(CELLCount, "A")
If InStr(Text, " ") > 0 Then
Text = Trim(Mid(Text, InStr(Text, " ")))
.Cells(CELLCount, "A") = Number
.Cells(CELLCount, "C") = Text
CELLCount = CELLCount + 1
End If
Loop
End With

End Sub

 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      13th Sep 2007
Does it actually crash or merely go into an endless loop, as I would expect
just at a glance at your code. Try changing

> CELLCount = CELLCount + 1
> End If
> Loop


to

End If
CELLCount = CELLCount + 1
Loop

In passing I'd suggest -
Head the module Option Explicit
declare all your variables
don't use variable names like Text & Number
If you are going to qualify .Cells to your sheet with 'With' might as well
qualify all the instances instead of only some. Might not matter in this
case as your 'With' sheet is the Activesheet, otherwise it would give
errors.

Regards,
Peter T

"Nolaughmtr" <(E-Mail Removed)> wrote in message
news:F68D2018-2BC5-4DCD-B885-(E-Mail Removed)...
> Sub copy_1()
> Dim SourceRange As Range, DestRange As Range
> Dim DestSheet As Worksheet, Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> 'fill in the Source Sheet and range
> Set SourceRange = Sheets("WE 9-8-07").Range("F2:G63")
>
> 'Fill in the destination sheet and call the LastRow
> 'function to find the last row
> Set DestSheet = Sheets("DIE STATUS")
> Lr = DestSheet.Cells(Rows.count, "A").End(xlUp).Row
>
> 'With the information from the LastRow function we can
> 'create a destination cell and copy/paste the source range
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.COPY DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
> Worksheets("DIE STATUS").Activate
> CELLCount = 2
> With Worksheets("Die status")
> Do While Cells(CELLCount, "A") <> ""
>
> Number = Val(Cells(CELLCount, "A"))
> Text = Cells(CELLCount, "A")
> If InStr(Text, " ") > 0 Then
> Text = Trim(Mid(Text, InStr(Text, " ")))
> .Cells(CELLCount, "A") = Number
> .Cells(CELLCount, "C") = Text
> CELLCount = CELLCount + 1
> End If
> Loop
> End With
>
> End Sub
>



 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      13th Sep 2007
Don't see anything inherent in the code that would cause this. Have you
stepped through it and seen where the crash occurs?

In article <F68D2018-2BC5-4DCD-B885-(E-Mail Removed)>,
Nolaughmtr <(E-Mail Removed)> wrote:

> Sub copy_1()
> Dim SourceRange As Range, DestRange As Range
> Dim DestSheet As Worksheet, Lr As Long
>
> With Application
> .ScreenUpdating = False
> .EnableEvents = False
> End With
>
> 'fill in the Source Sheet and range
> Set SourceRange = Sheets("WE 9-8-07").Range("F2:G63")
>
> 'Fill in the destination sheet and call the LastRow
> 'function to find the last row
> Set DestSheet = Sheets("DIE STATUS")
> Lr = DestSheet.Cells(Rows.count, "A").End(xlUp).Row
>
> 'With the information from the LastRow function we can
> 'create a destination cell and copy/paste the source range
> Set DestRange = DestSheet.Range("A" & Lr + 1)
> SourceRange.COPY DestRange
>
> With Application
> .ScreenUpdating = True
> .EnableEvents = True
> End With
> Worksheets("DIE STATUS").Activate
> CELLCount = 2
> With Worksheets("Die status")
> Do While Cells(CELLCount, "A") <> ""
>
> Number = Val(Cells(CELLCount, "A"))
> Text = Cells(CELLCount, "A")
> If InStr(Text, " ") > 0 Then
> Text = Trim(Mid(Text, InStr(Text, " ")))
> .Cells(CELLCount, "A") = Number
> .Cells(CELLCount, "C") = Text
> CELLCount = CELLCount + 1
> End If
> Loop
> End With
>
> End Sub

 
Reply With Quote
 
=?Utf-8?B?Tm9sYXVnaG10cg==?=
Guest
Posts: n/a
 
      13th Sep 2007
OMG ITS AMAZING!!!!!! Thank you so much!! my first real macro with a loop.
"CHEERS"!!!!

"Peter T" wrote:

> Does it actually crash or merely go into an endless loop, as I would expect
> just at a glance at your code. Try changing
>
> > CELLCount = CELLCount + 1
> > End If
> > Loop

>
> to
>
> End If
> CELLCount = CELLCount + 1
> Loop
>
> In passing I'd suggest -
> Head the module Option Explicit
> declare all your variables
> don't use variable names like Text & Number
> If you are going to qualify .Cells to your sheet with 'With' might as well
> qualify all the instances instead of only some. Might not matter in this
> case as your 'With' sheet is the Activesheet, otherwise it would give
> errors.
>
> Regards,
> Peter T
>
> "Nolaughmtr" <(E-Mail Removed)> wrote in message
> news:F68D2018-2BC5-4DCD-B885-(E-Mail Removed)...
> > Sub copy_1()
> > Dim SourceRange As Range, DestRange As Range
> > Dim DestSheet As Worksheet, Lr As Long
> >
> > With Application
> > .ScreenUpdating = False
> > .EnableEvents = False
> > End With
> >
> > 'fill in the Source Sheet and range
> > Set SourceRange = Sheets("WE 9-8-07").Range("F2:G63")
> >
> > 'Fill in the destination sheet and call the LastRow
> > 'function to find the last row
> > Set DestSheet = Sheets("DIE STATUS")
> > Lr = DestSheet.Cells(Rows.count, "A").End(xlUp).Row
> >
> > 'With the information from the LastRow function we can
> > 'create a destination cell and copy/paste the source range
> > Set DestRange = DestSheet.Range("A" & Lr + 1)
> > SourceRange.COPY DestRange
> >
> > With Application
> > .ScreenUpdating = True
> > .EnableEvents = True
> > End With
> > Worksheets("DIE STATUS").Activate
> > CELLCount = 2
> > With Worksheets("Die status")
> > Do While Cells(CELLCount, "A") <> ""
> >
> > Number = Val(Cells(CELLCount, "A"))
> > Text = Cells(CELLCount, "A")
> > If InStr(Text, " ") > 0 Then
> > Text = Trim(Mid(Text, InStr(Text, " ")))
> > .Cells(CELLCount, "A") = Number
> > .Cells(CELLCount, "C") = Text
> > CELLCount = CELLCount + 1
> > End If
> > Loop
> > End With
> >
> > End Sub
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel crashes when I add VBA code programatically mdgabriel0616@gmail.com Microsoft Excel Programming 9 22nd May 2008 08:25 PM
VBA code ok in Excel 2003, but crashes Excel 2002 & 2000 =?Utf-8?B?dG9tZ3JlZW4xMDAw?= Microsoft Excel Programming 5 6th Jul 2007 07:46 AM
VBA code crashes Excel 97 - Help please! GB Microsoft Excel Programming 4 14th Apr 2007 07:15 AM
Code that Crashes Excel without fail - Excel 97 SR2 WinNT Matt Jensen Microsoft Excel Programming 14 10th Jan 2005 03:08 PM
Code in ThisWorkbook crashes Excel Pat Beck Microsoft Excel Programming 6 25th Aug 2003 09:07 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:55 PM.