Forum Discussion

PaulSpanbauer's avatar
PaulSpanbauer
Copper Contributor
Apr 07, 2021
Solved

Alphanumeric Data

Hi Team Excel

 

I would like to see added a toggle switch to turn on or off stripping leading zero's from numeric text data when opened or imported into Excel.  Excel automatically strips leading zero's.  I have to use power query to retrieve data (for example, from comma delimited file) and manually back out the automatic formatting from the query.

 

Sometimes I want to retain leading zero's, sometimes I do not.  A toggle switch would help me control the Excel behavior.  Thanks for reading.

  • NikolinoDE's avatar
    NikolinoDE
    Apr 15, 2021
    I hope you don't misunderstand. It is good that you are here and of course you are welcome, as we are at microsoft in this forum.
    I was concerned that the definition could be precise. As you described it at the beginning it was to be understood as if you needed a solution to your problem. But you would like to propose a solution / disseminate it. This could be better pointed out. Anyway, I would like to apologize from the start if I have worded anything wrong and accidentally offended you.

    To put it briefly, please ask when and how much you want, there are no stupid questions, in the worst case you will get bad answers (some of them unintentionally from me).
    Through questions I also learned what I know (or not) today.

    I hope you continue to have fun with Excel .... the most beautiful invention since Schoko ... Uh, Microsoft.

    Thank you for your understanding and patience

    Nikolino
    I know I don't know anything (Socrates)

14 Replies

  • NikolinoDE's avatar
    NikolinoDE
    Platinum Contributor
    There was something ... believe in cell formatting

    Custom formatting

    0; -0 ;; @ that was for the zeros in front of it

    0. ### that was, I think, for after the decimal point
    Try it

    I tried the first "0; -0 ;; @" formula and works very well with zeros before the comma.

    Hope you can be helped with it.

    Niklino
  • Rajesh_Sinha's avatar
    Rajesh_Sinha
    Iron Contributor

    PaulSpanbauer 

     

    You need to use this VBA code:

     

     

    Private Sub CommandButton1_Click()
    
    If CommandButton1.Caption = "Remove Leading Zero" Then
    
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "Toggle Leading Zero"
    
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select The Range", xTitleId, WorkRng.Address, Type:=8)
    
    WorkRng.NumberFormat = "General"
    WorkRng.Value = WorkRng.Value
    
    CommandButton1.Caption = "Add Leading Zero"
    
    ElseIf CommandButton1.Caption = "Add Leading Zero" Then
    
    xTitleId = "Toggle Leading Zero"
    
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select The Range", xTitleId, WorkRng.Address, Type:=8)
    
    WorkRng.NumberFormat = "000000"
    
    CommandButton1.Caption = "Remove Leading Zero"
    
    End If

     

     

     

    How it works:

    • From Developer TAB hit Design Mode & on left is INSERT,, just hit it.
    • Draw Command button from ActiveX controls.
    • Select & Right click the Command button & from menu find Properties.
    • Set the CAPTION as Remove Leading Zero.
    • Press ALT+F11 to open VB editor.
    • Then Copy & Paste this code as Standard Module.
    • Save the Workbook as Macro Enabled *.xlsm.
    • From Developer TAB, release the Design Mode.
    • Now hit the command button & Follow the instructions.
    • PaulSpanbauer's avatar
      PaulSpanbauer
      Copper Contributor

      Rajesh_Sinha 

      This works on a range of data.  What I want is a toggle button in Excel configuration that turns off the standard Excel functionality of removing leading zeros from alphanumeric, imported data.

      • Rajesh_Sinha's avatar
        Rajesh_Sinha
        Iron Contributor
        Leading ZEROs are an exceptionally used Cell format is not the default one also,,, so considering all that the VBA code I've suggested is best suitable and has liberty to work with the selected data range.

        Using any programming method may spoil your data in another data range /Sheets,, and if you want to apply LEADING ZEROs as soon you open the Workbook,, in that case you need Workbook Open Event,, but remember then after you find Leading Zeros everywhere with all numeric Value !!

Resources