试下下面的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Dim str1 As String, i As Integer, d As Range, firstaddress As String
If Target.Address = "$D$3" Or Target.Address = "$E$3" Then
Rows("5:65536").Clear
If [d3] <> "" And [e3] <> "" Then
str1 = Trim([d3].Value)
With Worksheets("出库")
Set d = .Range("b4", .[b65536].End(xlUp)).Find(str1, LookIn:=xlValues)
If Not d Is Nothing Then
firstaddress = d.Address
i = 5
Do
If [e3].Value = "全部" Then
Range("b" & i & ":M" & i) = d.Resize(1, 12).Value
i = i + 1
Else
If Month(d(1, 2)) = [e3].Value Then
Range("b" & i & ":M" & i) = d.Resize(1, 12).Value
i = i + 1
End If
End If
Set d = .Range("b4", .[b65536].End(xlUp)).FindNext(d)
Loop While Not d Is Nothing And d.Address <> firstaddress
End If
End With
End If
End If
Application.EnableEvents = True
End Sub
事例:http://ys-d.ys168.com/?原材料VBA.rar_68dks7bshkis7bsr0cl0cn0bktl4b5bks0c2bs0c2bu14z97f14z
文件在哪?
嘿嘿。。加我吧,5分钟应该够了。。
设置好一个Vlookup函数,即使拖满整张表,估计也不需5分钟噢,何况还可以双击填充柄快速复制公式,以及使用CTRL+回车在选中区域快速复制公式等方法。
有函数可用,不必迷信VBA。
设a列输入数据,后面诸列自动从sheet2中选择数据填充。
选中b-n列,输入
=if(iserror(vlookup(a1,sheet2!a:n,column(),0)),"",vlookup(a1,sheet2!a:n,column(),0)),CTRL+回车。
如果有标题行(表头),删掉第一行公式,输入标题行内容(或复制粘贴sheet2的表头即可)。