代码如下。。。
Sub test()
Dim wb As Workbook, sht As Worksheet
Application.ScreenUpdating = False
Set wb = ThisWorkbook
Set sh1 = wb.Sheets("血常规")
Set sh2 = wb.Sheets("生化")
Set sh3 = wb.Sheets("名单")
Set sh4 = wb.Sheets("汇总")
Set sh5 = wb.Sheets("想要的效果")
arr = sh1.[a1].CurrentRegion
brr = sh2.[a1].CurrentRegion
crr = sh3.[a1].CurrentRegion
frr = sh5.[a1].CurrentRegion
grr = Application.Index(frr, 1)
ReDim hrr(1 To UBound(frr) - 1, 1 To 2)
n = 0
Set d = CreateObject("scripting.dictionary")
ReDim drr(1 To UBound(frr) - 1, 1 To UBound(frr, 2))
For i = 2 To UBound(frr)
If frr(i, 1) <> Empty Then n = n + 1: d(frr(i, 1)) = n: drr(n, 1) = frr(i, 1)
If sh5.Cells(i, 1).Interior.Color <> 0 Then hrr(i - 1, 1) = i: hrr(i - 1, 2) = sh5.Cells(i, 1).Interior.Color
Next
For i = 2 To UBound(crr)
If d.exists(crr(i, 1)) Then
For j = 2 To UBound(crr, 2)
drr(d(crr(i, 1)), j) = crr(i, j)
Next
End If
Next
For i = 2 To UBound(arr)
If arr(i, 1) = Empty Then arr(i, 1) = arr(i - 1, 1)
If d.exists(Trim(arr(i, 1))) And arr(i, 2) <> Empty Then
x = d(Trim(arr(i, 1))): y = Application.Match(arr(i, 2), grr, 0)
drr(x, y) = arr(i, 3)
End If
Next
For i = 2 To UBound(brr)
If brr(i, 1) = Empty Then brr(i, 1) = brr(i - 1, 1)
If d.exists(Trim(brr(i, 1))) And brr(i, 2) <> Empty Then
x = d(Trim(brr(i, 1))): y = Application.Match(brr(i, 2), grr, 0)
drr(x, y) = brr(i, 3)
End If
Next
With sh5
.UsedRange.Clear
.[a1].Resize(UBound(frr), UBound(frr, 2)) = frr
.Cells(2, 1).Resize(UBound(drr), UBound(drr, 2)).NumberFormat = "@"
.Cells(2, 1).Resize(n, UBound(drr, 2)) = drr
.Cells(1, 1).Resize(n + 1, UBound(drr, 2)).Borders.LineStyle = 1
.Cells(1, 1).Resize(n + 1, UBound(drr, 2)).HorizontalAlignment = xlCenter
.Cells(1, 1).Resize(n + 1, UBound(drr, 2)).Columns.AutoFit
For i = 1 To UBound(hrr)
If hrr(i, 1) <> Empty Then .Cells(i + 1, 1).Resize(, UBound(drr, 2)).Interior.Color = hrr(i, 2)
Next
End With
Beep
Set d = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|