|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test1()
- Dim strFile As String
- ' With Application.FileDialog(msoFileDialogOpen)
- ' .InitialFileName = ThisWorkbook.Path
- ' With .Filters
- ' .Clear
- ' .Add "Excel文件(xls*)", "*.xls*"
- ' End With
- ' .AllowMultiSelect = False
- ' If .Show Then strFile = .SelectedItems(1) Else Exit Sub
- ' End With
- strFile = ThisWorkbook.Path & "\信息.xlsm"
- If Dir(strFile) = "" Then MsgBox strFile & " 文件不存在!", 64: Exit Sub
-
- ' Dim Conn As New ADODB.Connection, rs As New ADODB.Recordset
- Dim Conn As Object, rs As Object, Dic As Object, Dict As Object
- Dim strConn As String, SQL As String
- Dim ar, br, cr() As Long
- Dim i As Long, j As Long, pos As Long
-
- With Range("A1").CurrentRegion
- ar = Application.Rept(.Rows(2), 1)
- br = .Offset(2).Resize(366)
- ReDim cr(1 To UBound(ar))
- End With
-
- Set Dic = CreateObject("Scripting.Dictionary")
- 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
- Set Dic(ar(j)) = CreateObject("Scripting.Dictionary")
- For i = 1 To cr(j)
- Dic(ar(j)).Add br(i, j), vbNullString
- Next
- Next
-
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & strFile
- SQL = "SELECT * FROM [单价$B1:E] WHERE 姓名 IS NOT NULL ORDER BY 姓名"
- rs.Open SQL, Conn, 1, 3
-
- ar = rs.GetRows()
- For j = 0 To UBound(ar, 2)
- If Dict.Exists(ar(0, j)) Then
- If Not Dic(ar(0, j)).Exists(ar(1, j)) Then
- pos = Dict(ar(0, j))
- cr(pos) = cr(pos) + 1
- For i = 1 To UBound(ar)
- br(cr(pos), pos + i - 1) = ar(i, j)
- Next
- End If
- End If
- Next
- Range("A13").Resize(WorksheetFunction.Max(cr), UBound(br, 2)) = br '改为 Range("A3")
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Dict = Nothing
- Set Dic = Nothing
- Beep
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|