|
本帖最后由 mzbao 于 2017-5-28 20:46 编辑
见附件
你在【每日需要更新的工作表】的B列输入的时候,代码会自动填写后面的内容
在B列直接粘贴多个,代码也可以自动填写多行
Sheet1中代码
- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim Rng As Range
- If Target.Columns.Count = 1 Then
- If Target.Column = 2 Then
- For Each Rng In Target
- If Len(Rng.Value) Then Call WriteData(Rng)
- Next
- End If
- End If
- End Sub
复制代码
模块里代码
- Public d
- Sub ReadDataDic()
- Dim Path$, wb As Object, iRow%, wbArr, i%
-
- Path = ThisWorkbook.Path
- Set d = CreateObject("Scripting.Dictionary")
- Set wb = GetObject(Path & "\源数据表.xlsx")
- With wb.Sheets("Sheet1")
- iRow = .Cells(.Rows.Count, 1).End(xlUp).Row
- If iRow < 2 Then Exit Sub
- wbArr = .Range("A2:G" & iRow).Value
- End With
- wb.Close
- For i = 1 To iRow - 1
- d(wbArr(i, 2)) = wbArr(i, 4) & "|" & wbArr(i, 5) & "|" & wbArr(i, 6) & "|" & wbArr(i, 3)
- Next
- End Sub
- Sub WriteData(Rng As Range)
- Dim BatchCode$, arr, col%
-
- On Error Resume Next
- If Rng.Count > 1 Then Exit Sub
- d.Count
- If Err.Number = 424 Then Call ReadDataDic
- BatchCode = Rng.Value
- If d.Exists(BatchCode) Then
- arr = Split(d(BatchCode), "|")
- For col = 1 To 4
- Rng.Offset(0, col) = arr(col - 1)
- Next
- Else
- MsgBox "源数据表没有样品批号" & BatchCode & "!", vbExclamation
- End If
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|