Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem with retaining numbers formatted as text after code runs

I am running code to check through each column in a worksheet and make
substitutions for various characters, e.g.

For i = 1 To numcols
For j = 1 To numrows - 1
fvalue = ActiveCell.Offset(j, 0)
Call cleanse_data
ActiveCell.Offset(j, 0).Value = StrConv(fvalue,
vbUpperCase)
Next

Sub cleanse_data()
'data cleansing routine

fvalue = Application.WorksheetFunction.Substitute
(fvalue, "&", " AND ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "#", " NO. ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "/", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "/", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "_", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, " ", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, " ", " ")
End sub

my problem is that when the value is written back into the cell
"numbers" which were previously formatted as text revert to a number
format, e.g. "000888" reverts to "888".

How can I run this code, or similar code and retain the original
formatting?

I can't use a leading apostraphe as these values are uploaded to
database system.

Would appreciate any input ...
Julie
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Problem with retaining numbers formatted as text after code runs

Not sure that I really understand the question properly. Do you mean that you
have strings that are a mixture of numerics and other characters and after
removing other characters you are left with a string of purely numerics with
leading zeros and you want to retain the leading zeros?

If the above is correct then try formatting the cells to text (either prior
to running the code or within the code.)

The following example might help to point you in the right direction.
The code takes a string with a mixture of numeric and alpha, substitutes
nothing for the alpha characters leaving numerics with leading zeros.

It then tests for all numerics in the string.
If all numerics then the activecell is formatted to text and assigns the
string to the cell. (leading zeros will be retained)
Else it simply assigns the text to the cell without formatting.

Note that some databases when importing data do not like a mixture of text
and numerics in the same column so you might be better to simply format the
entire column to text.



--
Regards,

OssieMac


"needhelp" wrote:

I am running code to check through each column in a worksheet and make
substitutions for various characters, e.g.

For i = 1 To numcols
For j = 1 To numrows - 1
fvalue = ActiveCell.Offset(j, 0)
Call cleanse_data
ActiveCell.Offset(j, 0).Value = StrConv(fvalue,
vbUpperCase)
Next

Sub cleanse_data()
'data cleansing routine

fvalue = Application.WorksheetFunction.Substitute
(fvalue, "&", " AND ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "#", " NO. ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "/", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "/", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, "_", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, " ", " ")
fvalue = Application.WorksheetFunction.Substitute
(fvalue, " ", " ")
End sub

my problem is that when the value is written back into the cell
"numbers" which were previously formatted as text revert to a number
format, e.g. "000888" reverts to "888".

How can I run this code, or similar code and retain the original
formatting?

I can't use a leading apostraphe as these values are uploaded to
database system.

Would appreciate any input ...
Julie

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Problem with retaining numbers formatted as text after code runs

Hello Julie,

Something failed as I was trying to copy the code before so here it is again.

Not sure that I really understand the question properly. Do you mean that
you have strings that are a mixture of numerics and other characters and
after removing other characters you are left with a string of purely numerics
with leading zeros and you want to retain the leading zeros?

If the above is correct then try formatting the cells to text (either prior
to running the code or within the code.)

The following example might help to point you in the right direction.
The code takes a string with a mixture of numeric and alpha, substitutes
nothing for the alpha characters leaving numerics with leading zeros.

It then tests for all numerics in the string.
If all numerics then the activecell is formatted to text and assigns the
string to the cell. (leading zeros will be retained)
Else it simply assigns the text to the cell without formatting.

Note that some databases when importing data do not like a mixture of text
and numerics in the same column so you might be better to simply format the
entire column to text.

Dim fValue As String

fValue = "000B898A"

fValue = Application.WorksheetFunction.Substitute(fValue, "A", "")
fValue = Application.WorksheetFunction.Substitute(fValue, "B", "")

If IsNumeric(fValue) Then
'Format cell as text
ActiveCell.NumberFormat = "@"

ActiveCell = fValue
Else
ActiveCell = fValue
End If

--
Regards,

OssieMac


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Problem with retaining numbers formatted as text after code runs

thank you OssieMac

I had actually tried using

ActiveCell.Offset(j, 0).NumberFormat = "@"

in my code before posting my question and it didn't work.

It does now, so i'm off to have a coffee!

Cheers
Julie
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Problem with retaining numbers formatted as text after code ru

Hi again Julie,

Did you have the number format in the correct place in the code in your
initial test?

The number format must be performed before assigning the value or it will
not work because the leading zeros will have already been removed and while
it will become text, it cannot replace the leading zeros.

Anyway pleased for you that you have it working now so good luck.

--
Regards,

OssieMac


"needhelp" wrote:

thank you OssieMac

I had actually tried using

ActiveCell.Offset(j, 0).NumberFormat = "@"

in my code before posting my question and it didn't work.

It does now, so i'm off to have a coffee!

Cheers
Julie

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
numbers formatted as text willemeulen[_28_] Excel Discussion (Misc queries) 13 June 8th 09 02:25 PM
a code to trace pure numbers formatted as Text swordsman Excel Programming 4 September 3rd 07 11:28 PM
formatted numbers to CSV problem mark Excel Programming 6 November 15th 05 11:09 PM
Pivot Table keep apart cells text that I've formatted as numbers Paulo Bevervanso Excel Worksheet Functions 2 September 21st 05 08:45 PM
Converting numbers formatted as text to numbers Bill Excel Discussion (Misc queries) 1 July 19th 05 07:10 PM


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