Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用多维数组拆分Excel VBA单元_Vba_Excel - Fatal编程技术网

使用多维数组拆分Excel VBA单元

使用多维数组拆分Excel VBA单元,vba,excel,Vba,Excel,我正在尝试使用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 (c

我正在尝试使用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), " "

现在我的问题是,当我试图修改它并将其更改为一维数组时,它只输出数组第一个索引的值。为什么阵列必须是多维的?高级版谢谢。

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

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

使用拆分的示例

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

我假设有一个UserForm,它有一个名为“xmlRequestTextBox”的多行文本框和一个名为“CommandButton1”的按钮,它的单击事件启动您的那段代码,这是一个使用split的示例

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”的按钮,它的点击事件启动您的那段代码来回答您的问题,数组不必是多维的。但是,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 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 5 |   |   |   |   |
如果将数组写入一行中的水平范围,则它将显示整个数组:

结果:

    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 1 |   |   |   |   |
3 | 1 |   |   |   |   |
4 | 1 |   |   |   |   |
5 | 1 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 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 | 1 |   |   |   |   |
3 | 1 |   |   |   |   |
4 | 1 |   |   |   |   |
5 | 1 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 5 |   |   |   |   |

有关这方面的更多信息,请参见Chip Pearson的页面上的回答您的问题,数组不必是多维的。但是,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 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 5 |   |   |   |   |
如果将数组写入一行中的水平范围,则它将显示整个数组:

结果:

    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 1 |   |   |   |   |
3 | 1 |   |   |   |   |
4 | 1 |   |   |   |   |
5 | 1 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 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 | 1 |   |   |   |   |
3 | 1 |   |   |   |   |
4 | 1 |   |   |   |   |
5 | 1 |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 | 2 | 3 | 4 | 5 |
2 |   |   |   |   |   |
3 |   |   |   |   |   |
4 |   |   |   |   |   |
5 |   |   |   |   |   |
    A | B | C | D | E
  +---+---+---+---+---+
1 | 1 |   |   |   |   |
2 | 2 |   |   |   |   |
3 | 3 |   |   |   |   |
4 | 4 |   |   |   |   |
5 | 5 |   |   |   |   |

有关这方面的更多信息,请参见Chip Pearson的网页,网址为

Hi Alex谢谢你提供的信息,请你解释一下为什么Split有一个(i)?Hi Alex谢谢你提供的信息,请你解释一下Split为什么有一个(i)?