字符串数组到新工作表

时间:2016-06-27 18:40:36

标签: arrays excel vba excel-vba

下面的代码执行并将原始单元格拆分为新数组,但我无法将新的字符串数组移动到新工作表。抛出错误的行是varHorizArray(1,intCol).Select。如何更改此代码以将数组直接移动到新工作表?我想在新数组中搜索单词。



Sub SplitWithFormat()
    Dim R As Range, C As Range
    Dim i As Long, V As Variant
    Dim varHorizArray As Variant
    Dim rge As Range
    Dim intCol As Integer
            
    
Set R = Range("d1", Cells(Rows.Count, "d").End(xlUp))
For Each C In R
    With C
        .TextToColumns Destination:=.Offset(0, 1), DataType:=xlDelimited, _
        consecutivedelimiter:=True, Tab:=False, semicolon:=True, comma:=False, _
        Space:=True, other:=True, Otherchar:=vbLf

        Set rge = Selection
        varHorizArray = rge
        .Copy
        Range(.Offset(0, 1), Cells(.Row, Columns.Count).End(xlToLeft)).PasteSpecial xlPasteFormats
    End With
Next C

Application.CutCopyMode = False


For intCol = LBound(varHorizArray, 2) To UBound(varHorizArray, 2)
        Debug.Print varHorizArray(1, intCol)
    Next intCol
    
Sheets("Sheet1").Activate
varHorizArray(1, intCol).Select
ActiveSheet.Paste

End Sub
​




1 个答案:

答案 0 :(得分:1)

要直接将数组转储到工作表,只需设置一个与数组具有相似维度的范围,并将该数组指定给该范围。

Sheet1.Activate
Sheet1.Range(Cells(1, LBound(varHorizArray, 2)), Cells(UBound(varHorizArray), UBound(varHorizArray, 2))) = varHorizArray