Home > Mobile >  How to align code generated by a macro (VBA)
How to align code generated by a macro (VBA)

Time:06-22

  • List item

i'm using a macro to generate some code. I have this to print it on a .vhdl file

While iloop_line <= nblines
         If MySheet.Cells(iloop_line, Type_Column) = Type_Register Then
         
            nbRegister= nbRegister  1
         
            reg_addr = Mid(MySheet.Cells(iloop_line, Low_ADDR_Column), 1, 6)

            Print #my_file_id, "    constant " & LCase(MySheet.Cells(iloop_line, Name_Column)) & "_addr     : std_logic_vector(" & AddrWidthTocomp - 1 & " downto 0) := X""" & reg_addr & """;"

            reg_data = Replace(Mid(MySheet.Cells(iloop_line, Default_Value_Column), 3, 8), "_", "")

            Print #my_file_id, "    constant " & LCase(MySheet.Cells(iloop_ligne, Name_Column)) & "_val     : std_logic_vector(" & AddrWidthTocomp - 1 & " downto 0) := X""" & reg_data & """;"
            Print #my_file_id, ""
            End If
            iloop_line = iloop_line   1
    Wend

And i have this in result :

    constant reg_test12345_addr     : std_logic_vector(23 downto 0) := X"000000";
    constant reg_test12345_val     : std_logic_vector(23 downto 0) := X"000000";

    constant reg_123456789_addr     : std_logic_vector(23 downto 0) := X"0000FF";
    constant reg_123456789_val     : std_logic_vector(23 downto 0) := X"000000";

    constant reg_1234567891234567_addr     : std_logic_vector(23 downto 0) := X"000100";
    constant reg_reg_1234567891234567_val     : std_logic_vector(23 downto 0) := X"000000";

And to make it cleaner i want this :

    constant reg_test12345_addr                  : std_logic_vector(23 downto 0) := X"000000";
    constant reg_test12345_val                : std_logic_vector(23 downto 0) := X"000000";

    constant reg_123456789_addr               : std_logic_vector(23 downto 0) := X"0000FF";
    constant reg_123456789_val                : std_logic_vector(23 downto 0) := X"000000";

    constant reg_1234567891234567_addr        : std_logic_vector(23 downto 0) := X"000100";
    constant reg_reg_1234567891234567_val     : std_logic_vector(23 downto 0) := X"000000";

Do you have any idea to make this ?

CodePudding user response:

As an example I inserted various string lengths into Sheet1!B1:B20 using =REPT("a",RANDBETWEEN(1,20)).

Sub Test()

    Dim MySheet As Worksheet
    Set MySheet = ThisWorkbook.Worksheets("Sheet1")

    Dim Name_Column As Long
    Name_Column = 2

    Dim iloop_line As Long
    Dim StringValue As String
    For iloop_line = 1 To 20
        StringValue = LCase(MySheet.Cells(iloop_line, Name_Column))
        Debug.Print StringValue & "_addr" & Space(30 - Len(StringValue & "_addr")) & ": std_logic_vector"
        Debug.Print StringValue & "_val" & Space(30 - Len(StringValue & "_val")) & ": std_logic_vector"
    Next iloop_line
    
End Sub

The code above gave me the output below - the : is always the 31st character.

aaaaaaaaaaa_addr              : std_logic_vector
aaaaaaaaaaa_val               : std_logic_vector
aaaaaaaaaaaa_addr             : std_logic_vector
aaaaaaaaaaaa_val              : std_logic_vector
aaaaaaaaaa_addr               : std_logic_vector
aaaaaaaaaa_val                : std_logic_vector
aaaaaaaaaa_addr               : std_logic_vector
aaaaaaaaaa_val                : std_logic_vector 

Edit: Well that's embarrassing - never knew about the SPACE command in VBA. Have updated code to use that rather than REPT. Think last time I used that could have been with Sinclair BASIC.

CodePudding user response:

Another approach is to use a fixed length string.

Dim fixedString As String * 30

fixedString = "abc"
Debug.Print fixedString & ": " & 1

fixedString = "something longer"
Debug.Print fixedString & ": " & 2

'abc                           : 1
'something longer              : 2
  • Related