Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub AddHeadersLoadsSheets()
- 'Created by Lionel Jamaigne on 12/10/2018
- RefreshRanges HeaderLoads 'Refresh what is to be copied
- Dim ws_active As Worksheet: Set ws_active = ActiveSheet
- Dim sheet_name_parts() As String: sheet_name_parts = Split(ws_active.Name, " - ")
- Dim sheet_number As Integer: sheet_number = CInt(sheet_name_parts(0)) 'Retrieving current sheet number
- Dim row As Integer: row = 6
- Dim i As Integer: i = 1
- Dim temp As Range
- 'Check for any exchanger and then copy 1st headersloads sheet after 1st one
- While ws_active.Cells(row, 1).Value2 <> "" And Len(ws_active.Cells(row, 1).Value2) = 4
- sheet_number = sheet_number + 1
- ws_active.Copy After:=Worksheets(ws_active.Index + (i - 1)) 'copy current sheet
- ActiveSheet.Name = sheet_number & " - " & sheet_name_parts(1) 'change name of copied sheet
- CleanHeaderLoadsTable Worksheets(ws_active.Index + i).Name
- 'Looking for exchanger name occurence that need to be change to the choosen exchanger
- With Worksheets(ws_active.Index + i).Cells
- Set temp = .Find(ws_active.Cells(5, 1).Value2, LookIn:=xlValues) 'looking for an occurence
- While Not temp Is Nothing 'if it found something
- temp.Value2 = Replace(temp.Value2, ws_active.Cells(5, 1).Value2, ws_active.Cells(row, 1).Value2)
- Set temp = .FindNext() 'looking for next occurence
- Wend
- End With
- i = i + 1
- row = row + 1
- Wend
- If ws_active.Cells(row, 1).Value2 <> "" And Len(ws_active.Cells(row, 1).Value2) <> 4 Then
- MsgBox "Exchanger name doesn't fit requirements ! -> " & ws_active.Cells(row, 1).Value2
- Exit Sub
- End If
- 'Need to change sheets number if some sheets were added
- If i > 1 Then
- 'checking every sheet after active one, which is the last added
- For i = ActiveSheet.Index + 1 To ThisWorkbook.Sheets.Count
- Set ws_active = Worksheets(i)
- 'not all sheets have a number in their name
- If Split(ws_active.Name, " - ")(0) <> ws_active.Name Then
- ws_active.Name = i & " - " & Split(ws_active.Name, " - ")(1)
- End If
- Next i
- Else
- MsgBox "Nothing to add here !"
- End If
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement