Forcing Text Format

  • Thread starter Thread starter F. Michael Miller
  • Start date Start date
F

F. Michael Miller

I've got a spreadsheet that's being read as a datasource by an SQL statement
in a vb.net program. I have a field that contains data that can be numeric,
text, or mixed but is to be considered as text.

The problem is that a pure numeric value is being considered as numeric even
when the cell is formatted as text. If I edit the cell and put a single
quote (') at the beginning of the cell, the ' is not visible, the cell
displayes as text and imports correctly.

I need a way to do this in a macro. I've tried using ="'"&A2, but that
results in the ' being visible, and part of the data rather than just
forcing to text.

Any ideas?
 
.....
when the cell is formatted as text. If I edit the cell and put a single
quote (') at the beginning of the cell, the ' is not visible, the cell
displayes as text and imports correctly.
this is intendet behaviour and the correct way.

Cellentries starting with a single quote are Text - formatted.
 
for each cell in selection
cell.numberformat = "@"
cell.Value = "'" & cell.Text
Next
 
That did it, thanks!:

Sub AllCellsInSelectionToText()

Range("A2").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Dim CurCell As Object
For Each CurCell In Selection
CurCell.NumberFormat = "@"
CurCell.Value = "'" & CurCell.Text
Next

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top