Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
Arrays 如何将特定单元格存储到数组中?vba excel_Arrays_Vba_Excel - Fatal编程技术网

Arrays 如何将特定单元格存储到数组中?vba excel

Arrays 如何将特定单元格存储到数组中?vba excel,arrays,vba,excel,Arrays,Vba,Excel,我有这个: A列 第1行:str1;str2;str3 第2行:str4;str5;str6 第3行:str7;str8;str9 ……… 罗恩:斯特恩;strn;斯特恩 下面的代码在A列中找到“;”字符: Range("A:A").Find(What:=";", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, M

我有这个:
A列
第1行:str1;str2;str3
第2行:str4;str5;str6
第3行:str7;str8;str9
………
罗恩:斯特恩;strn;斯特恩

下面的代码在A列中找到“;”字符:

     Range("A:A").Find(What:=";", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
     xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
    , SearchFormat:=False).Activate
我想将所有行(从A列开始,包含分号字符)放入一个数组中。我尝试使用SET,如下所示:

       dim r as Variant
       Set r = Range("A:A").Find(What:=rngsearch, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=_  
       xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,MatchCase:=False _
       , SearchFormat:=False).Activate
Sub GetSemicolonData()
    Dim rngCell               As Excel.Range
    Dim asValues()            As String
    Dim lngCount              As Long
    Dim x                     As Long


    With Range("A1").CurrentRegion.Columns(1)
        .AutoFilter field:=1, Criteria1:="*;*"
        lngCount = .SpecialCells(xlCellTypeVisible).Count
        If lngCount > 1 Then
            x = 1
            ' exclude header row
            ReDim asValues(1 To lngCount - 1)
            For Each rngCell In .SpecialCells(xlCellTypeVisible)
                If rngCell.Row > 1 Then
                    ' load value into array
                    asValues(x) = rngCell.Value
                    x = x + 1
                End If
            Next rngCell
        End If
    End With
End Sub
…但不起作用。它是运行时错误“13”,类型不匹配

我需要这个数组(包含所有带分号的单元格),因为我想提取字符串(从str1到strn)并将它们分隔在不同的行中


有人能帮我吗?也许有人对我如何做到这一点有另一个想法?

可能有更有效的方法来做到这一点,我个人更希望避免提及整个专栏,但这应该能满足您的期望:

Sub test()

Dim ws As Worksheet
Dim rng As Range
Dim cel As Range
Dim strTmp As String
Dim arrFinal As Variant

Set ws = Sheets("Sheet1")
Set rng = ws.Range("A:A")

' Loop through all cells in column A
For Each cel In rng.Cells
    ' Is there a semicolon character in the cell?
    If InStr(1, cel.Value, ";") > 0 Then
        ' Add the cell value to strTmp and add a _
            semicolon at the end to separate this _
            row from the next row
        strTmp = strTmp & cel.Value & ";"
    End If
Next cel

' Split strTmp into an array
arrFinal = Split(strTmp, ";")

End Sub

最终结果是一个名为arrFinal的数组,它包含分号字符之间的所有字符串

       dim r as Variant
       Set r = Range("A:A").Find(What:=rngsearch, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=_  
       xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext,MatchCase:=False _
       , SearchFormat:=False).Activate
Sub GetSemicolonData()
    Dim rngCell               As Excel.Range
    Dim asValues()            As String
    Dim lngCount              As Long
    Dim x                     As Long


    With Range("A1").CurrentRegion.Columns(1)
        .AutoFilter field:=1, Criteria1:="*;*"
        lngCount = .SpecialCells(xlCellTypeVisible).Count
        If lngCount > 1 Then
            x = 1
            ' exclude header row
            ReDim asValues(1 To lngCount - 1)
            For Each rngCell In .SpecialCells(xlCellTypeVisible)
                If rngCell.Row > 1 Then
                    ' load value into array
                    asValues(x) = rngCell.Value
                    x = x + 1
                End If
            Next rngCell
        End If
    End With
End Sub

您还可以使用Dave方法的一种变体,将所有数据加载到一个数组中,并处理这些数据-它应该比逐单元格读取更快。

为什么不使用带“;”的自动筛选作为标准,然后在可见单元格中循环?是否要像这样添加arr(0)=str1,arr(1)=str2,arr(3)=str3,arr(n)=strn?是的,@JagadishDabbiru@Rory,我必须使用VBA代码..我的意思是使用VBA代码-我没想到你会手工操作。:)多谢各位!这就是我需要的。但是如果我需要在分号之前或之后的字符串(我的意思是把字符串放在新行中),我必须操作拆分行代码,对吗?对不起,我不知道你的意思?你的想法也很棒!但没错,Dave的代码处理速度更快。无论如何谢谢你!