在存储在工作表1中的VB函数中使用工作表2
问题描述:
我想比较两个不同工作表中2列的值,如果值相等,则第一个工作表中相应的单元格将在相应的单元格中取相同的值在第二张纸上。所以我使用了一个循环函数,但问题是我无法在第一个工作表中的函数中定义和使用第二个工作表。在存储在工作表1中的VB函数中使用工作表2
我会很感激指导我解决这个问题。
你会发现以下的功能我想使用:
Sub Test_V01()
Dim i As Integer
Dim j As Integer
Dim Bouira As Excel.Worksheet
For j = 3 To 100
For i = 3 To 120
If Cells(j, 3) = Bouira!Cells(i, 30) Then
Cells(j, 12) = Bouira!Cells(i, 31)
End If
Next i
Next j
End Sub
答
分别替换
Cells(j, 3)
和Cells(j, 12)
与Me.Cells(j, 3)
和Me.Cells(j, 12)
,以确保始终使用该片中的细胞,而不是细胞来自活动工作表。-
将
Bouira!Cells
替换为Bouira.Cells
。在表中没有收集其元素的文字值为Cells
。Set Bouira = ThisWorkbook.Worksheets("Sheet name")
答
这将设置引用到工作表Bouira
:
Sub Test_V01()
Dim i As Integer, j As Integer, Bouira As Worksheet
Set Bouira = Worksheets("Bouira")
For j = 3 To 100
For i = 3 To 120
If Cells(j, 3) = Bouira.Cells(i, 30) Then
Cells(j, 12) = Bouira.Cells(i, 31)
End If
Next i
Next j
End Sub
+0
非常感谢您的帮助,它完美运作 – user1317100 2012-04-06 10:16:16
非常感谢,因为我有自己的想法 – user1317100 2012-04-06 11:31:58
+1进入循环前
分配参考
Bouira
努力实际解释它! – 2012-04-06 12:26:55