簡體   English   中英

在Excel VBA中的兩個工作表之間查找和替換

[英]Find and replace between two sheets in Excel VBA

我知道以前已經做過,但是我遇到了要更改此腳本部分的問題,我只是不知道該怎么做。

這是代碼,下面是我需要做的。

Option Explicit

Sub PENCMR()
    Dim i As Integer

    With Application
        .ScreenUpdating = False
    End With

    'Internal NCMR
    Dim wsPE As Worksheet
    Dim wsNDA As Worksheet

    'Copy Ranges
    Dim c As Variant

    'Paste Ranges
    Dim p As Range

    'Setting Sheet
    Set wsPE = Sheets("Print-Edit NCMR")
    Set wsNDA = Sheets("NCMR Data")
    Set p = wsPE.Range("A54:U54")

    With wsPE
        c = Array(.Range("AG3"), .Range("B11"), .Range("B14"), .Range("B17"), .Range("B20"), .Range("B23") _
                , .Range("Q11"), .Range("Q14"), .Range("Q17"), .Range("Q20"), .Range("R25"), .Range("V23") _
                , .Range("V25"), .Range("V27"), .Range("B32"), .Range("B36"), .Range("B40"), .Range("B44") _
                , .Range("D49"), .Range("L49"), .Range("V49"))
    End With

    For i = LBound(c) To UBound(c)
        p(i + 1).Value = c(i).Value
    Next

    With wsNDA
        Dim rFind As Long, NR As Long, LR As Long, LC As Long
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        LC = .Cells(2, Columns.Count).End(xlToLeft).Column
        NR = LR + 1
        rFind = wsNDA.Range("A3:A" & LR).Find(wsPE.Range("A54:U54")).Row

        .Range("A54", .Cells(2, LC)).Copy
        .Range("A" & rFind).PasteSpecial xlPasteValues
        .Range("A54", .Cells(1, LC)).ClearContents
    End With

    With Application
        .ScreenUpdating = True
    End With
End Sub

該腳本旨在執行以下操作:

激活代碼后,它是要復制所有單元格,然后將它們粘貼到表單下方的行中,然后在引用第二頁后,將新創建的行的第一個單元格與第二頁列表副本進行比較並替換信息。

我想看到的是,由於被告知無需粘貼即可粘貼到同一頁面上,因此可以復制數據,在第二張紙上搜索ID編號,並在該行上粘貼新數據。

這是工作表:

Excel替換WkSht

現在編寫的方式,它不會替換信息,而只是用空白信息覆蓋它。 我還沒有弄清楚為什么...希望通過重寫此請求,我能夠解決該問題。

再次感謝您的幫助。 到目前為止,他們在幫助我不僅學習,而且從長遠來看更聰明的寫作方面所取得的成就令人贊嘆。

建議的更改:

Sub PENCMR()
    Dim i As Integer

    'Internal NCMR
    Dim wsPE As Worksheet
    Dim wsNDA As Worksheet
    Dim c As Variant 'Copy Ranges
    Dim p As Range 'Paste Ranges

    Application.ScreenUpdating = False

    'Setting Sheet
    Set wsPE = Sheets("Print-Edit NCMR")
    Set p = wsPE.Range("A54:U54")

    Set wsNDA = Sheets("NCMR Data")

    c = Array("AG3", "B11", "B14", "B17", "B20", "B23" _
            , "Q11", "Q14", "Q17", "Q20", "R25", "V23" _
            , "V25", "V27", "B32", "B36", "B40", "B44" _
            , "D49", "L49", "V49")

    For i = LBound(c) To UBound(c)
        p(i + 1).Value = wsPE.Range(c(i)).Value
    Next

    With wsNDA
        Dim NR As Long, LR As Long, LC As Long
        Dim f As Range

        LR = .Range("C" & Rows.Count).End(xlUp).Row
        LC = .Cells(2, Columns.Count).End(xlToLeft).Column
        NR = LR + 1

        'find matching row if it exists
        Set f = .Range("A3:A" & LR).Find(what:=p.Cells(1).Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            f.Resize(1, p.Cells.Count).Value = p.Value
        Else
            'what should happen if not found?
        End If
    End With

    Application.ScreenUpdating = True

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM