在工作表B中查找工作表A中的值,然后在相应的工作表B单元格中执行操作

时间:2015-01-27 18:59:37

标签: excel vba excel-vba

我需要在工作表B中对表A中的值进行操作,然后在VBA中的相应工作表B单元格中执行操作,这样我就可以更改工作表1中的数据,按下按钮,然后使其工作。字母标题表示excel中行的名称。 xs只是说它是我不想操纵的数据。 我有第1页和第2页。

 Worksheet 1                        
H     I            J          K          L           M          N
1   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    100     xxxxxxxx
2   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    200     xxxxxxxx
3   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    300     xxxxxxxx
4   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    400     xxxxxxxx
5   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    500     xxxxxxxx
6   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    600     xxxxxxxx
7   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    700     xxxxxxxx
8   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    800     xxxxxxxx
9   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    900     xxxxxxxx
10  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1000    xxxxxxxx
11  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1100    xxxxxxxx
12  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1200    xxxxxxxx
13  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1300    xxxxxxxx
14  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1400    xxxxxxxx
15  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1500    xxxxxxxx
16  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1600    xxxxxxxx
17  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1700    xxxxxxxx
18  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1800    xxxxxxxx
19  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1900    xxxxxxxx
20  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    2000    xxxxxxxx

Worksheet 2                     
   H        I         J           K           L         M     N
   2    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    25  xxxxxxxx
   5    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    40  xxxxxxxx
   9    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    35  xxxxxxxx
  11    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    50  xxxxxxxx
  10    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    65  xxxxxxxx
   6    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    90  xxxxxxxx
   7    xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    10  xxxxxxxx

这样就可以在工作表1中减去相应的值,从而改变M列的值。

Worksheet 1 - new                   
H     I            J          K          L           M          N
1   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    100     xxxxxxxx
2   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    175     xxxxxxxx
3   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    300     xxxxxxxx
4   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    400     xxxxxxxx
5   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    460     xxxxxxxx
6   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    510     xxxxxxxx
7   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    690     xxxxxxxx
8   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    800     xxxxxxxx
9   xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    865     xxxxxxxx
10  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    935     xxxxxxxx
11  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1050    xxxxxxxx
12  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1200    xxxxxxxx
13  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1300    xxxxxxxx
14  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1400    xxxxxxxx
15  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1500    xxxxxxxx
16  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1600    xxxxxxxx
17  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1700    xxxxxxxx
18  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1800    xxxxxxxx
19  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    1900    xxxxxxxx
20  xxxxxxxx    xxxxxxxx    xxxxxxxx    xxxxxxxx    2000    xxxxxxxx

3 个答案:

答案 0 :(得分:0)

这将对工作表B

上的值执行更新
Sub New2VBA()
    Dim A As Worksheet, B As Worksheet
    Dim i As Long, j As Long, v As Variant
    Dim N As Long, M As Long
    Set A = Sheets("Sheet A")
    Set B = Sheets("Sheet B")
    N = A.Cells(Rows.Count, "A").End(xlUp).Row
    M = B.Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To N
        v1 = A.Cells(i, "A").Value
        v2 = A.Cells(i, "B").Value
        For j = 1 To M
            If v1 = B.Cells(j, "A").Value Then
                B.Cells(j, "B").Value = B.Cells(j, "B").Value - v2
                Exit For
            End If
        Next j
    Next i
End Sub

修改#1:

这是更新的代码.....删除旧代码:

这将根据工作表 B 上的增量值更新工作表 A (您的工作表1)上的值(2) )

Sub New2VBA()
    Dim A As Worksheet, B As Worksheet
    Dim i As Long, j As Long, v As Variant
    Dim N As Long, M As Long
    Set A = Sheets("Sheet A")
    Set B = Sheets("Sheet B")
    '
    '   A is worksheet 1
    '   B is worksheet 2
    '
    N = A.Cells(Rows.Count, "H").End(xlUp).Row
    M = B.Cells(Rows.Count, "H").End(xlUp).Row

    For i = 1 To M
        v1 = B.Cells(i, "H").Value
        v2 = B.Cells(i, "M").Value
        For j = 1 To N
            If v1 = A.Cells(j, "H").Value Then
                A.Cells(j, "M").Value = A.Cells(j, "M").Value - v2
                Exit For
            End If
        Next j
    Next i
End Sub

答案 1 :(得分:0)

通过对Gary的学生答案的致谢,这是主题的变体,(a)使用Range.Find方法查找帐户,(b)允许您设置您请求的列(和其他设置参数)。因此,请根据您的方案设置各种参数。请注意,我的测试数据集都定位在A1开始,而公司/费用/账户是并排的。 Offset的1。

Option Explicit
Sub subExp()
Dim wsExp As Worksheet, wsAcc As Worksheet
Dim accRng As Range, fndCo As Range, c As Range
Dim expStRow As Long, expEndRow As Long, expCoCol As Long, expExpColOffset As Long
Dim accStRow As Long, accEndRow As Long, accCoCol As Long, accAcColOffset As Long

'Assign worksheets
Set wsExp = Worksheets("Expenses")
Set wsAcc = Worksheets("Accounts")

'Assign data start position on each sheet
expStRow = 2
expCoCol = 1
expExpColOffset = 1
accStRow = 2
accCoCol = 1
accAcColOffset = 1

    With wsAcc
        accEndRow = .Cells(Rows.Count, accCoCol).End(xlUp).Row
        Set accRng = .Range(.Cells(accStRow, accCoCol), .Cells(accEndRow, accCoCol).Offset(0, accAcColOffset))
    End With

    With wsExp
        expEndRow = .Cells(Rows.Count, expCoCol).End(xlUp).Row
        Set expRng = .Range(.Cells(expStRow, expCoCol), .Cells(expEndRow, expCoCol))
            For Each c In expRng
                Set fndCo = accRng.Find(What:=c, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
                If Not fndCo Is Nothing Then
                    fndCo.Offset(0, accAcColOffset).Value = fndCo.Offset(0, accAcColOffset).Value - c.Offset(0, expExpColOffset).Value
                End If
            Next c
    End With
End Sub

答案 2 :(得分:0)

作为避免循环的替代方法:

Sub tgr()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim rngH1 As Range, rngM1 As Range
    Dim rngH2 As Range, rngM2 As Range
    Dim arrResults As Variant

    Set ws1 = ActiveWorkbook.Sheets("Sheet1")
    Set rngH1 = ws1.Range("H2", ws1.Cells(Rows.Count, "H").End(xlUp))
    Set rngM1 = Intersect(rngH1.EntireRow, ws1.Columns("M"))

    Set ws2 = ActiveWorkbook.Sheets("Sheet2")
    Set rngH2 = ws2.Range("H2", ws2.Cells(Rows.Count, "H").End(xlUp))
    Set rngM2 = Intersect(rngH2.EntireRow, ws2.Columns("M"))

    arrResults = Evaluate("=INDEX(" & rngM1.Address(External:=True) & "-SUMIF(" & rngH2.Address(External:=True) & "," & rngH1.Address(External:=True) & "," & rngM2.Address(External:=True) & "),)")

    rngM1.Value = arrResults

End Sub