【问题标题】:Output an Array or List to String将数组或列表输出到字符串
【发布时间】:2013-06-11 13:27:39
【问题描述】:

我编写了以下代码(在 Outlook 中的 VBA 中)尝试将我的列表输出到字符串。这种方式可能并不完美;它可以编译,但我还没有测试过;无论如何,我想知道你们是否有任何关于如何在 VBA 或 VB.NET 中将数组或列表输出到字符串的替代想法。这种方式对我来说只是一种陪审团操纵,我无法找到任何“标准”或“官方”的方式来完成这项工作。

我认为这个代码可以工作,所以我没有必要有替代方案,但我对此很陌生,我想从这个项目中尽可能多地学习。感谢您提供的任何帮助!

    Dim nplct As Integer
    Dim npl As Integer
    Dim strNotpresentList As String
    strNotpresentList = ""
    npl = 0
    nplct = notpresentList.Count
    For Each Computer In notpresentList
        If npl <> nplct Then

            If strNotpresentList = "" Then
                    strNotpresentList = notpresentList(npl)
                    npl = npl + 1
                Else
                    strNotpresentList = strNotpresentList & ", " & notpresentList(np1)
                    npl = npl + 1
                End If


            Else
                strNotpresentList = strNotpresentList & ", " & notpresentList(np1) & "."
            End If

    Next Computer

【问题讨论】:

    标签: arrays string list vba


    【解决方案1】:

    VBA 如下:

    Dim strNotPresentList as String
    strNotPresentList = Join(notpresentList, ",") & "."
    

    或者如果你想在同一行:

    Dim strNotPresentList As String: strNotPresentList = Join(notpresentList, ",") & "."
    

    对于 VB.Net

    Dim strNotPresentList As String = String.Join(", ", notpresentList) + "."
    

    【讨论】:

    • 谢谢!那么,这究竟取代了什么?我可以用strNotPresentList = Join(notpresentList, ",") &amp; "." 替换strNotpresentList = strNotpresentList &amp; ", " &amp; notpresentList(np1) &amp; "." 吗?
    • 如果您在开头添加一个 Dim ,它可以替换您粘贴的所有代码。
    • @TimB 哇。这是出乎意料的。谢谢!
    • 这在 VBA 中不起作用(代码不会编译),但在 VB.Net 中可以。我正在更新我的示例以展示如何定义为 String 并尽可能简短地为两者分配。
    【解决方案2】:

    在 VB.NET 中:

    strNotpresentList = String.Join(", ", notpresentList)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多