Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default HELP...anybody got any ideas why this is running so slowly......

I've tried the below function over a range of 6000 cells.

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell
For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell
End Function
'======================================


.....it's very slow.
What is wrong with my code ?


Any help greatly appreciated
J

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default HELP...anybody got any ideas why this is running so slowly......

Assuming you are calling this from VBA and not using it as a User Defined
Function in a cell, perhaps:

Private Function TrimAll(myTrimArea As Range)
dim v, cell
v = myTrimArea.Value
For Each cell In v
cell = Trim(cell)
Next cell
MyTrimArea.Value = v
End Function

--
Regards,
Tom Ogilvy


"WhytheQ" wrote:

I've tried the below function over a range of 6000 cells.

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell
For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell
End Function
'======================================


.....it's very slow.
What is wrong with my code ?


Any help greatly appreciated
J


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default HELP...anybody got any ideas why this is running so slowly......

I'm wondering why you made this a function instead of a sub. How are you
calling the function?

"WhytheQ" wrote in message
oups.com...
I've tried the below function over a range of 6000 cells.

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell
For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell
End Function
'======================================


....it's very slow.
What is wrong with my code ?


Any help greatly appreciated
J



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 120
Default HELP...anybody got any ideas why this is running so slowly......


Whether is sub or function, this should help

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell

application.screenupdating = false
For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell

application.screenupdating = true
End Function


hth

Keith

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default HELP...anybody got any ideas why this is running so slowly......

nice one Keith
- screenupdating was turned off in the main routine which calls the
function, but not in the function itself.

J

On 24 Aug, 14:30, Keith74 wrote:
Whether is sub or function, this should help

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell


application.screenupdating = false For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell


application.screenupdating = true

End Function


hth

Keith





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default HELP...anybody got any ideas why this is running so slowly....

That would mean screenupdating was already turned off for your function as
shown by this modification of the help example for ScreenUpdating:

Sub ABC()
Dim ElapsedTime(2)
Application.ScreenUpdating = True
For i = 1 To 2
If i = 2 Then Application.ScreenUpdating = False
EFG ElapsedTime, i
Next i
Application.ScreenUpdating = True
MsgBox "Elapsed time, screen updating on: " & ElapsedTime(1) & _
" sec." & Chr(13) & _
"Elapsed time, screen updating off: " & ElapsedTime(2) & _
" sec."

End Sub

Sub EFG(ElapsedTime, i)
startTime = Time
Worksheets("Sheet1").Activate
For Each c In ActiveSheet.Columns
If c.Column Mod 2 = 0 Then
c.Hidden = True
End If
Next c
stopTime = Time
ElapsedTime(i) = (stopTime - startTime) * 24 * 60 * 60
End Sub

--
Regards,
Tom Ogilvy


"WhytheQ" wrote:

nice one Keith
- screenupdating was turned off in the main routine which calls the
function, but not in the function itself.

J

On 24 Aug, 14:30, Keith74 wrote:
Whether is sub or function, this should help

'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell


application.screenupdating = false For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell


application.screenupdating = true

End Function


hth

Keith




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 246
Default HELP...anybody got any ideas why this is running so slowly....

On 24 Aug, 16:06, Tom Ogilvy
wrote:
That would mean screenupdating was already turned off for your function as
shown by this modification of the help example for ScreenUpdating:

Sub ABC()
Dim ElapsedTime(2)
Application.ScreenUpdating = True
For i = 1 To 2
If i = 2 Then Application.ScreenUpdating = False
EFG ElapsedTime, i
Next i
Application.ScreenUpdating = True
MsgBox "Elapsed time, screen updating on: " & ElapsedTime(1) & _
" sec." & Chr(13) & _
"Elapsed time, screen updating off: " & ElapsedTime(2) & _
" sec."

End Sub

Sub EFG(ElapsedTime, i)
startTime = Time
Worksheets("Sheet1").Activate
For Each c In ActiveSheet.Columns
If c.Column Mod 2 = 0 Then
c.Hidden = True
End If
Next c
stopTime = Time
ElapsedTime(i) = (stopTime - startTime) * 24 * 60 * 60
End Sub

--
Regards,
Tom Ogilvy



"WhytheQ" wrote:
nice one Keith
- screenupdating was turned off in the main routine which calls the
function, but not in the function itself.


J


On 24 Aug, 14:30, Keith74 wrote:
Whether is sub or function, this should help


'======================================
Private Function TrimAll(myTrimArea As Range)
Dim cell


application.screenupdating = false For Each cell In myTrimArea.Cells
cell.Value = Trim(cell.Value)
Next cell


application.screenupdating = true


End Function


hth


Keith- Hide quoted text -


- Show quoted text -




Thanks for the help Tom.
J


Unfortunately even with screenupdating on calling that function 18,000
times is still really slow, so I changed my tack to the below (column
Z was an empty column):
'======================================
Sub TrimAll(myColumn As Integer)
With Sheets("Data")
.Range("Z2").FormulaR1C1 = "=TRIM(RC" & myColumn & ")"
.Range("Z2").AutoFill .Range(.Cells(2, 26), .Cells(x, 26))
.Range(.Cells(2, 26), .Cells(x, 26)).Copy
.Range(.Cells(2, myColumn), .Cells(x,
myColumn)).PasteSpecial xlPasteValues
End With
End Sub
'======================================


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
Can you help me figure out why my macro is running slowly? Phil Excel Programming 5 June 12th 07 11:54 PM
here is my code...it's running a bit slow though..any ideas to make it more efficient? thanks Zarlot531 Excel Programming 2 April 30th 07 02:12 PM
workbook macros now run VERY slowly ... ideas please wdeleo[_2_] Excel Programming 4 July 18th 06 12:26 AM
Paste code running extremely slowly... KR Excel Programming 1 August 3rd 05 03:26 PM
Macros in Excel 2000 running very slowly cottage6 Excel Programming 3 January 6th 05 09:43 PM


All times are GMT +1. The time now is 09:30 PM.

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

About Us

"It's about Microsoft Excel"