格式化消息框中的文本

Formatting text in message box

Result so Far The Data 在一些帮助下我已经走到这一步,但我不太确定如何添加一些组织。

所以下面的代码有效。当我 double-click 一个带有超链接的员工姓名时,会出现一个消息框,其中包含来自另一个 sheet 的信息列表。我想要做的是将该列表中的列 header 放在每个列出的项目的前面。

循环 1 到 12 基本上抓取了不同信息的列表,我正在寻找一种方法来为该列表中的每个项目命名,以使列表更加清晰。 这是我现在的代码

   ****Dim iLoop As Integer
Dim strHolder As String     '<~ string that will hold the details
    For iLoop = 1 To 12          '<~ change this to your requirements
strHolder = strHolder & rEmployee.Offset(0, iLoop - 1).Value & vbNewLine
Next
MsgBox strHolder
End Sub****

我认为您可以在 strHolder 字符串生成器中添加一些简单的额外代码。我不得不对我放在一起的代码进行一些编辑以使其完整,但您应该了解它的一般要点。通过使用 Cells(1, iLoop).value & ": " & ...,它会将列 header 放入消息框中,并在其提取的数据前加上前缀。

Option Explicit

Sub test()

    Dim rEmployee As Range
    Set rEmployee = Cells(2, 1)
    Dim iLoop As Integer
    Dim strHolder As String     '<~ string that will hold the details
    For iLoop = 1 To 12          '<~ change this to your requirements
    strHolder = strHolder & Cells(1, iLoop).Value & ": " & rEmployee.Offset(0, iLoop - 1).Value & vbNewLine
    Next
    MsgBox strHolder

End Sub

继续编码。

干杯克里斯

在您的示例中,要将列标题放在 MsgBox 中第 12 个单元格的内容之前,您可以使用在 sheet 的第 1 行中找到的值。如果您想要第一行以外的其他内容作为前缀,则需要将代码调整为。

For iLoop = 1 To 12          
    strHolder = strHolder & ThisWorkbook.Sheets("Employee and Job List").Cells(1, iLoop) & _
                ": " & vbTab & rEmployee.Offset(0, iLoop - 1).Value & vbNewLine
Next