VBA Tab Indentation

vahnx

Board Regular
Joined
Apr 10, 2011
Messages
188
How do you specify the fixed indentation alignment for text output in a file.

ie my text file looks like:

HELLO | 534
BYE | 4
HOME | 635
GIRAFFEERAA | 11

I need it to look like:

HELLO--------|-534
BYE----------|-4
HOME--------|-635
GIRAFFEERAA-|-11

or

HELLO--------|-534
BYE----------|---4
HOME--------|-635
GIRAFFEERAA-|--11

where '-' represents a space for fixed alignment. Kind of like vbTab but a fixed length of my choosing. I did this before along time ago but forget how.
 
Last edited:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Being able to see your code would be helpful but in the absence of that, the general technique for ensuring a text string is padded to a certain length is as follows:-
Code:
str=left(str & space(32),32)
 
Upvote 0
Haha yes, padding, that's the word I was looking for.

Code:
Sub Test()
    Debug.Print Now & ": The following may take as long as 30 seconds to run and may seem to freeze. Please be patient."
    Debug.Print Now & ": Sizing arrays"
    ReDim fg(MaxRows), gr(MaxRows)
    Dim j, temp
    
    Debug.Print Now & ": Populating arrays"
    For i = 2 To MaxRows
        fg(i) = Range("AW" & i).Value
        gr(i) = Range("A" & i)
    Next i

    Debug.Print Now & ": Sorting arrays"
    For i = 2 To MaxRows
        j = i
        Do While j > 0 And fg(j - 1) > fg(j)
            temp = fg(j)
            fg(j) = fg(j - 1)
            fg(j - 1) = temp
            temp = gr(j)
            gr(j) = gr(j - 1)
            gr(j - 1) = temp
            j = j - 1
        Loop
    Next i
    
    Dim startingTime, endingTime

   myfile = "C:\output.txt"
   Debug.Print Now & ": Writing to " & myfile
   fnum = FreeFile()
  
   Dim str As String
   startingTime = Timer
   Open myfile For Output As fnum
   For i = MaxRows To 2 Step -1
            If Not fg(i) = "" Then
                str = gr(i) & " | " & fg(i)
                str = Left(str & Space(32), 32)
                Print #fnum, str
            End If
    Next i
    Close #fnum
    endingTime = Timer
    Debug.Print Now & ": Total time took ", startingTime - endingTime
End Sub

The output file remains looking the same.
 
Upvote 0
I've just run this code and checked the output file and each line is padded to 32 characters on the right, exactly as you've programmed.

I think you want to pad each field separately though, the first on on the right and the second one on the left - no?

Try:-
Code:
                gr(i) = Left(gr(i) & Space(32), 32)
                fg(i) = Right(Space(32) & fg(i), 32)
                str = gr(i) & " | " & fg(i)
 
Upvote 0
aaaaaao.png


My output file.. opened in both notepad and wordpad and still no padded spaces appear, not even in the debugging window when I print it there.
 
Upvote 0
When I run your code, the lines are definitely padded on the right, at the end of each line, as programmed. You can't see them because they are spaces, but you can place your cursor on them. If you change the pad character to something visible, you will see them.

It doesn't matter in any case, because the source of the problem has been identified. Have you tried the revised code?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,610
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top