Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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 迭代使用搜索函数找到的字符串_Excel_Vba - Fatal编程技术网

Excel 迭代使用搜索函数找到的字符串

Excel 迭代使用搜索函数找到的字符串,excel,vba,Excel,Vba,我一直在寻找一种方法来创建一个宏,该宏涉及通过find函数使用循环,该函数类似于: With ActiveSheet For i = 1 To LastEntity Cells.Find(What:="ENTITY(i)", After:=ActiveCell, LookIn:=xlFormulas, _ MatchCase:=False, SearchFormat:=False).Activate SOME OPERATION Ne

我一直在寻找一种方法来创建一个宏,该宏涉及通过find函数使用循环,该函数类似于:

With ActiveSheet
    For i = 1 To LastEntity
    Cells.Find(What:="ENTITY(i)", After:=ActiveCell, LookIn:=xlFormulas, _
               MatchCase:=False, SearchFormat:=False).Activate
    SOME OPERATION
    Next i
此处“实体(I)”旨在模拟以下代码用于打开多个文件的过程:

    For i = 1 To .FoundFiles.Count
        Set wb = Workbooks.Open(Filename:=.FoundFiles(i))
        SOME OPERATION
    Next i
我的问题是:如何正确地将此功能扩展到find函数?我确信我上面写的方式是不正确的,但我也确信一定有办法做到这一点。任何帮助都将不胜感激

编辑:

如果需要双回路,是否可以进行以下更改

Sub searchRangeAndDoStuff(ByVal ENTITY As String)

Dim xlRange As Excel.Range, varA As Variant, i As Long, x As Long

x = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

Set xlRange = ActiveSheet.Range(Cells(1, 1), Cells(x, 1))

set varA = xlRange.value

For i = LBound(varA, 1) To UBound(varA, 1)
    If InStr(1, varA(i, 1), ENTITY, vbTextCompare) Then
    Copy ENTITY
        For j = Beginning To End 
            If InStr(1, varA(j, 1), ITEM, vbTextCompare) Then
            Move cells down
            Move up one cell
            Paste ENTITY
            End If
        Next j
     End If
 Next i          

End Sub

此子项采用名为ENTITY的搜索值。它获取列A中的最后一行数据,并将A1:A&x分配给一个变量,这使我能够非常快速有效地遍历它。默认情况下,变量将有两个维度,因此最好指定要循环的变量(以帮助您记住它是二维的,如果没有其他变量的话)

如果您需要保留行号,并且您的范围并不总是从顶部的同一偏移开始,您可以使用

Dim xlCell as Excel.Range

For Each xlCell in xlRange
'if in string, or if string compared, do something
'or assign the values and their row numbers to a 2d string array (clng() the row
'numbers), so you can continue to work with arrays
Next xlCell
下面的内容非常混乱,如果您有很多重复的值,或者“粘贴到”范围与“复制自”范围相同,则会出现很多奇怪的行为。但是,如何纠正这一点将取决于您的实际项目(我已经对如何管理其中一些项目提出了一些建议)。它演示了如何执行类似于您在编辑中建议的操作:

Sub searchRangeAndDoStuff(ByVal ENTITY As String, ByRef CheckRange As Excel.Range)

Dim xlRange As Excel.Range, varA As Variant, x As Long
Dim xlCell As Excel.Range, xlCell1 As Excel.Range

x = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

Set xlRange = ActiveSheet.Range(Cells(1, 1), Cells(x, 1))

'please remember that if the check range is the same as the target range
'you are going to get some very wierd behaviour

For Each xlCell In xlRange
    'StrComp matches the full string, InStr simply returns true if a substring is
    'contained within the string - I don't know which one you want, but StrComp sounded
    'closer
    If StrComp(xlCell.Value, ENTITY, vbTextCompare) = 0 Then
    varA = xlCell.Value
        For Each xlCell1 In CheckRange
            'if not xlcell.row = xlcell1.row then
                If StrComp(xlCell.Value, xlCell1.Value, vbTextCompare) = 0 Then
                    xlCell1.Insert xlDown
                    xlCell1.Offset(-1, 0).Value = varA
                End If
            'end if
        Next xlCell1
        'xlCell.Delete
    End If
