0

我在 Excel 中有两张工作表。Sheet2 具有来自数据库源的所有输出数据表。我一直在尝试编写一个可以将特定数据记录从 sheet2 传输到 sheet1 的 VBA。我试图制定一个标准,如果大约 4 个字段列的特定记录的行数据与具有要传输的数据。

例如,我在表 2 中有一系列数据,如下所示

            LIMIT    SALES    REGION   LOCATION  ITEM
            422234   4768.24  HR       1         BUIL
            343222   190.73   BP       2         CON
            432220   1494.62  OP       1         EQ
            343332   1302.66  BP       1         AR
            433322   3881.67  BP       1         NO

标准是,如果 sheet2 中的行显示 - 区域“OP”,位置“2”,则项目 BUIL 的销售记录只能显示在 sheet1 中的特定单元格上。同样的规则将适用于在 sheet1 中显示的限制记录。Sheet1 将 limit 和 sales 作为列,将 items 作为行。

4

1 回答 1

0

The following might help you get started.

Suppose you have your database data (as shown) in Sheet 2 starting in Range A1 and on Sheet1 you have as follows:

    A        B        C
1   Item     Limit    Sales
2   BUIL
3   CON
4   EQ
    etc...

The following code will work down your list of items in Sheet1 and if it finds a match in your data on Sheet2 it will then check to see if the criteria are met i..e Region = "OP" and Loaction = 2:

Sub GetSalesAndLimit()
    Dim items As Range, entries As Range, item As Range, entry As Range, row As Long

    Set items = Range("A2:A" & Range("A2").End(xlDown).row)
    Set entries = Worksheets("Sheet2").Range("E2:E" & Worksheets("Sheet2").Range("E2").End(xlDown).row)

    For Each item In items
        For Each entry In entries
            With Worksheets("Sheet2")
                If entry = item Then
                    If entry.Offset(0, -2) = "OP" And entry.Offset(0, -1) = 2 Then 'Criteria is region = 'OP' and Loaction = 2
                        item.Offset(0, 1) = entry.Offset(0, -4) 'Limit
                        item.Offset(0, 2) = entry.Offset(0, -3) 'Sales
                    End If
                End If
            End With
        Next entry
    Next item
End Sub
于 2012-04-17T15:47:00.773 回答