简体   繁体   English

根据当前工作簿中的匹配值编辑主工作簿中的单元格

[英]Edit cells in master workbook based on matched values from current workbook

The goal here is to compare values in column "A" between two workbooks (the current workbook is .xlsm, the target workbook is .xlsx). 此处的目标是比较两个工作簿之间的“ A”列中的值(当前工作簿为.xlsm,目标工作簿为.xlsx)。 If any matches are found; 如果找到匹配项; the value in column "E" on the same row of matched value, is changed in the target workbook. 在目标工作簿中,匹配值的同一行上“ E”列中的值已更改。 It is mandatory to keep the workbooks separate in this case. 在这种情况下,必须将工作簿分开。

I decided to do this by selecting the first value in the current workbook (A2), applying it's value to a variable, then scanning column "A" in the target workbook to find a match (there should always be at least one match). 我决定通过选择当前工作簿(A2)中的第一个值,将其值应用于变量,然后扫描目标工作簿中的“ A”列以找到匹配项(始终至少有一个匹配项)来做到这一点。 Then changing the value of column "E" in the target workbook to "DSC" for those matched rows. 然后将那些匹配行的目标工作簿中“ E”列的值更改为“ DSC”。 Afterwards the selected cell in the current workbook is moved down one, and loops this process until a blank cell is reached. 然后,将当前工作簿中的选定单元格向下移动一个,并循环执行此过程,直到到达空白单元格为止。

Here is the code currently: 这是当前的代码:

Sub DSC()
  Dim RowCount As Long

  secondWorkbook = "Master.xlsx"
  currentWorkbook = ThisWorkbook.Name
  Workbooks.Open ThisWorkbook.Path & "\" & secondWorkbook

  ' Define number of rows
  RowCount = Workbooks("Master.xlsx").Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

  ' Select First Cell
  Windows(currentWorkbook).Activate
  Worksheets("Update Wipe").Activate
  Range("A2").Select
  Serial = ActiveCell.Value

  Windows(secondWorkbook).Activate
  Worksheets("Sheet1").Activate

  ' Run Function
  For c = 2 To (RowCount - 1)
    Windows(secondWorkbook).Activate
    If Sheet1.Cells(c, 1).Value = Serial Then
        Sheet1.Cells(c, 5) = "DSC"
        Windows(currentWorkbook).Activate
        Worksheets("Update Wipe").Activate
        Selection.Offset(1, 0).Select
        Serial = ActiveCell.Value
        If Serial = "" Then Exit For
    End If
  Next c
End Sub

At the moment no errors are returned, however nothing is updating in the target workbook. 目前没有错误返回,但是目标工作簿中没有任何更新。 It will open the target workbook on the computer. 它将在计算机上打开目标工作簿。 Bouncing the active workbook and worksheet back and forth to change the selected cell and update the variable may be the cause. 来回跳动活动的工作簿和工作表以更改所选的单元格并更新变量可能是原因。

Here is a simplified working example. 这是一个简化的工作示例。

You will want to change Range("A1:A20") to the range you want to compare. 您需要将Range("A1:A20")更改为要比较的范围。

You can also change Sheet("Sheet1") to the proper sheets. 您也可以将Sheet("Sheet1")更改为适当的工作表。

AWorkbook is the .xlsm workbook. AWorkbook.xlsm工作簿。

MasterWorkbook is the .xlsx workbook. MasterWorkbook.xlsx工作簿。

As you can see, it's not necessary to use select. 如您所见,没有必要使用select。

You can loop through the cells using a For or For Each loop. 您可以使用ForFor Each循环遍历单元格。

By nesting the for loops you can compare cells, though with larger datasets I would probably use Find and FindNext as opposed to looping through cells. 通过嵌套for循环,您可以比较单元格,尽管对于较大的数据集,我可能会使用FindFindNext而不是遍历单元格。

In this macro, I loop through each cell in our macro workbook, and compare it to each cell in the target workbook. 在此宏中,我遍历宏工作簿中的每个单元,并将其与目标工作簿中的每个单元进行比较。

