简体   繁体   English

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

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

Trying to put together a macro that searches each row to see if it contains 7 search terms (see "Warranty:" example below). 尝试组合一个宏来搜索每一行以查看其是否包含7个搜索词(请参见下面的“保修:”示例)。 If the cell starts with one of the phrases (like "Warranty:"), then that cell is pasted in a specific cell (same row but different column) in another worksheet. 如果该单元格以短语之一开头(例如“ Warranty:”(保修:)),则该单元格将粘贴到另一个工作表中的特定单元格(相同的行,但不同的列)中。

Issues: 问题:

  • Had trouble with the macro until I added the select function - I know this slows them down, but I couldn't figure out a way to do this without it 直到我添加了选择功能,宏才遇到麻烦-我知道这会使它们变慢,但是如果没有它,我将找不到解决方法
  • Can't figure out how to get it to loop through all rows 无法弄清楚如何使其遍历所有行
  • Errors if the row doesn't have the word - need it to just keep going through 如果该行没有单词,则会发生错误-需要它以继续浏览

     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 

UPDATE: 更新:

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

To loop through each row in the worksheet: 要遍历工作表中的每一行:

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

Then to copy the values, depending on which cells you need to copy 然后复制值,具体取决于您需要复制的单元格

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

For it to continue when you have an error, you can tell it to resume 要让它在出现错误时继续运行,可以告诉它恢复

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

Using the code in your update, the following code should work for you. 使用更新中的代码,以下代码将为您工作。

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

A slightly more elegant way that Quicksilver alluded to is: 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