6

I am new to Excel VBA and have written Vba code to loop through cells and get their values.After that do some processing and if it matches a certain criteria append them to a list with line break. Do this until all rows are done. This works perfectly fine and end result is as image below:

enter image description here

The thing is i want the formatting to look neat , so is there a way that the gap in between the text is same on all rows so it looks neat. The way i add the rows is :

          Dim tmpLine
          tmpLine = line & "      " & dateVal
          mainMessage = mainMessage & tmpLine & vbNewLine

Not sure if its the perfect way but that is what i know...

4

4 回答 4

15

我明白了,也许它会解决你的问题:

Sub msgBoxTest()

    MsgBox "inininegefedf" & vbTab & "1234" & vbCr & _
            "asdcainininegefedf" & vbTab & "1234" & vbCr & _
            "inininegefedf" & vbTab & "1234afsad", vbCritical

End Sub

结果如下:

结果

于 2013-11-20T21:58:53.327 回答
3

我已经为此实现了一个尽可能完整的解决方案,并为此付出了相当大的努力。当从FmsgBox.xlsm工作簿传输clsFmsgBoxfrmFmsgBoxmodFmsgBox时,下面的代码会导致这种消息框可以用相对较少的精力进行管理。上面的工作簿也提供了编码示例。

 With cFmsgBox
    .Title = "Message Box supporting formatted text. Example 1"
    .Msg = "This is the first " & _
     .b("Test ") & "message spanning over several lines and paragraphs. The default margins, spaces, and font size had been used. " & _
                   "No need to say that the form width had been adjusted manually in order to have an optimum appearance." & _
     .Lf & _
     .Lf & "The formats " & _
     .b("bold") & ", " & _
     .u("underline") & ", and " & _
     .i("italic ") & "and may be combined with any of the colours " & _
     .b(.i(.u(.red("red")))) & ", " & _
     .b(.i(.u(.blue("blue")))) & ", and " & _
     .b(.i(.u(.green("green")))) & "." & _
     .Lf & .Lf & _
           "5 different links may be included in the message text, either in the full form like " & _
     .link("www.google.com") & " or with a friendly name like " & .link("www.google.com", "google.com") & ", which masks the url behind it." & _
     .Lf & _
     .Lf & _
        "Also it shows 2 of the 6 possible reply buttons and that they may contain any text. Since the number of lines is maximized to 3 their width will be adjusted " & _
        "automatically - but will remain the same for all buttons. The string returned by the display call is identical with the string of the clicked reply button."

    .Reply1 = "Click this reply to continue with the next example"
    .Reply2 = "Click this reply to finish with the Message Box solution's features"
    .Dsply 318

    If .Reply = .Reply1 Then Example2
End With

消息显示在专用的用户窗体中,为每个格式化的文本字符串动态创建标签和动态创建(最多 6 个)命令按钮。格式化消息的关键是格式属性 .b("xxx") 表示粗体,.i("xxx") 表示斜体等等,它们都可以像 .b(.i("xxx")) 一样嵌套例如,获取斜体、粗体文本。

或者,可以为消息文本提供类似 RTF/HTML 格式的标签,而不是串联字符串,其中打开/关闭标签字符默认为 {},但也可以更改为 <>。例如:“{b}bold{/b) {i}italic{/i}。” 将显示: 斜体

于 2015-11-22T21:42:50.573 回答
1

实现 string.Format() - 对于这个 msgbox 来说有点矫枉过正,但可重用性是无穷无尽的:

在 VB6 中实现 String.Format()

类似的东西msg = StringFormat("{0}\n{1,-10}{2:cMM/DD/YYYY}", msg, line, dateVal)应该可以工作。

或者,专注于实现的这一部分:

alignmentPadding = Abs(CInt(alignmentSpecifier))
If CInt(alignmentSpecifier) < 0 Then
    'negative: left-justified alignment
    If alignmentPadding - Len(formattedValue) > 0 Then _
        formattedValue = formattedValue & _
            String$(alignmentPadding - Len(formattedValue), PADDING_CHAR)
