【问题标题】:Cell Split Excel VBA using Multi-dimensional Array使用多维数组的单元格拆分 Excel VBA
【发布时间】:2016-03-06 04:46:31
【问题描述】:

我正在尝试使用 VBA 拆分多行文本框并将输出粘贴到 MS Excel 的下一张工作表中,我在下面找到了一段代码,它可以工作:

    Dim Str As String, a
    Dim cnt As Integer
    Dim w()

    Str = xmlRequestTextBox.Value
    a = Chr(10)
    cnt = UBound(Split(Str, a))
    MsgBox (Str)
    MsgBox (a)
    MsgBox (cnt)
    ReDim w(1 To cnt + 1, 1 To 1)

    For i = 0 To cnt
     w(i + 1, 1) = Split(Str, Chr(10))(i)
    Next i

    Sheet2.range("A1").Resize(i, 1) = w
    Sheet2.Cells.Replace Chr(13), " "

现在我的问题是当我尝试修改它并将其更改为一维数组时,它只输出数组第一个索引的值。为什么数组必须是多维的?提前谢谢你。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    对于多行文本,您需要使用由Split() 函数返回的二维数组,其中第一个维度通常是指行(行),第二个索引是指这些行中的单个单词。希望这可能会有所帮助。

    【讨论】:

    • 嗨,Alex,谢谢你提供的信息,你能解释一下为什么 Split 有 (i) 吗?
    【解决方案2】:

    使用拆分的示例

    Option Explicit
    Private Sub CommandButton1_Click()
    
    Dim Str As String
    Dim cnt As Integer
    Dim w As Variant
    
    Str = Me.xmlRequestTextBox.Value
    w = Split(Str, Chr(10))
    cnt = UBound(w)
    MsgBox (Str)
    MsgBox (Chr(10))
    MsgBox (cnt)
    
    ' suppose the multi-line textbox value is:
    ' first line : 1 2 3
    ' second line: 4 5 6
    ' third line:  7 8 9
    With ThisWorkbook.Sheets("split")
        .Range("A1").Resize(cnt + 1) = w(1) ' writes "4 5 6" in A1:A3 (i.e. the 2nd element of w)
        .Range("B1").Resize(cnt + 1) = Split(w(0), " ")(2) ' writes "3" in B1:B3 (i.e. the third element of the 1st element of w)
        .Cells.Replace Chr(13), " "
    End With
    
    End Sub
    

    我假设有一些用户窗体带有一个名为“xmlRequestTextBox”的多行文本框和一个名为“CommandButton1”的按钮,其点击事件会启动你的那段代码

    【讨论】:

      【解决方案3】:

      要回答您的问题,数组确实不是必须是多维的。但是,Excel 假定一维数组将是水平的,即在单行 中填充。因此,如果您要写入电子表格中的垂直范围(多行单列中),它将采用第一个元素(列)并将其写入列中的每个单元格。例如,假设你有一个这样的数组:

      MyArray = Array(1, 2, 3, 4, 5)
      

      此代码会将 MyArray 的第一个元素写入A列中的垂直范围:

      Set MyRange = Range("A1").Resize(5, 1)    ' Cells A1:A5
      MyRange.Value = MyArray
      

      结果:

          A | B | C | D | E
        +---+---+---+---+---+
      1 | 1 |   |   |   |   |
      2 | 1 |   |   |   |   |
      3 | 1 |   |   |   |   |
      4 | 1 |   |   |   |   |
      5 | 1 |   |   |   |   |
      

      如果您将数组写入单行中的水平范围,那么它将显示整个数组

      Set MyRange = Range("A1").Resize(1, 5)    ' Cells A1:E1
      MyRange.Value = MyArray
      

      结果:

          A | B | C | D | E
        +---+---+---+---+---+
      1 | 1 | 2 | 3 | 4 | 5 |
      2 |   |   |   |   |   |
      3 |   |   |   |   |   |
      4 |   |   |   |   |   |
      5 |   |   |   |   |   |
      

      但是,我假设您实际上想要将数组写入 单列 中的垂直 范围。您可以通过对数组使用 Application.WorksheetFunction.Transpose 方法来完成此操作。这会将数组的每个元素写入 A 列中的行:

      Set MyRange = Range("A1").Resize(5, 1)    ' Cells A1:A5
      MyRange.Value = Application.Transpose(MyArray)
      

      结果:

          A | B | C | D | E
        +---+---+---+---+---+
      1 | 1 |   |   |   |   |
      2 | 2 |   |   |   |   |
      3 | 3 |   |   |   |   |
      4 | 4 |   |   |   |   |
      5 | 5 |   |   |   |   |
      

      有关这方面的更多信息,请参阅 Chip Pearson 的页面VBA Arrays And Worksheet Ranges

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多