+ Reply to Thread
Results 1 to 3 of 3

Keep leading zero in vba

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2020
    Location
    Indonesia
    MS-Off Ver
    2013
    Posts
    1

    Question Keep leading zero in vba

    Hi , I need help for my vba code . I want to keep the leading zero after doing permutation . What should i do ? Thanks

    Sub Test()
      Dim Number As String
      Dim Numbers, Data, Item, Value
      Dim C As Collection
      Dim Dict As Object 'Scripting.Dictionary
      Dim i As Integer
      'Read the number from the cell
      Number = Range("A1")
      'Split into chars
      ReDim Numbers(1 To Len(Number))
      For i = 1 To Len(Number)
        Numbers(i) = Mid$(Number, i, 1)
      Next
      'Build all combinations
      Set C = PermutationsTail(Numbers)
      Debug.Print C.Count
      'Convert into values and remove duplicates
      Set Dict = CreateObject("Scripting.Dictionary")
      For Each Item In C
        Value = CDbl(Join(Item, ""))
        If Not Dict.Exists(Value) Then Dict.Add Value, 0
      Next
      'Get the remaning values
      Data = Dict.Keys
      'Flush into the sheet
      With Range("C1")
        .EntireColumn.ClearContents
        .Resize(UBound(Data) + 1, 1).Value = WorksheetFunction.Transpose(Data)
      End With
    End Sub
    Function PermutationsTail(ByVal Arr) As Collection
      'Return all possible permutations in Arr as arrays in a collection
      'Based on a c++ algorithm by Phillip Paul Fuchs
      'Tail Permutations Using a Linear Array Without Recursion
      Dim i As Long, j As Long, ax As Long, N As Long
      Dim p() As Long, Temp
      Set PermutationsTail = New Collection
      'constant index ceiling (Arr[N] length)
      N = UBound(Arr) - LBound(Arr) + 1
      If N = 0 Then Exit Function
      ax = N - 1
      'target array and index control array
      ReDim p(0 To N)
      'p[N] > 0 controls iteration and the index boundary for i
      For i = 0 To N
        p(i) = i
      Next
      PermutationsTail.Add Arr
      'setup first swap points to be ax-1 and ax respectively (i & j)
      i = 1
      Do While i < N
        'decrease index "weight" for i by one
        p(i) = p(i) - 1
        'If i is odd then j = ax - p[i] otherwise j = ax
        j = ax - (i Mod 2) * p(i) + LBound(Arr)
        'adjust i to permute tail (i < j)
        i = ax - i + LBound(Arr)
        'set Scope
        Temp = Arr(j)
        Arr(j) = Arr(i)
        Arr(i) = Temp
        PermutationsTail.Add Arr
        'reset index i to 1 (assumed)
        i = 1
        Do While p(i) = 0
          'reset p[i] zero value
          p(i) = i
          'set new index value for i (increase by one)
          i = i + 1
        Loop
      Loop
    End Function
    Ru.PNG
    Last edited by alansidman; 07-08-2020 at 08:50 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,736

    Re: Keep leading zero in vba

    Code Tags Added
    Your post does not comply with Rule 2 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,177

    Re: Keep leading zero in vba

    Try this
    With Range("C1")
        .EntireColumn.ClearContents
        .Resize(UBound(Data) + 1, 1).Value = WorksheetFunction.Transpose(Data)
        .Resize(UBound(Data) + 1, 1).NumberFormat = String(Len(Number), "0")
      End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to add leading zero.
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-13-2020, 11:01 AM
  2. Help with leading 0's
    By pleasir in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-07-2017, 07:18 PM
  3. Leading Zero
    By zachharriman in forum Excel General
    Replies: 7
    Last Post: 05-03-2008, 04:21 AM
  4. Leading 0's
    By exsam21 in forum Excel General
    Replies: 5
    Last Post: 01-20-2006, 02:40 PM
  5. [SOLVED] Leading zero
    By Firefli in forum Excel General
    Replies: 2
    Last Post: 11-09-2005, 01:30 PM
  6. leading 0
    By cb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-13-2005, 10:06 AM
  7. [SOLVED] Add a leading zero
    By yahoo in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 03-12-2005, 03:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1