If the values match, I place DSC in the target workbook (column E ) 如果值匹配,我将DSC放在目标工作簿中(列E

Finally, close the workbook ( SaveChanges:=True ) 最后,关闭工作簿( SaveChanges:=True

Sub DSC()
Dim AWorkbook, MasterWorkbook, c, d, ALastRow, MLastRow
Set AWorkbook = ThisWorkbook
Set MasterWorkbook = Workbooks.Open(ThisWorkbook.Path & "\" & "MasterWorkbook.xlsx", ReadOnly:=False)
ALastRow = AWorkbook.Sheets("Sheet1").Cells(Rows.CountLarge, "A").End(xlUp).Row
MLastRow = MasterWorkbook.Sheets("Sheet1").Cells(Rows.CountLarge, "A").End(xlUp).Row
For Each c In AWorkbook.Sheets("Sheet1").Range("A2:A" & ALastRow)
    For Each d In MasterWorkbook.Sheets("Sheet1").Range("A2:A" & MLastRow)
        If c.Value = d.Value Then MasterWorkbook.Sheets("Sheet1").Cells(d.Row, "E").Value = "DSC"
    Next d
Next c
MasterWorkbook.Close (True)
End Sub

Load all of the values from Worksheets("Update Wipe")'s column A anto a dictionary. 从Worksheets(“ Update Wipe”)的A列中将所有值加载到字典中。 Open the Master workbook and Autofilter using the dictionary keys. 打开母版工作簿,然后使用字典键自动筛选。 Put "DSC" into all the visible cells in column E. 将“ DSC”放入E列中所有可见的单元格中。

Option Explicit

Sub DSC()
    Dim mWB As Workbook
    Dim masterWorkbook As String, c As Long, rowCount As Long, serial As Variant
    Dim k As Long, dict As Object

    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare

    With ThisWorkbook.Worksheets("Update Wipe")
        For k = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
            dict(.Cells(k, "A").Value2) = vbNullString
        Next k
    End With

    masterWorkbook = "Master.xlsx"
    Set mWB = Workbooks.Open(ThisWorkbook.Path & "\" & masterWorkbook)

    With mWB.Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, "A").CurrentRegion
            .AutoFilter field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            With .Resize(.Rows.Count - 1, 5).Offset(1, 0)
                If CBool(Application.Subtotal(103, .Cells)) Then
                    .Columns("E").SpecialCells(xlCellTypeVisible) = "DSC"
                End If
            End With
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
        'optionally close the master workbook
        '.Parent.Close savechanges:=True
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA从当前单元格值打开工作簿 - VBA Open workbook from current cells value 根据条件将单元格范围从主工作簿复制到多个文件 - Copy range of cells from master workbook onto multiple files based on criteria 将多个工作簿中的数据收集到主工作簿中 - 来自单元格的路径 - Gather Data from multiple workbooks into master workbook - paths from cells 将工作表从主工作簿复制到活动工作簿 - Copy worksheets from a master workbook to active workbook 想要从另一个工作簿中使用 SUMIF 带来匹配数据的值 - Want to bring values of matched data with SUMIF from another workbook 对工作簿中的单元格求和,然后粘贴到另一个工作簿中 - Sum cells from a workbook and paste in another workbook 从Outlook编辑Excel中的单元格而不重新打开工作簿 - Edit Cells in Excel from Outlook without reopening Workbook 将工作表从另一个工作簿(#2)导入到当前工作簿(#1) - Import a worksheet from another workbook (#2) to current workbook (#1) 将复选框从另一个工作簿复制到当前工作簿? - Copy checkbox from another workbook to current workbook? 比较和匹配2列,并将匹配项的值从工作簿1中的下一列复制到工作簿2中的空列对照匹配项 - Compare and Match 2 Columns and Copy the values of Matched items from next Column in Workbook 1 to Empty Column in Workbook 2 against Matched items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM