首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据横向单元格中的值,根据Sheet2副本中的范围搜索范围Sheet2到Sheet3

根据横向单元格中的值,根据Sheet2副本中的范围搜索范围Sheet2到Sheet3
EN

Stack Overflow用户
提问于 2016-05-13 20:20:20
回答 1查看 205关注 0票数 1

例如,我搜索并发现了一些类似的帖子。

Look for values from sheet A in sheet B, and then do function in corresponding sheet B cellFor each Loop Will Not Work Search for Value On one Sheet and Change Value on another Sheet

虽然每一个都涉及到我的目标的某些方面,但它们并不完全是这样。我有3张工作表,sheet1 - 3,我想在A和B列的sheets1 -2中搜索和匹配,如果在A列的B列中找到匹配或没有找到匹配,请检查A列中的值是否复制到sheet3。

这就是到目前为止我使用Office 2016的情况。

代码语言:javascript
复制
Public Sub SeekFindCopyTo()

Dim lastRow1 As Long
Dim lastRow2 As Long
Dim tempVal As String

lastRow1 = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
lastRow2 = Sheets("Sheet2").Range("B" & Rows.Count).End(xlUp).Row

For sRow = 4 To lastRow1
    Debug.Print ("sRow is " & sRow)
    tempVal = Sheets("Sheet1").Cells(sRow, "B").Text

    For tRow = 4 To lastRow2
        Debug.Print ("tRow is " & tRow)
        TestVal = Sheets("Sheet2").Cells(tRow, "B")
        Operations = Sheets("Sheet2").Cells(tRow, "A")

        If Sheets("SAP_XMATTERS").Cells(tRow, "B") = tempVal Then
            Operations = Sheets("Sheet2").Cells(tRow, "A")
            Debug.Print ("If = True tempVal is " & tempVal)
            Debug.Print ("If = True TestVal is " & TestVal)
            Debug.Print ("If = True Operaitons is " & Operations)
            If Operations = "REMOVE" Then
                 Sheets("Sheet2").Range("A" & tRow).EntireRow.Copy
                 Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell
                 'Sheets("Sheet2").Rows(tRow).Delete
            Else
                 'Sheets("Sheet2").Rows(tRow).Delete
            End If
        End If
    Next tRow
Next sRow
End Sub

代码运行得很好,但问题是,我在B:B中寻找工作表1和2之间的匹配,如果匹配,我想检查A:A中的相邻单元格,如果字符串删除,则删除,然后将整行复制到sheet3。这里还有一个问题,我还想知道B:B中的B:B是否与相邻单元格中的字符串进程之间的B:B不匹配,如果是这样,那么将整行复制到sheet3。我可以在单独的潜艇中做任何一种选择,但不能一次过就让它工作。

即使你的帮助是“你做不到的”,我们也会感激你的;

提亚

鲍勃

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-16 21:00:43

使用.Find进行的完全重写就成功了。

代码语言:javascript
复制
Sub SeekFindCopy()
    Dim sourceValue As Variant
    Dim resultOfSeek As Range
    Dim targetRange As Range

    LastRow = Sheets("Sheet1").Range("B" & Rows.Count).End(xlUp).Row
    Set targetRange = Sheets("Sheet2").Range("B:B")

    For sourceRow = 4 To LastRow
    Debug.Print ("sRow is " & sRow)

    sourceValue = Sheets("Sheet1").Range("B" & sRow).Value

    Set resultOfSeek = targetRange.Find(what:=sourceValue, After:=targetRange(1))
        'Debug.Print (sourceValue)
    Operations = Sheets("Sheet1").Cells(sRow, "A")
    If resultOfSeek Is Nothing Then
            'Debug.Print ("Operations is " & Operations)
        If Operations = "PROCESS" Then
            Sheets("Sheet1").Range("A" & sRow).EntireRow.Copy
            Sheets("UpLoad").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell
            'Sheets("Sheet1").Rows(tRow).Delete
        End If
    Else
            'Debug.Print ("Operations is " & Operations)
        If Operations = "REMOVE" Then
            Sheets("Sheet1").Range("A" & sRow).EntireRow.Copy
            Sheets("UpLoad").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Insert xlcutcell
            'Sheets("Sheet1").Rows(tRow).Delete
        End If
    End If
    Next sourceRow
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37218775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档