+ Reply to Thread
Results 1 to 8 of 8

left padding (with given character)

Hybrid View

acolbrantNMBS left padding (with given... 03-04-2019, 06:27 AM
Roel Jongman Re: left padding (with given... 03-04-2019, 07:06 AM
acolbrantNMBS Re: left padding (with given... 03-04-2019, 07:46 AM
nigelog Re: left padding (with given... 03-04-2019, 07:52 AM
acolbrantNMBS Re: left padding (with given... 03-04-2019, 08:22 AM
jindon Re: left padding (with given... 03-04-2019, 08:08 AM
Roel Jongman Re: left padding (with given... 03-04-2019, 08:44 AM
acolbrantNMBS Re: left padding (with given... 03-04-2019, 08:53 AM
  1. #1
    Registered User
    Join Date
    10-25-2016
    Location
    Brussels
    MS-Off Ver
    2010
    Posts
    17

    left padding (with given character)

    Hi Folks

    For each cell in a selection I would want the following executed:
    set the cell format to 'text'
    select the needed length of the cell by InputBox [=Var1]
    select the character to use for padding by inputbox [=Var2]
    left pad the cells according to the above input.

    Any help very much appreciated.

    Grtz
    A

  2. #2
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,493

    Re: left padding (with given character)

    Have a look if the code below gives you what you need

    Sub CellPadding()
    
    Length = InputBox(Prompt:="Enter number of repeats", Title:="Cell Padding - Repetitions")
    Charac = InputBox(Prompt:="Enter character", Title:="Cell Padding - Choose character")
    
    If IsNumeric(Length) Then
        For i = 1 To Length
           Cellpad = Cellpad & Charac
        Next i
    End If
    With Selection
        .Value = Cellpad
        .NumberFormat = "@"
        .HorizontalAlignment = xlLeft
    End With
    End Sub

    Just to point out in case you dont know...
    You also have several options without VBA please read this link to see how you can fill out a cell with a function or with (custom) cell formatting
    https://www.extendoffice.com/documen...r-in-cell.html

  3. #3
    Registered User
    Join Date
    10-25-2016
    Location
    Brussels
    MS-Off Ver
    2010
    Posts
    17

    Re: left padding (with given character)

    Hi Roel

    Thanks for your answer and code.
    My question was not so clear, sorry.

    Var1 = is the desired length of the string in the cells, not the number of repeats

    eg.
    Var1 = 9
    Var2 = a

    Then eg.
    12526 ==> aaa12526
    526 ==> aaaaaaa526
    12526000 ==> a12526000
    125265626 ==> 125265626

    Thx for your effort/feedback.
    i kind of like macroes, since they do the job quite quickly...

    Grtz
    A

  4. #4
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: left padding (with given character)

    Try
    Sub CellPadding()
    Dim Length
    Dim Charac As String: Dim cellpad As String: Dim Content As String
    Dim Cl As Integer: Dim i As Integer: Dim mylength As Integer
    Content = Selection.Text
    Cl = Len(Content)
    Length = InputBox(Prompt:="Enter Padded Length", Title:="")
    Charac = InputBox(Prompt:="Enter character", Title:="")
    mylength = Length - Cl
      
        For i = 1 To mylength
           cellpad = cellpad & Charac
        Next i
    MsgBox cellpad & Content
    Selection = cellpad & Content
    End Sub
    Last edited by nigelog; 03-04-2019 at 07:54 AM.

  5. #5
    Registered User
    Join Date
    10-25-2016
    Location
    Brussels
    MS-Off Ver
    2010
    Posts
    17

    Re: left padding (with given character)

    Hi Nigelog
    Thx for your feedback.
    That does the trick when one cell is selected; when a range selected marco halts at
    Content = Selection.Text

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: left padding (with given character)

    Try
    Sub test()
        Dim e
        For Each e In Array(12526, 526, 12526000, 125265626)
            MsgBox LeftPadding(e, 9, "a")
        Next
    End Sub
    
    Function LeftPadding(ByVal txt As String, Var1 As Long, Var2 As String) As String
        LeftPadding = IIf(Len(txt) < Var1, String(Var1 - Len(txt), Var2), "") & txt
    End Function

  7. #7
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,493

    Re: left padding (with given character)

    Ok, lets try with this code then..
    Sub CellPadding()
    
    Length = InputBox(Prompt:="Enter length of cell", Title:="Cell Padding - Desired Length")
    Charac = InputBox(Prompt:="Enter character", Title:="Cell Padding - Choose character")
    
    
    If IsNumeric(Length) Then
        For Each cl In Selection
            Cellpad = Length - Len(cl.Value)
             If Cellpad > 0 Then
                With cl
                    .Value = WorksheetFunction.Rept(Charac, Cellpad) & cl.Value
                    .NumberFormat = "@"
                    .HorizontalAlignment = xlLeft
                End With
            End If
        Next
    End If
    End Sub

  8. #8
    Registered User
    Join Date
    10-25-2016
    Location
    Brussels
    MS-Off Ver
    2010
    Posts
    17

    Re: left padding (with given character)

    Hi Roel

    That does the trick!
    Thx very much, you made my day.

    Thx for the other suggestions; quite learned from it as well

    Grtz
    A

+ 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] How to use LEFT and RIGHT to delete last character
    By nicolaivinther in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-06-2014, 09:53 AM
  2. Box as rows and padding left
    By toplisek in forum Excel General
    Replies: 1
    Last Post: 07-28-2014, 04:46 AM
  3. Replies: 3
    Last Post: 11-20-2012, 10:03 PM
  4. Replies: 3
    Last Post: 10-31-2012, 11:20 PM
  5. Replies: 2
    Last Post: 08-19-2008, 07:18 AM
  6. left padding + blank
    By Alex in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-05-2006, 06:25 PM
  7. left padding + blank
    By Alex in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-05-2006, 06:25 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