|
- Sub test3() '不用SQL获取数据
- Dim strFile As String
- strFile = ThisWorkbook.Path & "\信息.xlsm"
- If Dir(strFile) = "" Then MsgBox strFile & " 文件不存在!", 64: Exit Sub
-
- Application.ScreenUpdating = False
-
- Dim ar, br, cr() As Long, Dict As Object
- Dim i As Long, j As Long, pos As Long, iMax As Long
-
- iMax = 366
- With Range("A1").CurrentRegion
- ar = Application.Rept(.Rows(2), 1)
- br = .Offset(2).Resize(iMax)
- ReDim cr(1 To UBound(ar))
- End With
-
- Set Dict = CreateObject("Scripting.Dictionary")
- For j = 1 To UBound(ar) Step 3
- Dict.Add ar(j), j
- cr(j) = Cells(1, j).End(xlDown).Row - 2
- br(iMax, j) = "|"
- For i = 1 To cr(j)
- br(iMax, j) = br(iMax, j) & br(i, j) & "|" & br(i, j + 1) & "|" & br(i, j + 2) & "|"
- Next
- Next
-
- With Workbooks.Open(strFile, False)
- ar = .Worksheets("单价").Range("A1").CurrentRegion.Offset(, 1).Resize(, 4)
- .Close False
- End With
- For i = 2 To UBound(ar)
- If Dict.Exists(ar(i, 1)) Then
- pos = Dict(ar(i, 1))
- If InStr(br(iMax, pos), "|" & ar(i, 2) & "|" & ar(i, 3) & "|" & ar(i, 4) & "|") = 0 Then
- cr(pos) = cr(pos) + 1
- For j = 2 To UBound(ar, 2)
- br(cr(pos), pos + j - 2) = ar(i, j)
- br(iMax, pos) = br(iMax, pos) & ar(i, j) & "|"
- Next
- End If
- End If
- Next
-
- Range("A13").Resize(WorksheetFunction.Max(cr), UBound(br, 2)) = br '改为 Range("A3")
-
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
3
查看全部评分
-
|