Ensure cell content is text using VBA macro

KG Old Wolf

Board Regular
Joined
Sep 20, 2009
Messages
65
All,

This seems so easy but it is eluding me. The end result is to produce a table that has a column of 5 digit values - sometimes alpha and other times numeric. I want every value to be TEXT... never numeric. (The field is to be used as a Lookup key going up against a table in which all the entries are in Text).

I can get what I want using =Text(x,"00000") but I want to execute this within a VBA macro. Using the Application.WorksheetFunction.Text(x,"00000") will convert the variable to the text value I want but it reverts to a numeric value as soon as I post the work field back to the cell.

i.e.

X_Chg = Application.WorksheetFunction.Text(Cells(Rows,Columns))
X_Chg_test = Application.WorksheetFunction.IsText(X_Chg)
MsgBox(X_Chg_Test)
Cells(Rows,Columns) = X_Chg

I a message box to display if X_Chg "isText" and it shows TRUE but after the macro completes and I test the field, it comes back FALSE.

All I want to do is ensure that what I place in Cells(Rows,Columns) will always be in TEXT format.... what am I missing?

Thanks in advance
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
KG Old Wolf,

Try:

Code:
With Cells(Rows, Columns)
  .NumberFormat = "@"
  .Value = X_Chg
End With
 

KG Old Wolf

Board Regular
Joined
Sep 20, 2009
Messages
65
Hiker! Yes, that worked... unfamiliar with the use of the "@" value in number formats so.... time to do a little more research on this end. Thanks a million for the prompt help.

Ken
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,684
Messages
5,512,856
Members
408,916
Latest member
juliesmithing

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top