Use Excel VBA to repeat a character N times to generate padding, formatting markers, or placeholder text efficiently. This is commonly used in data export, report formatting, or preparing input strings for legacy systems that require fixed-width fields. No external tools or add-ins are needed—just the built-in VBA Repeat function or a simple loop.

What You'll Need

  • Microsoft Excel 2010 or later (32-bit or 64-bit)
  • Enabled Developer tab in Excel (to access the VBA editor)
  • A basic understanding of VBA syntax and how to run macros

Step-by-Step

  1. Open Excel and press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
  2. In the VBA editor, go to Insert > Module to create a new module if one doesn't already exist.
  3. Copy and paste the following function into the module:
Function RepeatChar(char As String, count As Long) As String
    If count < 0 Then
        RepeatChar = ""
        Exit Function
    End If
    RepeatChar = String(count, Asc(char))
End Function
  1. Close the VBA editor and return to Excel.
  2. In any cell, type =RepeatChar("*", 10) to generate ten asterisks (e.g., "**********").
  3. To use it in VBA code (not just a cell formula), call the function from a subroutine. For example:
Sub GeneratePadding()
    Dim result As String
    result = RepeatChar("-", 25)
    MsgBox result
End Sub
  1. Press F5 while your cursor is inside the subroutine to run it, or assign it to a button on your worksheet via the Developer tab > Insert > Button.
  2. For dynamic input, reference cell values. For example, if cell A1 contains the character and B1 contains the number, use:
Sub RepeatFromCells()
    Dim char As String
    Dim n As Long
    char = Range("A1").Value
    n = Range("B1").Value
    Range("C1").Value = RepeatChar(char, n)
End Sub
  1. Save your workbook as a macro-enabled file (.xlsm) to preserve the VBA code.

Common Mistakes

  • Using String(count, char) instead of String(count, Asc(char)) — the String function expects a character code (ASCII/Unicode), not a string. Passing a string will cause a type mismatch error.
  • Passing a multi-character string (e.g., "AB") as the first argument — the function will only use the first character of the string. Always ensure you're passing a single character.
  • Forgetting to declare the count as a Long — using Integer may cause overflow if you need to repeat a character more than 32,767 times.
  • Not saving the workbook as .xlsm — the macro will disappear when you close and reopen the file if saved as .xlsx.
  • Calling the function from a cell without enabling macros — Excel will show #NAME? error if macros are disabled.

Tips

  • The String function is the fastest way to repeat a character in VBA — it's optimized for this exact use case and faster than loops.
  • To repeat a space character, use RepeatChar(" ", 5) — the space must be included as a literal character, not omitted.
  • If you need to repeat a non-printable character (like a tab or newline), use its ASCII code: String(1, 9) for tab, String(1, 10) for line feed.
  • Use this function to build fixed-width text files for import into legacy systems — e.g., 10-character ID, 20-character name, 5-character status.
  • Test with small values (e.g., 3 or 5 repetitions) before scaling up to avoid performance issues in large loops.

Troubleshooting

  • If you get a #NAME? error in a cell, macros are disabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros (only if you trust the source).
  • If the function returns an empty string, check if the count parameter is zero or negative — the function explicitly returns blank for negative values.
  • If you see a compile error like "Invalid use of property", ensure the function is declared as Function and not Sub — functions return values; subs do not.
  • If the character is not repeating as expected (e.g., you get numbers instead of symbols), verify you're using Asc(char) correctly. For example, Asc("A") returns 65, so String(3, 65) returns "AAA".
  • If the VBA editor won't open, ensure the Developer tab is enabled: File > Options > Customize Ribbon > Check "Developer".
  • If Excel crashes when running the macro, check for circular references or if you're calling the function recursively without a base case.

Frequently Asked Questions

Can I repeat a string longer than one character?

No, the String function in VBA only repeats a single character based on its ASCII code. To repeat a multi-character string, you must use a loop or concatenation. Example: Dim i As Long, out As String: For i = 1 To 5: out = out & "ABC": Next.

What if I need to repeat a Unicode character like © or €?

Yes — use AscW instead of Asc. Modify the function to use String(count, AscW(char)) for Unicode support. This works for characters outside the ASCII range (128–255).

Can I use this in Excel Online or Google Sheets?

No. VBA is only supported in the desktop version of Excel. Excel Online does not support macros. Google Sheets uses Google Apps Script, which has a different syntax (e.g., new Array(n+1).join(char)).

Is there a way to repeat a character without writing a function?

Yes — in a cell, you can use =REPT("*",10) to repeat a character 10 times. This is the native Excel formula equivalent and doesn't require VBA. But if you need logic, variables, or integration with other VBA code, the custom function is more flexible.

Why does my function return 0 when I pass a variable from a cell?

If the cell contains text like "5" instead of the number 5, convert it: n = CLng(Range("B1").Value). Text values that look like numbers may not convert automatically in VBA if the cell format is text.

Can I use this to create borders or separators in Excel sheets?

Yes — use the repeated characters to fill a row (e.g., 50 dashes) and apply a monospace font like Consolas so the alignment stays consistent. You can then use this as a visual separator between report sections. Combine it with cell formatting for better appearance.