【问题标题】:VBA to find and update row based on valueVBA根据值查找和更新行
【发布时间】:2015-09-17 21:22:51
【问题描述】:

我有一个主工作簿和多个子工作簿,每个工作簿都位于固定位置,它们将工作记录保存为单独的行。根据选择的工作簿,将行从主工作簿复制到子工作簿。

但是,我坚持使用 VBA 编码(以宏形式),因此他们可以从每个子工作簿中更新主工作簿。我需要它根据分配给每件作品并出现在子工作簿和主工作簿的同一列(D 列)中的唯一 ID 号来查找和更新主文件中的工作行。

任何帮助或想法将不胜感激。

提前致谢

抱歉。请查看我的示例子工作簿中的以下数据(抱歉,我无法正确格式化),在此之下,我必须将当前 VBA 代码复制回主工作簿:

数据:

Complaint Type  Raised by   Status      ID
Billing         Percy       Completed   101
Billing         Percy       Completed   102
Metering        John        Pending     103
Reads           John        Pending     104
Reads           Jack        Pending     105
Billing         Julie       Untouched   106
Service         Jack        Completed   107
Metering        Julie       Untouched   108
Service         Percy       Pending     109
Payment         Pete        Pending     110

VBA 代码:

Private Sub CommandButton21_Click()

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

 Dim SourceRange As Range, DestRange As Range

 Set SourceRange = Sheets("Sheet1").Range("A2:D2") 'data source
 wb = ActiveWorkbook.Name

 Workbooks.Open "C:\Users\user\Desktop\Test.xlsm" 'path to Master
 Windows(wb).Activate 'Activate Child Workbook
 SourceRange.Cut 'define the range to copy 'Cut data from child workbook

 Windows("Test.xlsm").Activate 'Activate Master
 Sheets("Completed").Select 'Activate Sheet
 Workbooks("Test.xlsm").Sheets("Completed").Cells(Rows.Count, "A").End(xlUp).Offset(1).Paste 'Paste in Master

 Application.CutCopyMode = False 'Clear Clipboard

 End Sub

【问题讨论】:

  • 使用MATCH function 获取主工作表中的行号。一旦你有了它,其他一切都只是使用Range.Cells property 来定位目标的直接价值转移。
  • 我可以非常轻松快速地使用 WB 版本的 MATCH。有没有办法将该逻辑操作到 VBA 版本中?
  • 类似rw = application.match(.Range("A1"), sheets("Master").columns(1), 0)。然后使用rw 设置值。您没有样本数据,也没有我可以尝试理解的原始努力,因此您几乎只能靠自己。
  • 请参阅修改后的原始评论以及编码和示例数据表。抱歉,我对 VBA 编码还是很陌生。我会尽我所能使用你提供的代码,并用我拥有的东西回来。

标签: vba excel


【解决方案1】:

下面的代码可以放在您的每个子工作簿中,我不确定 ID 出现在主工作簿中的哪个位置,所以我只是假设 D 列与子工作簿相同,以下代码未经测试且基于 D 列在子项中匹配主项中的 D 列,它将更新 A、B 和 C 列。目前它只对 2000 行执行此操作,如果适用,请更改。 :)

Dim fpath As String
Dim owb As Workbook
Dim Master As Worksheet 
Dim Slave As Worksheet 'the following declares both master and slave as worksheets

fpath = "location of master workbook" 


Set owb = Application.Workbooks.Open(fpath) 'opens the file path

Set Master = ThisWorkbook.Worksheets("name of sheet in child workbook") 'declares this workbook and sheet as "master"
Set Slave = owb.Worksheets("name of sheet in master you are pasting to") 'declares the workbook and sheet you're copying to as "slave"


For j = 1 To 2000 '(the master sheet) 'goes through each row from 1 to 2000

For i = 1 To 2000 '(the slave sheet) 'again does the same and the slave sheet
    If Trim(Master.Cells(j, 4).Value2) = vbNullString Then Exit For 'if the ID is blank it will exit and move on to the next row
    If Master.Cells(j, 4).Value = Slave.Cells(i, 4).Value Then 'the 4 represents column D, if cell in column D matches the cell in column D in the masterwork book then it will..
            Slave.Cells(i, 1).Value = Master.Cells(j, 1).Value 'cell in column A child workbook equals cell in column A in master workbook
            Slave.Cells(i, 2).Value = Master.Cells(j, 2).Value
            Slave.Cells(i, 3).Value = Master.Cells(j, 3).Value 'same for B and C


    End If
    Next


Next


MsgBox ("Data Transfer Successful")

With owb
.Save
.Close
End With

【讨论】:

  • 哇!这是第一次完美地工作!非常感谢!!比我的代码更干净、更快捷。只是为了我自己的利益和学习,您能否快速总结一下这段代码的工作原理以及每个部分的作用?再次感谢@Calum!
  • 嗨@Wowdude,很高兴我能帮上忙。我添加了更多注释注释,希望能更多地解释它,总而言之,虽然它从 1 到 2000 遍历每个单元格,并且如果子工作簿的 D 列中的单元格等于主工作簿中 D 列的单元格,它将复制这些值这也落在那一行,所以在这种情况下,列 A、B 和 C。希望这是有道理的!如果您也能接受答案,那就太好了:-)谢谢
  • 非常感谢@Callum。我可以厚颜无耻地问你将如何关闭打开的从工作簿。我尝试了许多我知道的不同方法(包括 Activeworkbook.Close 和 Workbooks("owb").Close),但它们都失败了。再次感谢! =D
  • 感谢您的接受,我想您也想保存它吗?关闭不会保存它,只会关闭它。由于 Slave 不是无法工作的 active,请查看编辑后的代码和底部的“with owb”。谢谢:)
  • 啊太棒了。我再次欠你的债@Callum
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-16
相关资源
最近更新 更多