如何使用循环匹配工作表之间的数据

时间:2015-06-04 14:20:29

标签: excel vba excel-vba

好的,所以故障。

我有2张。一张(order_LVL)具有订单号沿着行向下的原始数据和具有相应单元格中该行的位置整数的“行号”。我正在尝试使用相同的订单号填充其他工作表(sheet1),但是使用位置编号(1-246)作为列标题(此时,order_LVL的单元格中的值正在成为列标题of sheet1)。在相应的单元格内,如果order_LVL表示订单中有一条来自该位置的行,那么我想放一个“1”,如果不是,那么我想放一个“0”。

这就是我填充数据的方法,并且我在if语句的行中不断收到“对象不支持此属性或方法”的错误“

Sub PopulateData()
Dim s1 As Excel.Worksheet
Dim s2 As Excel.Worksheet
Set s1 = Sheets("Order_LVL")
Set s2 = Sheets("sheet1")
Dim orderrows As Range
Dim Lastrow As Integer
Lastrow = s1.Cells(Rows.Count, 1).End(xlUp).row
Set orderrows = Range("B2" & Lastrow)
Dim iRow As Variant
For Each iRow In orderrows
Dim j As Variant
    For Each j In s1.Range("Q:DL")
    locationRow = 1
    Dim i As Variant
        For Each i In s2.Range("B1:JE1")
            If s1(iRow, j) = s2(locationRow, i) Then
            s2(iRow, i) = 1 'indicates that this order features a line from this location
            Else: s2(iRow, i) = 0
            End If
        Next i
    Next j
Next iRow
End Sub

我是VBA的新手,我们将非常感谢帮助指导。

1 个答案:

答案 0 :(得分:0)

尝试下面修复的代码。请原谅我没有阅读业务上下文(稍微匆忙),只修复了脚本中的语法问题....

Sub PopulateData()
Dim s1 As Worksheet
Dim s2 As Worksheet
Dim locationRow As Integer
Set s1 = Sheets("Order_LVL")
Set s2 = Sheets("sheet1")
Dim orderrows As Range
Dim Lastrow As Integer
Lastrow = s1.Cells(Rows.Count, 1).End(xlUp).Row
Set orderrows = Range("B2" & Lastrow)
Dim iRow As Integer

For iRow = 1 To orderrows.Rows.Count
Dim cellj As Range
    For Each cellj In s1.Range("Q:DL")
    locationRow = 1
    Dim celli As Range
        For Each celli In s2.Range("B1:JE1")
            If s1.Cells(iRow, cellj.Column).Value = s2.Cells(locationRow, celli.Column).Value Then
            s2.Cells(iRow, celli.Column).Value = 1 'indicates that this order features a line from this location
            Else: s2.Cells(iRow, celli).Value = 0
            End If
        Next celli
    Next cellj
Next iRow
End Sub