Никита Дворец, я сейчас метрики столбец (D) и содержание столбец (E) сделала уникальными, ну просто протянула
вот что получилось благодаря нейросетям
Скрытый текст |
---|
Sub АвтозаполнениеДашборда() |
, но не совсем корректно отрабатывает:
Код |
---|
Sub АвтозаполнениеДашборда()
Dim wsDashboard As Worksheet
Set wsDashboard = ThisWorkbook.Sheets(1)
Dim Path As String, FileName As String
Path = ThisWorkbook.Path & "\"
Dim wbSource As Workbook, wsSource As Worksheet
Dim DashLastRow As Long, DashRow As Long
DashLastRow = wsDashboard.Cells(wsDashboard.Rows.Count, "D").End(xlUp).Row
Dim DashDateTitle As String
DashDateTitle = wsDashboard.Range("K1").Value
Dim Done As Long
Application.ScreenUpdating = False
Application.StatusBar = "Заполнение начато..."
frmStatusBar.lblProgress.Width = 0
frmStatusBar.lblPercent.Caption = "0%"
frmStatusBar.lblPercent.Left = frmStatusBar.FrameBar.Left + (frmStatusBar.FrameBar.Width - frmStatusBar.lblPercent.Width) / 2
frmStatusBar.lblStatus.Caption = "Заполнение..."
frmStatusBar.Show vbModeless
For DashRow = 2 To DashLastRow
Dim DashMetrika As String, DashSod As String
DashMetrika = Trim(wsDashboard.Cells(DashRow, "D").Value)
DashSod = Trim(wsDashboard.Cells(DashRow, "E").Value)
If DashMetrika = "" Or DashSod = "" Then GoTo SkipDashRow
Dim FoundValue As Variant, FoundPct As Variant, FoundConf As Variant
FoundValue = "": FoundPct = "": FoundConf = ""
Dim IsFound As Boolean: IsFound = False
FileName = Dir(Path & "*.xls*")
Do While FileName <> ""
If FileName <> ThisWorkbook.Name Then
Set wbSource = Workbooks.Open(Path & FileName, ReadOnly:=True)
Set wsSource = wbSource.Sheets(1)
Dim DateCol As Integer, Col As Integer
DateCol = 0
For Col = 1 To wsSource.Cells(1, wsSource.Columns.Count).End(xlToLeft).Column
If InStr(wsSource.Cells(1, Col).Text, DashDateTitle) > 0 Then
DateCol = Col
Exit For
End If
Next Col
If DateCol > 0 Then
Dim SrcLastRow As Long, SrcRow As Long
SrcLastRow = wsSource.Cells(wsSource.Rows.Count, "D").End(xlUp).Row
For SrcRow = 2 To SrcLastRow
Dim SrcMetrika As String, SrcSod As String
SrcMetrika = Trim(wsSource.Cells(SrcRow, "D").Value)
SrcSod = Trim(wsSource.Cells(SrcRow, "E").Value)
If SrcMetrika = DashMetrika And SrcSod = DashSod Then
FoundValue = wsSource.Cells(SrcRow, DateCol).Value
FoundPct = wsSource.Cells(SrcRow, DateCol + 1).Value
FoundConf = wsSource.Cells(SrcRow, DateCol + 2).Value
IsFound = True
Exit For
End If
Next SrcRow
End If
wbSource.Close False
If IsFound Then Exit Do
End If
FileName = Dir
Loop
If IsFound Then
wsDashboard.Cells(DashRow, "K").Value = FoundValue
wsDashboard.Cells(DashRow, "L").Value = FoundPct
wsDashboard.Cells(DashRow, "M").Value = FoundConf
End If
SkipDashRow:
Done = DashRow - 1
Dim Progress As Double
Progress = Done / (DashLastRow - 1)
Application.StatusBar = "Заполнение: " & Format(Progress, "0%") & _
" (" & Done & " из " & DashLastRow - 1 & ")"
frmStatusBar.lblProgress.Width = frmStatusBar.FrameBar.Width * Progress
frmStatusBar.lblPercent.Caption = Format(Progress, "0%")
frmStatusBar.lblPercent.Left = frmStatusBar.FrameBar.Left + (frmStatusBar.FrameBar.Width - frmStatusBar.lblPercent.Width) / 2
frmStatusBar.lblStatus.Caption = "Обработка строки: " & DashRow & " из " & DashLastRow
DoEvents
Next DashRow
Application.StatusBar = False
Application.ScreenUpdating = True
Unload frmStatusBar
MsgBox "Заполнение завершено!", vbInformation
End Sub |