在消息框中Excel VBA中的文本格式

问题描述 投票:4回答:4

我是新来的Excel VBA,并通过细胞书面VBA代码中循环,并得到他们的values.After是做一些处理,如果符合一定的标准,将它们附加与断行名单。直到所有行完成执行此操作。这工作完全正常和最终结果如下图:

事情是我想要的格式看起来整齐,那么有没有一种方式,在文字之间的差距是所有行一样的,所以它看起来整洁。我添加行的方法是:

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

不知道它的最佳方式,但是这就是我知道的...

vba excel-vba excel
4个回答
12
投票

我得到这个,也许它会解决你的问题:

Sub msgBoxTest()

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

End Sub

下面是结果:


3
投票

我实现了这个尽可能完整的解决方案,并花了相当大的努力吧。当从FmsgBox.xlsm工作簿转移clsFmsgBox,frmFmsgBox和modFmsgBox下面的代码导致this kind of message box可能与相当小的努力来管理。工作簿以上提供编码的例子为好。

 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一个concatenaed串,由此打开/关闭标记字符默认为{},但也可以被改变为<>。作为一个例子: “{B} {粗体/ b)的{I}斜体{/ I}”。显示:粗斜体。


1
投票

实施的String.format() - 有点矫枉过正仅这一项的msgbox,但可重用性是相当不已:

Implementing String.Format() in VB6

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

..works对我来说:

?"'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

UPDATE

看来问题不在于消息字符串本身,而是因为在MsgBox中使用的字体:什么工作对我来说,工作是因为我使用的立即窗格中取得立竿见影的效果,而这与等宽字体(所有的字符显示是相同的宽度)。

我建议你创建一个快速的形式使用,如“宋体”或“索拉”的字体固定宽度Label ...或者只是在可变长度部分的前面显示日期。


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)。执行通过这种形式在这一点:当执行回来,从表单变量进行检查,看是否取消已推,然后就可以看到我有在叫我的列表框的形式,在每个项目上运行的功能“lbWorkbooks”。在我的用户窗体的代码如下所示:

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)=“无所谓”(行0在列表框中,列1)。我不记得的AddItem将在顶部添加一个空白行供您使用的.List(0参考...或者,如果你需要添加计数器.AddItem随后的.List(我...

或者您可以使用Listbox.ListFillRange使用的单元格区域从您的电子表格作为您的列表框的数据源。

© www.soinside.com 2019 - 2024. All rights reserved.