Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
tod tod is offline
external usenet poster
 
Posts: 114
Default Apply expression to entire range

Here's an easy one for somebody.

My code currently cycles through a column in a worksheet
and removes unwanted characters, like this:

For Each Cell In mySheet.Range("A2:A" & mySheet.Range
("A65536").End(xlUp).Row)
If Left(Cell, 1) = "=" Or _
Left(Cell, 1) = "-" Or _
Left(Cell, 1) = "+" Or Then
Cell.Value = "'" & Cell.Value
End If
Next Cell

Problem is that if the worksheet has several thousand
records this code could take minutes. Is there a way to
apply this to the entire range at once instead of cycling
the range?

tod


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Apply expression to entire range

You can make it quicker

application.Screenupdating = False
Application.Calculation = xlCalculationManual

cLastRow = mySheet.Cells(Rows.Count,"A").End(xlUp).Row
For Each Cell In mySheet.Range("A2:A" & cLastRow)
tmp = Left(Cell, 1)
If tmp = "=" Or tmp = "-" Or tmp = "+" Then
Cell.Value = "'" & Cell.Value
End If
Next Cell


application.Screenupdating = True
Application.Calculation = xlCalculationAutomatic

--
HTH

-------

Bob Phillips
"Tod" wrote in message
...
Here's an easy one for somebody.

My code currently cycles through a column in a worksheet
and removes unwanted characters, like this:

For Each Cell In mySheet.Range("A2:A" & mySheet.Range
("A65536").End(xlUp).Row)
If Left(Cell, 1) = "=" Or _
Left(Cell, 1) = "-" Or _
Left(Cell, 1) = "+" Or Then
Cell.Value = "'" & Cell.Value
End If
Next Cell

Problem is that if the worksheet has several thousand
records this code could take minutes. Is there a way to
apply this to the entire range at once instead of cycling
the range?

tod




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Apply expression to entire range

Just guessing, but would you be trying to clear formulas, and numbers I am
not sure of what data you have, so I'll just throw out this idea.

Sub Demo()
ActiveSheet.UsedRange
On Error Resume Next
With Columns("A:A")
.SpecialCells(xlCellTypeFormulas).ClearContents
.SpecialCells(xlCellTypeConstants, xlNumbers).ClearContents
End With
End Sub

HTH
--
Dana DeLouis
Win XP & Office 2003


"Tod" wrote in message
...
Here's an easy one for somebody.

My code currently cycles through a column in a worksheet
and removes unwanted characters, like this:

For Each Cell In mySheet.Range("A2:A" & mySheet.Range
("A65536").End(xlUp).Row)
If Left(Cell, 1) = "=" Or _
Left(Cell, 1) = "-" Or _
Left(Cell, 1) = "+" Or Then
Cell.Value = "'" & Cell.Value
End If
Next Cell

Problem is that if the worksheet has several thousand
records this code could take minutes. Is there a way to
apply this to the entire range at once instead of cycling
the range?

tod




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Apply expression to entire range

It is not clear what the intent is because the code as shown doesn't
remove any characters. Further, by not specifying which property to
use in the If statement, you are relying on the default property of the
range object. Effectively you are checking Left(Cell,1).value.

So, the only way you can get '=' or '+' in the value of a cell is if
the cell contains text.

In any case, the code below duplicates as closely as I can figure out
your code and takes about 2 seconds to execute for 65,535 rows of data.
It uses a spare column as a scratch workarea (column B in this case) to
have XL do the work for us. Then, it copies the result back into
column A and clears the contents of the scratch area.

Sub Macro2()
Dim StartTime As Date
StartTime = Now()
Range("b1").FormulaArray = _
"=IF(ISBLANK(RC[-1]),"""",IF(OR(LEFT(RC[-1],1)=
{""="",""-"",""+""}),""'""&RC[-1],RC[-1]))"
Range("b1").AutoFill Columns("b")
Columns(2).Copy
Columns(1).PasteSpecial xlPasteValues
Columns(2).Clear
MsgBox Format(Now() - StartTime, "hh:mm:ss.s")
End Sub

[watch out for line breaks in the post.]

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Here's an easy one for somebody.

My code currently cycles through a column in a worksheet
and removes unwanted characters, like this:

For Each Cell In mySheet.Range("A2:A" & mySheet.Range
("A65536").End(xlUp).Row)
If Left(Cell, 1) = "=" Or _
Left(Cell, 1) = "-" Or _
Left(Cell, 1) = "+" Or Then
Cell.Value = "'" & Cell.Value
End If
Next Cell

Problem is that if the worksheet has several thousand
records this code could take minutes. Is there a way to
apply this to the entire range at once instead of cycling
the range?

tod



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
How do I apply formula to entire column Stephan Excel Discussion (Misc queries) 2 October 1st 06 06:04 PM
HOW DO I APPLY A FORMULA FOR AND ENTIRE ROW? jcpayne Excel Worksheet Functions 2 July 19th 06 06:02 PM
How do I apply my function to the entire column? jsthngn Excel Discussion (Misc queries) 1 January 23rd 06 03:21 PM
I want to apply a formula to an entire column. kitcox Excel Discussion (Misc queries) 3 November 28th 05 01:48 AM
I would like to apply the same calculation to an entire row mnirula Excel Worksheet Functions 3 April 25th 05 10:05 PM


All times are GMT +1. The time now is 09:35 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"