Excel 基于特定值更新表中的值

Excel 基于特定值更新表中的值,excel,vba,loops,match,paste,Excel,Vba,Loops,Match,Paste,我试图复制和粘贴,使用循环,匹配和偏移 在更新表中,我有一个表,用户将在其中更新值 A B C D E 1 PrimaryKey Line New Value 1 New Value 2 UpdateValue 2 XY.1 1 ABCD 1234 1 3 XY.2 2 ZXCV

我试图复制和粘贴,使用循环,匹配和偏移

在更新表中,我有一个表,用户将在其中更新值

    A           B       C              D              E
1   PrimaryKey  Line    New Value 1    New Value 2    UpdateValue
2   XY.1        1       ABCD           1234           1
3   XY.2        2       ZXCV           9876           1
4                   
我需要做以下工作

  • 搜索Sheet2(表1[PrimaryKey])并基于单元格A2值进行匹配
  • 复制第1页C2中的值(例如ABCD)
  • 在Lx中粘贴.value(其中x=在步骤1中找到的行id)
  • 将CD中的值复制到第1页(例如1234)
  • 粘贴.Ox中的值(其中x=在步骤1中找到的行id)
  • 在Wx中粘贴今天()
  • 列E=1的所有单元格的循环
  • 这是我找到的代码。我无法找出循环和匹配的组件

    “设置工作表”
    设置sh1=工作表(“数据捕获”)
    设置sh2=图纸(“数据表”)
    '在Sheet2的A列中查找字符串
    设置foundCell=sh2.Range(“表5[PrimaryKey]”)。查找(sh1.Range(“Z2”)。值,xlValues,xlother)
    如果未找到单元格,则“如果找到匹配单元格”
    sh1.范围(“B2”).副本
    foundCell.Paste特殊XLPaste值
    foundCell.Paste特殊XLPaste格式
    Application.CutCopyMode=False
    其他的
    如果结束
    端接头
    
    我不确定是否理解您的问题,但我尝试重建您的表。因此,我将第一张纸命名为“Sheet1”,从A1-E3中的问题复制您的表格,将第二张纸命名为“Sheet2”,并在A1-A2中写下“XY.1”和“XY.2”

    也许下面的宏将帮助您。它接受表中写入的值(在“Sheet1”上),转到“Sheet2”,在第一列中搜索值,并将其他内容写入L、O和W列。但我不明白如何处理表B列中的值

    Sub NameYourMacro()
    
    Dim i As Integer
    Dim strTheRow As String
    Dim strNewValue1 As String
    Dim strNewValue2 As String
    Dim rngFoundCell As Range
    
    i = 2 'First row on Sheet1 with values is row 2
    While Cells(i, 1).Value <> "" 'while there is a value in column A
        
        If Cells(i, 5).Value = 1 Then 'if value in column E is 1 then to things
        
            'Read Values:
            strTheRow = Cells(i, 1).Value
            strNewValue1 = Cells(i, 3).Value
            strNewValue2 = Cells(i, 4).Value
        
            'go to Sheet2 and search in the first column
            Sheets("Sheet2").Activate
            Set rngFoundCell = Columns(1).Find(strTheRow, , xlValues, xlWhole)
            If Not rngFoundCell Is Nothing Then 'if vba found the value in column 1
                Sheets("Sheet2").Range("L" & rngFoundCell.Row).Value = strNewValue1
                Sheets("Sheet2").Range("O" & rngFoundCell.Row).Value = strNewValue2
                Sheets("Sheet2").Range("W" & rngFoundCell.Row).Value = Date
            Else
                'if rngFoundCell is nothing (value not found) do nothing
            End If
        Else
            'if value in column E is not 1 do nothing
        End If
    
        'go back to Sheet1 and next row...
        Sheets("Sheet1").Activate
        i = i + 1
    Wend
    
    End Sub
    
    子名称YourMacro()
    作为整数的Dim i
    作为字符串的Dim STRHEROW
    作为字符串的Dim strNewValue1
    作为字符串的Dim strNewValue2
    Dim rngFoundCell作为范围
    i=2'表1上具有值的第一行是第2行
    当a列中有值时,While单元格(i,1)中的值为“”
    如果单元格(i,5).Value=1,则‘如果列E中的值为1,则表示事物
    '读取值:
    strTheRow=单元格(i,1).值
    strNewValue1=单元格(i,3).值
    strNewValue2=单元格(i,4).值
    '转到Sheet2并在第一列中搜索
    工作表(“工作表2”)。激活
    设置rngFoundCell=Columns(1)。查找(strTheRow、xlValues、xlWhole)
    如果不是rngFoundCell为Nothing,则“如果vba在第1列中找到值
    图纸(“图纸2”)。范围(“L”和rngFoundCell.Row)。值=strNewValue1
    工作表(“Sheet2”)。范围(“O”和rngFoundCell.Row)。值=strNewValue2
    工作表(“Sheet2”)。范围(“W”&rngFoundCell.Row)。值=日期
    其他的
    '如果rngFoundCell为nothing(未找到值),则不执行任何操作
    如果结束
    其他的
    '如果列E中的值不是1,则不执行任何操作
    如果结束
    '返回到Sheet1和下一行。。。
    工作表(“工作表1”)。激活
    i=i+1
    温德
    端接头
    
    这其中哪一部分给您带来了问题?所有问题。我找到了一些代码,但是我无法理解它以适应我的需要。
    “Set sheets Set sh1=sheets(“数据捕获”)Set sh2=sheets(“数据表”)在Sheet2 Set foundCell=sh2.Range(“Table5[PrimaryKey]”)的A列中查找字符串。查找(sh1.Range(“Z2”).Value,xlValues,xlother)如果未找到foundCell,则“如果找到匹配单元格sh1.Range(“B2”).Copy foundCell.PasteSpecial xlPasteValues foundCell.PasteSpecial xlPasteFormats Application.CutCopyMode=False Else End If End Sub
    这是我找到的代码,但我可以找出循环,以及匹配组件。当你计算出循环和匹配组件时,你所做的就是根据需要进行编辑。