|
工作簿采用动态选择,分表名改变也不用改代码了。- Sub ykcbf() '//2024.3.4
- Set Fso = CreateObject("scripting.filesystemobject")
- Set d = CreateObject("Scripting.Dictionary")
- Application.ScreenUpdating = False
- Set ws = ThisWorkbook
- Set sh = ws.Sheets("报修记录")
- With sh
- col = .Cells.Find("*", SearchOrder:=xlByColumns, searchdirection:=xlPrevious).Column
- For j = 1 To col
- s = .Cells(1, j)
- d(s) = j
- Next
- End With
- p = ThisWorkbook.Path & ""
- ReDim brr(1 To 10000, 1 To 100)
- With Application.FileDialog(msoFileDialogFilePicker)
- .InitialFileName = p
- .Title = "请选择对应Excel文件"
- .AllowMultiSelect = False
- .Filters.Clear
- .Filters.Add "Excel文件", "*.xls*"
- If .Show Then f = .SelectedItems(1) Else Exit Sub
- End With
- Set wb = Workbooks.Open(f, 0)
- With wb.Sheets(1)
- c = .Cells.Find("*", SearchOrder:=xlByColumns, searchdirection:=xlPrevious).Column
- c1 = .Rows(1).Find("故障描述1", , , , , 1).Column
- r = .Cells(Rows.Count, c1).End(3).Row
- arr = .[a1].Resize(r, c)
- wb.Close False
- End With
- For i = 2 To UBound(arr)
- m = m + 1
- For j = 1 To UBound(arr, 2)
- s = arr(1, j)
- If d.exists(s) Then
- If j = c1 Then
- brr(m, d(s)) = arr(i, j) & arr(i, j + 1)
- Else
- brr(m, d(s)) = arr(i, j)
- End If
- End If
- Next
- Next
- With sh
- .UsedRange.Offset(1).Clear
- With .[a2].Resize(m, col)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End With
- Application.ScreenUpdating = True
- Set d = Nothing
- MsgBox "OK!"
- End Sub
复制代码
|
|