vba - 文本框 VBA 中的固定宽度列

标签 vba excel

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

这会产生

  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

我想要它产生

  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

所以我认为我需要具有定义宽度的固定列,并且所有内容都居中。数字永远不会超过 4 位

我该如何解决这个问题?

最佳答案

您可以使用 Format@ 符号来填充并对齐右侧的每个值:

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

或者在左边:

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

并通过提供字符数:

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

关于vba - 文本框 VBA 中的固定宽度列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771468/

相关文章:

python - Pandas 的表现就像两个有条件的数据帧的计数一样

excel - 在 Excel 中向 ssn 样式编号添加条件零

vba - `Is` 对于指向同一单元格的两个范围变量,运算符不返回 true

excel - 如何使用 Excel VBA 确定整个工作簿中的任何单元格中是否存在错误

vba - 如何知道vba(excel)中填充的最后一行?

vba - 使用 VBA 从多个文件中提取表格到 Excel

vba - Excel vba Union方法复制重叠的单元格

php - 使用 PHP 解析 Excel XML

excel - 如何将实际的 Excel 公式(而不是结果)转换为文本字符串

vba - Excel 加载项将文件名更改为加载项的名称,尽管它不应该