简体   繁体   English

修复了文本框VBA中的宽度列

[英]Fixed Width Columns in Textbox VBA

For Each cell In wb.Sheets("RP Analysis").Range("F5:F" & lastRow)
    structure = "Layer " & WorksheetFunction.RoundDown(cell.Value, 2) & ": " & WorksheetFunction.RoundDown(cell.Offset(0, 2).Value / 1000000, 2) & " xs " & WorksheetFunction.RoundDown(cell.Offset(0, 3).Value / 1000000, 2) & " attaches at "
    RMS = RMS & structure & WorksheetFunction.RoundDown(cell.Offset(0, 10).Value, 2) & "m and exhausts at " & WorksheetFunction.RoundDown(cell.Offset(0, 11).Value, 2) & "m" & vbLf
    AIR = AIR & structure & WorksheetFunction.RoundDown(cell.Offset(0, 6).Value, 2) & "m and exhausts at " & WorksheetFunction.RoundDown(cell.Offset(0, 7).Value, 2) & "m" & vbLf
Next cell

For Each cell In wb.Sheets("RP Analysis").Range("A9:A" & 19)
        gucurve = gucurve & cell.Value & ":-   " & Format(cell.Offset(0, 2).Value / cell.Offset(0, 1).Value, "Percent") & vbLf
Next cell

TextBox1.Value = "RP years    RMS/AIR difference" & vbLf & gucurve & vbLf & "AIR" & vbLf & AIR & vbLf & "RMS" & vbLf & RMS

This produces 这产生了

  Layer 1: 25 xs 50 attaches at 8.16m and exhausts at 10.4m
  Layer 2: 100 xs 75 attaches at 10.4m and exhausts at 20.15m
  Layer 3: 44 xs 175 attaches at 20.15m and exhausts at 24.96m
  Layer 4: 144 xs 175 attaches at 20.15m and exhausts at 34.86m

I want it to produce 我想要它生产

  Layer 1: 25 xs  50 attaches at  8.16m and exhausts at  10.4m
  Layer 2:100 xs  75 attaches at  10.4m and exhausts at 20.15m
  Layer 3: 44 xs 175 attaches at 20.15m and exhausts at 24.96m
  Layer 4:144 xs 175 attaches at 20.15m and exhausts at 34.86m

So I think I need fixed columns with defined widths with everything right centered. 所以我认为我需要具有定义宽度的固定列,其中所有内容都居中。 The numbers won't be more than 4 digits ever 这些数字不会超过4位数

How do I go about this? 我该怎么做?

You could use Format with the @ symbol to pad and align each value on the right: 你可以使用带有@符号的Format来填充和对齐右边的每个值:

Format("123", "@@@@@@@@@@")     ' returns "       123"

Or on the left : 或者在左边:

Format("123", "!@@@@@@@@@@")    ' returns "123       "

And by providing the number of characters: 并通过提供字符数:

Format("123", String(25, "@"))  ' returns "                      123"

One method is to create your own function that returns fixed length strings. 一种方法是创建自己的函数,返回固定长度的字符串。 The below takes a string and prefixes with as many spaces as needed to hit the required length. 下面的字符串和前缀需要尽可能多的空格来达到所需的长度。 Oversized strings are not trimmed, but this would be a simple change if required. 超大字符串不会被修剪,但如果需要,这将是一个简单的更改。

Public Function Pad(ByVal OriginalString As String, ByVal RequiredLength As Integer) As String
' Prefixes the passed string with spaces, to return a fixed width string.

    ' Check padding required.
    If RequiredLength > Len(OriginalString) Then

        ' Required, prefix with spaces.
        Pad = Space(RequiredLength - Len(OriginalString)) & OriginalString
    Else

        ' Padding not required, return original value.
        Pad = OriginalString
    End If
End Function

You would call this function like so: 您可以像这样调用此函数:

..."Layer " & Pad(WorksheetFunction.RoundDown(cell.Value, 2), 10) &...

EDIT 编辑

@Michael posted a much neater approach. @Michael发布了一个更整洁的方法。 I wanted to rewrite my pad function using his code. 我想用他的代码重写我的pad函数。 At one line; 在一条线上; the body of the function is now much easier to debug/follow. 现在,函数体更容易调试/跟踪。 I'd forgotten just how flexible VBA's format function is. 我忘记了VBA 格式功能的灵活性。

Public Function Pad(ByVal OriginalString As String, ByVal RequiredLength As Integer) As String
' Prefixes the passed string with spaces, to return a fixed width string.

    Pad = Format(OriginalString, String(RequiredLength, "@"))
End Function

I've found that the easiest code to maintain for column formatting is just building the output with fixed width strings. 我发现维护列格式化的最简单代码就是用固定宽度字符串构建输出。 They will default to left aligned if you assign with = or use LSet . 如果您使用=分配或使用LSet它们将默认为左对齐。 You can right align them by using RSet . 您可以使用RSet将它们对齐。 Also keep in mind that a fixed length string will truncate if you try to assign a string that is longer than it can hold. 另请注意,如果您尝试分配的字符串长度超过其可容纳的长度,则会截断固定长度的字符串。

Example: 例:

Private Function ToColumns(layer As Long, percent As Long, xs As Long, attach As Double, _
                           exhaust As Double) As String
    Dim col1 As String * 1      'Change the widths here to adjust your columns.
    Dim col2 As String * 3
    Dim col3 As String * 3
    Dim col4 As String * 5
    Dim col5 As String * 5

    RSet col1 = layer
    RSet col2 = percent
    RSet col3 = xs
    RSet col4 = Format$(attach, "#.##")
    RSet col5 = Format$(exhaust, "#.##")

    ToColumns = "Layer " & col1 & ":" & _
                col2 & " xs " & _
                col3 & " attaches at " & _
                col4 & "m and exhausts at " & _
                col5 & "m"
End Function

Usage: 用法:

Debug.Print ToColumns(1, 25, 50, 8.16, 10.4)
Debug.Print ToColumns(2, 100, 75, 10.4, 20.15)

Output: 输出:

Layer 1: 25 xs  50 attaches at  8.16m and exhausts at  10.4m
Layer 2:100 xs  75 attaches at  10.4m and exhausts at 20.15m

Note that as mentioned by other posters, if you are displaying this in UI you'll need a monospace font. 请注意,正如其他海报所述,如果您在UI中显示此内容,则需要使用等宽字体。

Reference: Monospaced font 参考: Monospaced字体

You need to use a monospaced font, also called a fixed-pitch, fixed-width, or non-proportional font, is a font whose letters and characters each occupy the same amount of horizontal space. 您需要使用等宽字体,也称为固定间距,固定宽度或非比例字体,是一种字体和字符各自占据相同数量的水平空间的字体。 This contrasts with variable-width fonts, where the letters and spacings have different widths. 这与可变宽度字体形成对比,其中字母和间距具有不同的宽度。

Reference: Microsoft-Supplied Monospaced TrueType Fonts 参考: Microsoft提供的Monospaced TrueType字体

The only monospaced TrueType fonts shipped by Microsoft are Courier New, which shipped with Windows 3.1, and Lucida Sans Typewriter, which was included in the TrueType Font Pack. Microsoft提供的唯一等宽TrueType字体是Windows 3.1附带的Courier New和TrueType字体包中包含的Lucida Sans Typewriter。 All other TrueType fonts included with Windows 3.1 and the TrueType Font Pack are proportional fonts. Windows 3.1和TrueType字体包中包含的所有其他TrueType字体都是比例字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM