繁体   English   中英

VBA-更新查找功能以遍历行并在值不存在的情况下继续

[英]VBA - update find function to loop through rows and move on if value isn't there

尝试组合一个宏来搜索每一行以查看其是否包含7个搜索词(请参见下面的“保修:”示例)。 如果该单元格以短语之一开头(例如“ Warranty:”(保修:)),则该单元格将粘贴到另一个工作表中的特定单元格(相同的行,但不同的列)中。

问题:

  • 直到我添加了选择功能,宏才遇到麻烦-我知道这会使它们变慢,但是如果没有它,我将找不到解决方法
  • 无法弄清楚如何使其遍历所有行
  • 如果该行没有单词,则会发生错误-需要它以继续浏览

     Sub FindTest() Worksheets("Macro").Range("1:1").Find(What:="Warranty: ", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True).Copy 'Cell begins with "Warranty:" but text following varies Sheets("CSV Upload").Select Sheets("CSV Upload").Range("J1").Select ActiveSheet.Paste End Sub 

更新:

Sub FindTest()

Dim Macro As Worksheet: Set Macro = Sheets("Macro")
Dim CSV As Worksheet: Set CSV = Sheets("CSV Upload")

'On Error Resume Next
For R = 1 To Macro.UsedRange.Rows.Count
    Set rng = Macro.Rows(R)

Dim FindRange As Range: Set FindRange = rng.Find(What:="Warranty:", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)

'FindRange.Copy CSV.Range("J1")
'CSV.Cells(1, J) = Macro.Cells(FindRange)

Next

'On Error GoTo 0

End Sub

要遍历工作表中的每一行:

Dim ws As Worksheet: Set ws = Sheets("Macro")
Dim csv_upload As workseet: Set csv_upload = Sheets("CSV Upload")

For r = 1 To ws.UsedRange.Rows.Count
    Set rng = ws.Rows(r)
    rng.Find(What:="Warranty: ", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)

    ...
Next

然后复制值,具体取决于您需要复制的单元格

csv_upload.cells(dest_row, dest_col) = ws.cells(orig_row, orig_col)

要让它在出现错误时继续运行,可以告诉它恢复

On Error Resume Next
' potential for error to be raised
' Don't use this unless you know you are going to get a specific
' error and know there are no unintended consequences of ignoring it.
On Error GoTo 0

使用更新中的代码,以下代码将为您工作。

Sub FindWarranty()

    Dim Macro As Worksheet: Set Macro = Sheets("Macro")
    Dim CSV As Worksheet: Set CSV = Sheets("CSV Upload")
    Dim rng As Range, FindRange As Range
    Dim Phrase As String

    Phrase = "Warranty:"

    For r = 1 To Macro.UsedRange.Rows.Count

        Set rng = Macro.Rows(r)
        Set FindRange = rng.Find(What:=Phrase, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=True)

        If Not FindRange Is Nothing Then
            ' Set destination cell to what you need it to be
            c = 1
            CSV.Cells(r, c) = FindRange
        End If

    Next

End Sub

Quicksilver提到的一种更优雅的方式是:

Sub FindWarrantys()

    Dim Macro As Worksheet: Set Macro = Sheets("Macro")
    Dim CSV As Worksheet: Set CSV = Sheets("CSV Upload")
    Dim FoundCell As Range, FirstAddr As String
    Dim Phrase As String, c As Integer

    Phrase = "Warranty:"

    ' Find the first occurrence. The after variable is set to the
    ' last cell so that it will start searching from the beginning.
    Set FoundCell = Macro.UsedRange.Find(what:=Phrase, _
        after:=Macro.UsedRange.Cells(Macro.UsedRange.Cells.Count))

    ' Save the address of the first occurrence to prevent an infinite loop
    If Not FoundCell Is Nothing Then
        FirstAddr = FoundCell.Address
    End If

    ' Loop through all finds
    Do Until FoundCell Is Nothing

        c = 1 ' Adjust for logic to determine which column
        CSV.Cells(FoundCell.Row, c) = FoundCell

        ' Find the next occurrence
        Set FoundCell = Macro.UsedRange.FindNext(after:=FoundCell)

        ' Break if we're back at the first address
        If FoundCell.Address = FirstAddr Then
            Exit Do
        End If

    Loop

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM