Excel cell to string vba

Microsoft Excel cells basically have two values: the formatted value that is shown to the user, and the actual value of the cell. These two values can be extremely different when the cell has certain formatting types such as date, datetime, currency, numbers with commas, etc.

I am writing a script that cleans up database data. It uses find, replace, RegEx, and other functions that act on strings of text. I need to be able to convert data into very specific formats so that my upload tool will accept the data. Therefore I need all of the cells in my Excel spreadsheets to be TEXT. I need the displayed values to match the actual values.

This is surprisingly hard to do in Excel. There is no paste special option that I could find that pastes what you see on your screen as text. «Paste Special -> Values» pastes, predictably, unformatted values. So if you try to «Paste Special -> Values» into cells formatted as TEXT, and they are coming from cells formatted as DATES, you might get something like this:

Old Cell: 4/7/2001

New Cell: 36988

If you paste normally, then the paste goes in as 4/7/2001, but in date format. So if I create a RegEx that looks for 4/7/2001 and changes it to 4/7/01, it will not work, because the underlying value is 36988.

Anyway, I discovered the Cell.Text property and wrote some code that converts all cells in a spreadsheet from formatted to text. But the code has two major problems:

1) It does not work on cells with more than 255 characters. This is because Cell.Text literally displays what the cell is actually showing to the user, and if the column is too narrow, it will display pound signs (########), and columns have a maximum width of 255 characters.

2) It is extremely slow. In my 1.5 million cell test sheet, code execution took 394 seconds.

I’m looking for ideas to optimize the code’s speed, and to make the code work on all display value lengths.

Function ConvertAllCellsToText()

    ' Convert all cells from non-text to text
    ' This is helpful in a variety of ways
        ' Dates and datetimes can now be manipulated by replace, RegEx, etc., since their value is now 4/7/2001 instead of 36988
        ' Phone numbers and zip codes won't get leading zeros deleted if we try to add those

    StartNewTask "Converting all cells to text"

    ' first draft of the code, does not work as intended, dates get scrambled
    'Cells.Select
    'Selection.Copy
    'Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'Application.CutCopyMode = False

    Dim MyRange As Range
    Set MyRange = ActiveSheet.UsedRange

    ' prevent Cell.Text from being ########
    MyRange.ColumnWidth = 255

    Dim i As Long
    Dim CellText As String
    For Each Cell In MyRange
        If Cell.Text <> Cell.Value And Len(Cell.Value) <= 255 Then
            CellText = Cell.Text
            Cell.NumberFormat = "@"
            Cell.Value = CellText
        End If

        i = i + 1
        If i Mod 2500 = 0 Then
            RefreshScreen
        End If
    Next

    MyRange.ColumnWidth = 20

    ' set all cells to Text format
    Cells.Select
    Selection.NumberFormat = "@"

End Function

  • Remove From My Forums
  • Question

  • Hi everyone,

    I hope you can help. I have a spreadsheet that is used as part of a file import process using SSIS and SQL Server. The spreadsheet has a number of date fields/columns formatted as Date (Type:*DD/MM/YYYY, Locale: English (U.K.). My question is can someone
    help me out with the VBA code that;

    a) OnOpen and OnClose of the spreadsheet, those specific cells will be changed to Text datatype? Something like the following; =TEXT(<cell value>, «DD/MM/YYYY»). Basically I want all the data except for the header row to be stripped of any formatting
    Excel has chosen and forced to be changed to TEXT. 

    b) When the spreadsheet is opened, the option to change the formatting of any cells is disabled. The file is basically a template, so all I want the users to be able to do is to enter data and to save the spreadsheet. I don’t want them editing any panes,
    windows, adding/removing columns etc.

    Your help will be much appreciated. Thanks.

Answers

  • Hi Calvin and thanks for your response. Currently I have this routine which is called at Workbook_BeforeClose;

    Sub FormatCellsAsText()

    Dim MyFormula As String
        Sheets(«Sheet1»).Unprotect userinterfaceonly:=True
        For Each MyCell In Worksheets(«Input — Corrections»).Range(«DateFields3»).Cells
            If Not (MyCell.Value = vbNullString) Or MyCell.Value <> «» Then
                MyFormula = «=Text(«»» & MyCell.Value & «»»,»»DD/MM/YYYY»»)»
                MyCell.Value = MyFormula
            End If
        Next MyCell
    End Sub

    …seems to be doing the trick. But I like the idea that you and Cheng are suggesting, which is to modify the template so that the format of the cells in the range are TEXT, then protect the worksheet so that users are unable to change the format again.
    Again, my only problem with that is how do I write the code to prevent users from copying and pasting data from elsewhere into the workbook that could change the format. i.e. do you know how I can ensure that any data which is copied and pasted will always
    be PASTESPECIAL TEXT?

    Thanks in advance for your time and help.

    • Marked as answer by

      Monday, May 23, 2011 8:31 AM

  • …seems to be doing the trick. But I like the idea that you and Cheng are suggesting, which is to modify the
    template so that the format of the cells in the range are TEXT, then protect the worksheet so that users are unable to change the format again. Again, my only problem with that
    is how do I write the code to prevent users from copying and pasting data from elsewhere into the workbook that could change the format. i.e. do you know how I can ensure that any data which is copied and pasted will always
    be PASTESPECIAL TEXT?

    Hi
    wattyjnr,

    Good work!

    The code also works fine on my side.

    When you paste data into a worksheet, you can choice which type to paste in Paste Special: Paste formulas or Paste value…

    When we paste date using right clicking, we can easily choice it from the list. However, by default, when we paste data using shortcut key (Ctrl +
    v), the value and the formatting will be paste. So we can create a macro to implement the paste value feature and make the macro’s shortcut key as Ctrl + V to “overload” the shortcut key.

    1. Record a macro;

    2. In the dialog, make the shortcut key as Ctrl + v

    3. Perform the copy and paste value actions

    4. Stop the macro

    5. Open the macro, and delete the code we don’t need. Please check the code follow:

    Sub Macro2()
    '
    ' Macro2 Macro
    '
    ' Keyboard Shortcut: Ctrl+v
    
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    End Sub
    

    I hope this helps.


    Best Regards, Calvin Gao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by
      Calvin_Gao
      Monday, May 23, 2011 8:31 AM

  • If you would like to post, please check out the MrExcel Message Board FAQ and register here. If you forgot your password, you can reset your password.

  • Thread starter

    SpearC1993

  • Start date

    Feb 19, 2016

  • #1

Hello all,

Back again for another question!

Can I use a cell value in VBA to open a file for example:

Set January = Workbooks.Open(«C:Users»CELL A1 HERE».xls») <- this is a guess

In my work sheet have a cell like such

The result would then be that the macro would open an excel in the location «C:UsersJanuary_3047.xls». At the moment I have statically written these in but I dont want to write over this code every month.

Which Excel functions can ignore hidden rows?

The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

  • #2

Try this, adjusting ‘Sheetname’ to the name of the sheet where A1 holds your bookname.

Code:

Dim MyBook As String
MyBook = Sheets("Sheetname").Range("A1").Value
Set January = Workbooks.Open("C:Users" & MyBook & ".xls")

  • #3

Thank you! Such a quick response, thank you very much :)

James006

DanteAmor

Threads
1,192,536
Messages
5,993,052
Members
440,469
Latest member
Quaichlek

Понравилась статья? Поделить с друзьями:
  • Excel cell text not formula
  • Excel cell reference in formula
  • Excel cell reference from cell
  • Excel cell numbering format
  • Excel cell not linking