'Maakt nieuwe formule aan om te kijken of sheet al bestaat
Function SheetExists(shtName As String, Optional wb As Workbook) As Boolean
Dim sht As Worksheet
'bestand = UserForm1.ComboBox1.Value
bestand = "test1"
Set wb = Workbooks(bestand)
On Error Resume Next
Set sht = wb.Sheets(shtName)
On Error GoTo 0
SheetExists = Not sht Is Nothing
End Function
Sub stap1()
Application.EnableEvents = False
'bestand = UserForm1.ComboBox1.Value
bestand = "test1"
With Workbooks(bestand)
'nieuwe WS aannmaken, als sheet niet al bestaat. Zowel, voeg nummer toe
Dim csheet As String
If SheetExists("Componenten") = False Then
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Componenten"
csheet = "Componenten"
Else
For i = 2 To 15
If SheetExists("Componenten" & i) = False Then
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Componenten" & i
csheet = "Componenten" & i
Exit For
End If
Next i
End If
'Kopiëer bovenste kolom van sheet 1
LCol = ThisWorkbook.Sheets(1).Rows("72:72").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column
ThisWorkbook.Sheets(1).Range(ThisWorkbook.Sheets(1).Cells(72, 11), ThisWorkbook.Sheets(1).Cells(72, LCol)).Copy
.Sheets(csheet).Range(.Sheets(csheet).Cells(1, 1), .Sheets(csheet).Cells(1, LCol)).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
.Sheets(csheet).Rows(1).AutoFilter
'Zoeken naar eerste "Originals" in Code kolom
Set CodeCol = .Sheets(1).Range("A1:L1").Find("Code")
Origcell = .Sheets(1).Columns(CodeCol.Column).Find("Original*").Row
'kopiëren van kolommen vanaf OrigCell row
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, -1), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 0)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("A2")) 'Tree level & Code
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 2), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 2)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("D2")) 'Stock Item Commodity Code
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 6), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 6)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("H2")) 'Description
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 9), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 9)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("I2")) 'Stock Item Description
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 4), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 4)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("F2")) 'Quantity
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 1), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 1)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("C2")) 'Specification
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 5), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 5)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("G2")) 'posnr
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 10), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 10)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("K2")) 'Parent Component Code
.Sheets(1).Range(.Sheets(1).Cells(Origcell, CodeCol.Column).Offset(0, 3), .Sheets(1).Cells(Origcell, CodeCol.Column).Offset(.Sheets(1).Cells(.Sheets(1).Rows.Count, CodeCol.Column).End(xlUp).Row, 3)).Copy
.Sheets(csheet).Paste (.Sheets(csheet).Range("E2")) 'Category Code
LRow = .Sheets(csheet).Cells(.Sheets(csheet).Rows.Count, "B").End(xlUp).Row
LCol = .Sheets(csheet).Cells(1, .Sheets(csheet).Columns.Count).End(xlToLeft).Column
'invoeren formules kolommen J-N
.Sheets(csheet).Range("J2:J" & LRow).FormulaR1C1 = _
"=CONCATENATE(RC[-3],"" - "",RC[-1],"" ("",RC[-6],"")"")"
.Sheets(csheet).Range("L2:L" & LRow).FormulaR1C1 = _
"=IF(RC[1]<>"""",R[-1]C,CONCATENATE(RC[-1],""_"",VLOOKUP(RC[-1],C[-10]:C[-9],2,0)))"
.Sheets(csheet).Range("M2:M" & LRow).FormulaR1C1 = _
"=IFERROR(IFS(AND(NOT(RC[-8]=""""),RC[-11]=R[1]C[-2]),"""",LOOKUP(2,1/(R[-30]C[-2]:R[-1]C[-2]<>RC[-2]),R[-30]C[-8]:R[-1]C[-8])<>""SA-NP"","""",IFERROR(LOOKUP(2,1/(R[-30]C[-2]:R[-1]C[-2]<>RC[-2]),R[-30]C[-1]:R[-1]C[-1]),"""")<>"""",RC[-2]),"""")"
.Sheets(csheet).Range("N2:N" & LRow).FormulaR1C1 = _
"=IF(AND(R[1]C[-13]>RC[-13],RC[-10]=R[1]C[-10]),VLOOKUP(RC[-3],C[-12]:C[-8],5,0)*RC[-8],VLOOKUP(RC[-3],C[-12]:C,13,0)*RC[-8])"
'Kleuren, randjes kolommen
For Each Column In Range(.Sheets(csheet).Cells(1, 15), .Sheets(csheet).Cells(1, LCol))
If Column.Column Mod 2 = 1 Then
.Sheets(csheet).Columns(Column.Column).Interior.PatternColorIndex = xlAutomatic
.Sheets(csheet).Columns(Column.Column).Interior.ThemeColor = xlThemeColorDark1
.Sheets(csheet).Columns(Column.Column).Interior.TintAndShade = -4.99893185216834E-02
.Sheets(csheet).Columns(Column.Column).Interior.PatternTintAndShade = 0
End If
Next
.Sheets(csheet).Columns("N:N").Interior.PatternColorIndex = xlAutomatic
.Sheets(csheet).Columns("N:N").Interior.ThemeColor = xlThemeColorAccent2
.Sheets(csheet).Columns("N:N").Interior.TintAndShade = 0.799981688894314
.Sheets(csheet).Columns("N:N").Interior.PatternTintAndShade = 0
.Sheets(csheet).Activate
.Sheets(csheet).Range(.Sheets(csheet).Cells(1, 14), .Sheets(csheet).Cells(LRow, LCol)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = xlAutomatic
.TintAndShade = 0
.Weight = xlHairline
End With
With .Sheets(csheet).Range(Cells(2, 1), Cells(LRow, LCol))
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=3,$D2=""SA"")"
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=4,$D2=""SA"")"
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=5,$D2=""SA"")"
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=6,$D2=""SA"")"
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=7,$D2=""SA"")"
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($A2=8,$D2=""SA"")"
With .FormatConditions(1)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(252, 228, 214)
.TintAndShade = 0
End With
End With
With .FormatConditions(2)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(226, 239, 218)
.TintAndShade = 0
End With
End With
With .FormatConditions(3)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(217, 225, 242)
.TintAndShade = 0
End With
End With
With .FormatConditions(4)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 242, 204)
.TintAndShade = 0
End With
End With
With .FormatConditions(5)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 204, 255)
.TintAndShade = 0
End With
End With
With .FormatConditions(6)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(153, 255, 102)
.TintAndShade = 0
End With
End With
End With
'Bovenste rij scrollt mee, kolombreedte aangepast, bovenste rij verticale tekst
With .Sheets(csheet).Rows("1:1")
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 90
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
.Sheets(csheet).Activate
ActiveWindow.SplitRow = 1
ActiveWindow.FreezePanes = True
Columns("B:M").EntireColumn.AutoFit
Columns("A").ColumnWidth = 3.3
Columns("G").ColumnWidth = 2
Columns("I").ColumnWidth = 40
Columns("J").ColumnWidth = 50
Columns("N").ColumnWidth = 7.5
Columns(15).Resize(, LCol).ColumnWidth = 3.6
'>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<'
'>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<'
''''''''''''''''''''GELE SHEET''''''''''''''''''''''
'>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<'
'>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<'
'nieuwe WS aannmaken, als sheet niet al bestaat. Zowel, voeg nummer toe
Dim gsheet As String
If SheetExists("AssetTypeTask") = False Then
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "AssetTypeTask"
gsheet = "AssetTypeTask"
Else
For i = 2 To 15
If SheetExists("AssetTypeTask" & i) = False Then
.Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "AssetTypeTask" & i
gsheet = "AssetTypeTask" & i
Exit For
End If
Next i
End If
'Rijen voor nieuwe sheet kopiëren plakken
LCol = ThisWorkbook.Sheets(1).Cells(70, ThisWorkbook.Sheets(1).Columns.Count).End(xlToLeft).Column
ThisWorkbook.Sheets(1).Range(ThisWorkbook.Sheets(1).Cells(70, 11), ThisWorkbook.Sheets(1).Cells(70, LCol)).Copy
Workbooks(bestand).Sheets(gsheet).Paste
'bovenste rij scrollt mee, autofilter en kolombreedte
.Sheets(gsheet).Columns("A:AU").EntireColumn.AutoFit
.Sheets(gsheet).Columns("B:B").ColumnWidth = 28
.Sheets(gsheet).Columns("D:D").ColumnWidth = 42
.Sheets(gsheet).Columns("E:E").ColumnWidth = 42
.Sheets(gsheet).Rows(1).AutoFilter
.Sheets(gsheet).Activate
ActiveWindow.SplitRow = 1
ActiveWindow.FreezePanes = True
End With
NameGrabber.DefineWorkbookName
End Sub