Temat: Pomoc w napisaniu Makra lub formuł
Nad VBA głowię się od niedawna, ale też udało mi się napisać kod ("na około"), chociaż Panowie byliście szybszy:)
Skoro napisałem to wklejam, może komuś się kiedyś ta prosta metodologia przyda.
Raport tworzy się w 2 arkuszu.
pozdrawiam,
Sub Kopiowanie_warunkowe()
Application.ScreenUpdating = False
Sheets(1).Range("a1").CurrentRegion.Columns(7).Offset(1, 0).Copy _
Sheets(2).Range("p1")
Sheets(2).Range("p1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlNo
Sheets(2).Range("p1").CurrentRegion.Copy
Range("m1").PasteSpecial Transpose:=True
Columns(16).ClearContents
Sheets(1).Range("a1:l1").Copy Sheets(2).Range("a1")
Sheets(1).Columns("B:B").Copy Sheets(2).Range("B1")
Sheets(2).Range("a1").CurrentRegion.RemoveDuplicates Columns:=Array(2), _
Header:=xlYes
Dim i As Integer
For i = 2 To Range("a1").CurrentRegion.Rows.Count
Cells(i, 8).Value = WorksheetFunction.CountIf(Sheets(1).Columns(2), Cells(i, 2).Value)
Cells(i, 9).Value = WorksheetFunction.CountIfs(Sheets(1).Columns(2), Cells(i, 2).Value, Sheets(1).Columns(7), Sheets(2).Cells(1, 13))
Cells(i, 10).Value = WorksheetFunction.CountIfs(Sheets(1).Columns(2), Cells(i, 2).Value, Sheets(1).Columns(7), Sheets(2).Cells(1, 14))
Cells(i, 11).Value = WorksheetFunction.CountIfs(Sheets(1).Columns(2), Cells(i, 2).Value, Sheets(1).Columns(7), Sheets(2).Cells(1, 15))
Cells(i, 12).Value = WorksheetFunction.Sum(Cells(i, 9) * Cells(1, 13), Cells(i, 10) * Cells(1, 14), Cells(i, 11) * Cells(1, 15))
Sheets(1).Range("a1").CurrentRegion.AutoFilter Field:=2, Criteria1:=Sheets(2).Cells(i, 2)
Dim j As Integer
For j = 13 To 15
Sheets(1).Range("a1").CurrentRegion.AutoFilter Field:=7, Criteria1:=Sheets(2).Cells(1, j).Text
Sheets(1).Range("a1").CurrentRegion.Columns(5).Offset(1, 0).Copy Sheets(2).Cells(2, j)
Dim k As Integer
k = 2
Dim w As String
w = ""
If Len(Cells(k, j)) > 0 Then
Do
If k = 2 Then
w = Cells(k, j).Value
Else
w = w & "," & Cells(k, j).Value
End If
k = k + 1
Loop Until Cells(k, j) = ""
w = "NazwaUsługi" & " " & Cells(1, j - 4) & " " & Cells(i, j - 4) & " " & "x" & " " & Cells(1, j) & " " & "(" & w & ")"
'odfiltrowanie ceny z 1 arkusza
Sheets(1).Range("a1").CurrentRegion.AutoFilter Field:=7
Cells(j, 16).Value = w
End If
Next j
Sheets(1).Range("a1").CurrentRegion.AutoFilter Field:=2
Sheets(1).Range("a1").CurrentRegion.AutoFilter Field:=7
Sheets(2).Cells(i, 4) = Cells(13, 16).Value + vbNewLine + Cells(14, 16).Value + vbNewLine + Cells(15, 16).Value
Sheets(2).Range("a1").CurrentRegion.Columns("m:o").Offset(1, 0).ClearContents
Sheets(2).Range("p13:p15").ClearContents
Next i
Sheets(2).Columns("M:P").Delete
Columns("D:D").ColumnWidth = 40
Cells.EntireRow.AutoFit
Columns.EntireColumn.AutoFit
Application.ScreenUpdating = True
End Sub