Next xlCell


End Sub

VBA中的字符串插值看起来是这样的:
What:=“ENTITY(&i&)”
,但我认为这不是您实际想要做的。如果
ENTITY
充满了字符串,您可以跳过引号,只需写
What:=ENTITY(i)
@Dan我应该说明ENTITY是要找到的字符串。我曾希望类似于ENTITY(I)的符号可以允许.Find()搜索过程中出现的每个实体实例。什么:=“实体(“&I&”)仍然有效吗?如果您需要使用字符串插入变量,这样字符串“sam(I)”将被读取为“sam4”,其中I=4,那么是的,正如Dan所说,您可以简单地使用要断开字符串,请在中添加变量(&D)。如果实体是一个字符串变量,您首先需要创建一个字符串数组/集合等,以使i是某个特定字符串的索引,或者如果您只想将i标记到实体字符串上,则它是实体&i(显然,字符串的变量名不应放在引号中)@user3100456第二个案例似乎更符合我的要求。实体的每个实例都是一个字符串,但有许多实例需要排序,宏需要迭代。当我考虑这个问题时,我正在考虑Ctrl+F find函数,.find()方法是否创建了一个可以迭代的顺序?我会尝试简单地将要搜索的范围转换为一个数组并进行迭代。或者,您可以尝试使用过滤器。我会把阵列版本放在一起给你看。谢谢!我上面的修改也有意义吗?我需要研究填补这些空白所需的确切代码,但我只想确保结构合理。如果数据总是从顶部开始在同一行中,就可以了。然后您的数组索引将匹配原始行号(可能在某个恒定偏移量处)。您可以使用一个简单的
.Insert Shift:=xlDown
在相关行(现在占用该行索引)上创建一个新单元格,并用数组中的值填充它。您需要记住,数组在某种程度上独立于它从中提取的实际范围,对数组的更改不会更改单元格中的值。数组只是处理这些值的一种更快的方法。再次感谢!独立于范围的数组有什么含义?我对它的理解是混乱的,正如它向我建议的那样。Insert Shift:=xlDown不会像你建议的那样工作,因为它正在更改值。我在我的帖子中更新了代码,以反映你的要求。请让我重新迭代-您需要修改此代码以使其适合您。我建议用F8单步执行代码,将鼠标悬停在变量值上观察变量值,并逐步查看宏对实际数据集所做的操作。我进一步修改了代码,并建议如何管理搜索插入的相同范围时可能出现的问题(这些问题已注释掉)
Sub searchRangeAndDoStuff(ByVal ENTITY As String, ByRef CheckRange As Excel.Range)

Dim xlRange As Excel.Range, varA As Variant, x As Long
Dim xlCell As Excel.Range, xlCell1 As Excel.Range

x = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

Set xlRange = ActiveSheet.Range(Cells(1, 1), Cells(x, 1))

'please remember that if the check range is the same as the target range
'you are going to get some very wierd behaviour

For Each xlCell In xlRange
    'StrComp matches the full string, InStr simply returns true if a substring is
    'contained within the string - I don't know which one you want, but StrComp sounded
    'closer
    If StrComp(xlCell.Value, ENTITY, vbTextCompare) = 0 Then
    varA = xlCell.Value
        For Each xlCell1 In CheckRange
            'if not xlcell.row = xlcell1.row then
                If StrComp(xlCell.Value, xlCell1.Value, vbTextCompare) = 0 Then
                    xlCell1.Insert xlDown
                    xlCell1.Offset(-1, 0).Value = varA
                End If
            'end if
        Next xlCell1
        'xlCell.Delete
    End If
Next xlCell


End Sub