【问题标题】:Update values in a table based on specific values根据特定值更新表中的值
【发布时间】:2020-09-10 04:48:10
【问题描述】:

我正在尝试复制和粘贴,使用循环、匹配和偏移。

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

    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                   

我需要做以下事情。

  1. 搜索 Sheet2 (Table1[PrimaryKey]) 并根据单元格 A2 值进行匹配
  2. 在工作表 1 的 C2 中复制值(例如 ABCD)
  3. 在 Lx 中粘贴.Values(其中 x =在步骤 1 中找到的行 id)
  4. 在表 1 中复制 CD 中的值(例如 1234)
  5. Ox 中的粘贴.Values(其中 x =在步骤 1 中找到的行 ID)
  6. 在 Wx 中粘贴 today()
  7. 循环搜索列 E = 1 的所有单元格

这是我找到的代码。我无法弄清楚循环和匹配组件。

'Set sheets   
Set sh1 = Sheets("Data Capture")   
Set sh2 = Sheets("DataTable")      

'Find string in column A of Sheet2   
Set foundCell = sh2.Range("Table5[PrimaryKey]").Find(sh1.Range("Z2").Value, , xlValues, xlWhole)   
If Not foundCell Is Nothing Then 'If match cell is found     
    sh1.Range("B2").Copy     
    foundCell.PasteSpecial xlPasteValues     
    foundCell.PasteSpecial xlPasteFormats     
    Application.CutCopyMode = False   
Else   

End If 

End Sub

【问题讨论】:

  • 这其中的哪个具体部分给您带来了问题?
  • 全部。我找到了一些代码,但是我无法理解它以适应我的需要。
  • 'Set sheets Set sh1 = Sheets("Data Capture") Set sh2 = Sheets("DataTable") 'Find string in column A of Sheet2 Set foundCell = sh2.Range("Table5[PrimaryKey]").Find(sh1.Range("Z2").Value, , xlValues, xlWhole) If Not foundCell Is Nothing Then 'If match cell is found sh1.Range("B2").Copy foundCell.PasteSpecial xlPasteValues foundCell.PasteSpecial xlPasteFormats Application.CutCopyMode = False Else End If End Sub
  • 这是我找到的代码,但是我可以找出循环和匹配组件。
  • 当您弄清楚循环和匹配组件时,您所做的就是根据需要进行编辑。

标签: excel vba loops match paste


【解决方案1】:

我不确定我是否理解您的问题,但我尝试重建您的表。所以我将第一张表命名为“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多