Else
    'positive: right-justified alignment
    If alignmentPadding - Len(formattedValue) > 0 Then _
        formattedValue = String$(alignmentPadding - Len(formattedValue), PADDING_CHAR) & formattedValue
End If

PADDING_CHAR“”空格和alignmentSpecifier您需要的填充量在哪里。

换句话说,填充line到 20 个字符,然后附加日期:

tmpLine = line & String$(20 - Len(line), " ") & dateVal & vbNewline

..为我工作:

?"'abc12" & string$(20-len("'abc12"), " ") & "12/12/2004"
'abc12              12/12/2004
?"'abc1234" & string$(20-len("'abc1234"), " ") & "12/12/2004"
'abc1234            12/12/2004
?"'abc1234456" & string$(20-len("'abc1234456"), " ") & "12/12/2004"
'abc1234456         12/12/2004

更新

似乎问题不在于消息字符串本身,而是因为 : 中使用的字体MsgBox对我有用,因为我使用即时窗格来获得快速结果,并且使用等宽字体显示(所有字符都是宽度相同)。

消息框

我建议您创建一个固定宽度的快速表单Label,使用“Courier New”或“Consolas”之类的字体......或者只是在可变长度部分前面显示日期。

于 2013-08-29T23:27:47.177 回答
0

我建议使用“用户表单”。在 VBA 编辑器中的 VBA 项目下,右键单击并选择“插入/用户表单”(或使用顶部菜单“插入/用户表单”)

从那里,您会在“工具箱”(或单击“查看/工具箱”)中看到许多 ActiveX 控件,它们可以帮助您构建您所追求的内容:听起来您正在寻找一个“列表框”,您应该在工具箱中找到(将鼠标悬停在项目上,“列表框”将显示为工具提示)

选择列表框后,您可以在“属性”窗口中设置列数加上列宽(如果看不到,请单击“查看/属性窗口”)

看看你是怎么做的,网上有很多关于 VBA 用户表单和列表框的好信息,或者很乐意回答更多问题。

干杯,西

[编辑] 只是想到了一些我应该让你开始的代码(从我写的一个小工具将 Office 2010 引用转换为 Office 2003 引用):在我的主代码模块中,我刚刚得到:

Sub FixReferences() 'Ctrl-Shift-F

    ufWorkbooks.Show

    If ufWorkbooks.blCancel = False Then
        With ufWorkbooks.lbWorkbooks
            For intindex = 0 To .ListCount - 1
                If .Selected(intindex) Then
                    SwapReferences Workbooks(.List(intindex))
                End If
            Next
        End With
    End If
End Sub

这会在 ufWorkbooks.Show 行中加载我的用户表单(称为 ufWorkbooks)。此时执行传递到该表单:当执行返回时,检查表单中的一个变量以查看是否已推送取消,然后您可以看到我在表单上的列表框的每个项目上运行了一个函数称为“lb 工作簿”。我的用户表单中的代码如下所示:

Public blCancel As Boolean

Private Sub cbCancel_Click()
    blCancel = True
    Me.Hide
End Sub

Private Sub cbOK_Click()
    Me.Hide
End Sub

Private Sub UserForm_Activate()
    blCancel = False

    FillWorkbooks
End Sub

Sub FillWorkbooks()
    Dim wbBook As Workbook

    lbWorkbooks.Clear

    For Each wbBook In Workbooks
        lbWorkbooks.AddItem wbBook.Name
    Next

End Sub

在这里你可以看到我使用不同的“事件”来触发某些代码。在底部,您可以看到我的“FillWorkbooks”子首先清除我的列表框(我命名为 lbWorkbooks),然后我正在向其中添加项目。这适用于一列列表框。在您的情况下,您可能想要使用 .AddItem 然后 .List(0, 1) = "whatever" (列表框中的第 0 行,第 1 列)。我不记得 AddItem 是否会在顶部添加一个空白行供您使用 .List(0... 引用,或者您是否需要添加一个计数器 .AddItem 然后 .List(i...

或者,您可以使用 Listbox.ListFillRange 将电子表格中的一系列单元格用作列表框的数据源。

于 2013-08-29T23:34:52.200 回答