Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default i.Value, i.Text, Or just i?

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default i.Value, i.Text, Or just i?

I have no idea what's more efficient, but I would use i.value. I like to
specify the property and not depend on the default.

But if I wanted to be more efficient, I'd add:

Option Compare Text

to the top of the module.

Then
if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

Second, these are mutually exclusive conditions. A cell's value can't be NYCITY
and FTBENNING.

You could use if/then/else if...

if i.value = "nyCity" then
i.value = "NYC"
elseif i.value = "ftbenning then
i.value = "FTB"
....

Then you're not asking excel to look at each potential string.

In fact, a bunch of those if/then/else if's can get difficult to read. You may
want to look at:

Select case i.value
case is = "NYCITY" : i.value = "NYC"
case is = "FTBENNING" : i.value = "FTB"
case is = ...
End Select

I find this easier to read.

And finally (whew!), I wouldn't do this at all.

I'd just use a series of edit|Replaces against the range. Then instead of
checking each cell for its value, all the cells in the range are changed in one
command.

with irange
.replace what:="NYCITY", replacement:="NYC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.replace what:=....
end with

If there are lots of these, you could even set up a couple of arrays and loop
through the arrays.

Dim myWhats as variant
dim myWiths as variant
dim ictr as long

mywhats = array("nycity","ftbenning")
mywiths = array("NYC","FTB")

if ubound(mywhats) < ubound(mywiths) then
msgbox "not the same number!"
exit sub
end if

for ictr = lbound(mywhats) to ubound(mywhats)
irange.replace what:=mywhats(ictr), replacement:=mywiths(ictr),
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
next ictr





"Dan R." wrote:

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default i.Value, i.Text, Or just i?

In this portion:

if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

I was trying to say that with "option compare text", then nycity, NYCity, NyCiTy
would be picked up by a single statement--not that you'd need to do each
individually.


Dave Peterson wrote:

I have no idea what's more efficient, but I would use i.value. I like to
specify the property and not depend on the default.

But if I wanted to be more efficient, I'd add:

Option Compare Text

to the top of the module.

Then
if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

Second, these are mutually exclusive conditions. A cell's value can't be NYCITY
and FTBENNING.

You could use if/then/else if...

if i.value = "nyCity" then
i.value = "NYC"
elseif i.value = "ftbenning then
i.value = "FTB"
...

Then you're not asking excel to look at each potential string.

In fact, a bunch of those if/then/else if's can get difficult to read. You may
want to look at:

Select case i.value
case is = "NYCITY" : i.value = "NYC"
case is = "FTBENNING" : i.value = "FTB"
case is = ...
End Select

I find this easier to read.

And finally (whew!), I wouldn't do this at all.

I'd just use a series of edit|Replaces against the range. Then instead of
checking each cell for its value, all the cells in the range are changed in one
command.

with irange
.replace what:="NYCITY", replacement:="NYC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.replace what:=....
end with

If there are lots of these, you could even set up a couple of arrays and loop
through the arrays.

Dim myWhats as variant
dim myWiths as variant
dim ictr as long

mywhats = array("nycity","ftbenning")
mywiths = array("NYC","FTB")

if ubound(mywhats) < ubound(mywiths) then
msgbox "not the same number!"
exit sub
end if

for ictr = lbound(mywhats) to ubound(mywhats)
irange.replace what:=mywhats(ictr), replacement:=mywiths(ictr),
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
next ictr

"Dan R." wrote:

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan


--

Dave Peterson


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default i.Value, i.Text, Or just i?

Couldn't have asked for a better response... my code's running about
twice as fast now. Thanks Dave.

-- Dan

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default i.Value, i.Text, Or just i?

I like to specify the property and not depend on the default.

Then you should advocate changing this

For Each i In iRange

to this

For Each i In iRange.Cells

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Dave Peterson" wrote in message
...
I have no idea what's more efficient, but I would use i.value. I like to
specify the property and not depend on the default.

But if I wanted to be more efficient, I'd add:

Option Compare Text

to the top of the module.

Then
if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

Second, these are mutually exclusive conditions. A cell's value can't be
NYCITY
and FTBENNING.

You could use if/then/else if...

if i.value = "nyCity" then
i.value = "NYC"
elseif i.value = "ftbenning then
i.value = "FTB"
...

Then you're not asking excel to look at each potential string.

In fact, a bunch of those if/then/else if's can get difficult to read.
You may
want to look at:

Select case i.value
case is = "NYCITY" : i.value = "NYC"
case is = "FTBENNING" : i.value = "FTB"
case is = ...
End Select

I find this easier to read.

And finally (whew!), I wouldn't do this at all.

I'd just use a series of edit|Replaces against the range. Then instead of
checking each cell for its value, all the cells in the range are changed
in one
command.

with irange
.replace what:="NYCITY", replacement:="NYC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.replace what:=....
end with

If there are lots of these, you could even set up a couple of arrays and
loop
through the arrays.

Dim myWhats as variant
dim myWiths as variant
dim ictr as long

mywhats = array("nycity","ftbenning")
mywiths = array("NYC","FTB")

if ubound(mywhats) < ubound(mywiths) then
msgbox "not the same number!"
exit sub
end if

for ictr = lbound(mywhats) to ubound(mywhats)
irange.replace what:=mywhats(ictr), replacement:=mywiths(ictr),
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
next ictr





"Dan R." wrote:

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan


--

Dave Peterson





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,582
Default i.Value, i.Text, Or just i?

If there are a bunch of things to hunt for, I'd use Select Case:

Select Case i.Value
Case "NYCITY"
i.Value = "NYC"
Case "FTBENNING"
i.Value = "FORTBENNING"
End Select

I might resort to UCase instead of Option Compare Text:

Select Case UCase$(i.Value)
Case "NYCITY"
i.Value = "NYC"

If there are a lot of things, I'd set up a two column range in the worksheet
and use WorksheetFunction.VLookup, or the two-array approach of Dave's.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______


"Dave Peterson" wrote in message
...
I have no idea what's more efficient, but I would use i.value. I like to
specify the property and not depend on the default.

But if I wanted to be more efficient, I'd add:

Option Compare Text

to the top of the module.

Then
if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

Second, these are mutually exclusive conditions. A cell's value can't be
NYCITY
and FTBENNING.

You could use if/then/else if...

if i.value = "nyCity" then
i.value = "NYC"
elseif i.value = "ftbenning then
i.value = "FTB"
...

Then you're not asking excel to look at each potential string.

In fact, a bunch of those if/then/else if's can get difficult to read.
You may
want to look at:

Select case i.value
case is = "NYCITY" : i.value = "NYC"
case is = "FTBENNING" : i.value = "FTB"
case is = ...
End Select

I find this easier to read.

And finally (whew!), I wouldn't do this at all.

I'd just use a series of edit|Replaces against the range. Then instead of
checking each cell for its value, all the cells in the range are changed
in one
command.

with irange
.replace what:="NYCITY", replacement:="NYC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.replace what:=....
end with

If there are lots of these, you could even set up a couple of arrays and
loop
through the arrays.

Dim myWhats as variant
dim myWiths as variant
dim ictr as long

mywhats = array("nycity","ftbenning")
mywiths = array("NYC","FTB")

if ubound(mywhats) < ubound(mywiths) then
msgbox "not the same number!"
exit sub
end if

for ictr = lbound(mywhats) to ubound(mywhats)
irange.replace what:=mywhats(ictr), replacement:=mywiths(ictr),
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
next ictr





"Dan R." wrote:

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan


--

Dave Peterson



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default i.Value, i.Text, Or just i?

Oops. I missed that one.

I agree with you.

Jon Peltier wrote:

I like to specify the property and not depend on the default.


Then you should advocate changing this

For Each i In iRange

to this

For Each i In iRange.Cells

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
http://PeltierTech.com
_______

"Dave Peterson" wrote in message
...
I have no idea what's more efficient, but I would use i.value. I like to
specify the property and not depend on the default.

But if I wanted to be more efficient, I'd add:

Option Compare Text

to the top of the module.

Then
if i.value = "nycity" then i.value = "NYC"
and
if i.value = "nyCity" then i.value = "NYC"
and
if i.value = "NYCITY" then i.value = "NYC"

would all be replaced with NYC.

Second, these are mutually exclusive conditions. A cell's value can't be
NYCITY
and FTBENNING.

You could use if/then/else if...

if i.value = "nyCity" then
i.value = "NYC"
elseif i.value = "ftbenning then
i.value = "FTB"
...

Then you're not asking excel to look at each potential string.

In fact, a bunch of those if/then/else if's can get difficult to read.
You may
want to look at:

Select case i.value
case is = "NYCITY" : i.value = "NYC"
case is = "FTBENNING" : i.value = "FTB"
case is = ...
End Select

I find this easier to read.

And finally (whew!), I wouldn't do this at all.

I'd just use a series of edit|Replaces against the range. Then instead of
checking each cell for its value, all the cells in the range are changed
in one
command.

with irange
.replace what:="NYCITY", replacement:="NYC", LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False
.replace what:=....
end with

If there are lots of these, you could even set up a couple of arrays and
loop
through the arrays.

Dim myWhats as variant
dim myWiths as variant
dim ictr as long

mywhats = array("nycity","ftbenning")
mywiths = array("NYC","FTB")

if ubound(mywhats) < ubound(mywiths) then
msgbox "not the same number!"
exit sub
end if

for ictr = lbound(mywhats) to ubound(mywhats)
irange.replace what:=mywhats(ictr), replacement:=mywiths(ictr),
LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
next ictr





"Dan R." wrote:

I'm using a simple loop like this to replace certain values in a
range:

For Each i In iRange
If i = "NYCITY" Then i = "NYC"
If i = "FTBENNING" Then i = "FORTBENNING"
'...
Next

The values will always be text, so is it more efficient to use i.Text?
Or maybe i.Value? Or is it best to just leave it as i?

Thanks,
-- Dan


--

Dave Peterson


--

Dave Peterson
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
Using TEXT and &TEXT - display numbers with commas, underline text Gary Excel Discussion (Misc queries) 3 May 5th 23 03:46 AM
Text does not display in "Text boxs" and when wrapping text in a c Esteban Excel Discussion (Misc queries) 1 March 8th 07 11:59 PM
select text in cell based on text from another cell, paste the text at the begining of a thrid cell, etc... jsd219 Excel Programming 0 October 19th 06 05:04 PM
Excel VBA: Worksheet cell .Text property: 1024 bytes text len limit loyso Excel Programming 7 May 3rd 05 02:51 PM
extracting text from within a cell - 'text to rows@ equivalent of 'text to columns' Dan E[_2_] Excel Programming 4 July 30th 03 06:43 PM


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