Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.35 KB | None | 0 0
  1. Sub AddHeadersLoadsSheets()
  2.     'Created by Lionel Jamaigne on 12/10/2018
  3.  
  4.     RefreshRanges HeaderLoads 'Refresh what is to be copied
  5.  
  6.     Dim ws_active As Worksheet: Set ws_active = ActiveSheet
  7.     Dim sheet_name_parts() As String: sheet_name_parts = Split(ws_active.Name, " - ")
  8.     Dim sheet_number As Integer: sheet_number = CInt(sheet_name_parts(0)) 'Retrieving current sheet number
  9.     Dim row As Integer: row = 6
  10.    
  11.     Dim i As Integer: i = 1
  12.     Dim temp As Range
  13.    
  14.     'Check for any exchanger and then copy 1st headersloads sheet after 1st one
  15.     While ws_active.Cells(row, 1).Value2 <> "" And Len(ws_active.Cells(row, 1).Value2) = 4
  16.         sheet_number = sheet_number + 1
  17.         ws_active.Copy After:=Worksheets(ws_active.Index + (i - 1)) 'copy current sheet
  18.         ActiveSheet.Name = sheet_number & " - " & sheet_name_parts(1) 'change name of copied sheet
  19.         CleanHeaderLoadsTable Worksheets(ws_active.Index + i).Name
  20.        
  21.         'Looking for exchanger name occurence that need to be change to the choosen exchanger
  22.         With Worksheets(ws_active.Index + i).Cells
  23.             Set temp = .Find(ws_active.Cells(5, 1).Value2, LookIn:=xlValues) 'looking for an occurence
  24.            
  25.             While Not temp Is Nothing 'if it found something
  26.                 temp.Value2 = Replace(temp.Value2, ws_active.Cells(5, 1).Value2, ws_active.Cells(row, 1).Value2)
  27.                 Set temp = .FindNext() 'looking for next occurence
  28.             Wend
  29.         End With
  30.        
  31.         i = i + 1
  32.         row = row + 1
  33.     Wend
  34.    
  35.     If ws_active.Cells(row, 1).Value2 <> "" And Len(ws_active.Cells(row, 1).Value2) <> 4 Then
  36.         MsgBox "Exchanger name doesn't fit requirements ! -> " & ws_active.Cells(row, 1).Value2
  37.         Exit Sub
  38.     End If
  39.    
  40.     'Need to change sheets number if some sheets were added
  41.     If i > 1 Then
  42.         'checking every sheet after active one, which is the last added
  43.         For i = ActiveSheet.Index + 1 To ThisWorkbook.Sheets.Count
  44.             Set ws_active = Worksheets(i)
  45.            
  46.             'not all sheets have a number in their name
  47.             If Split(ws_active.Name, " - ")(0) <> ws_active.Name Then
  48.                 ws_active.Name = i & " - " & Split(ws_active.Name, " - ")(1)
  49.             End If
  50.         Next i
  51.     Else
  52.         MsgBox "Nothing to add here !"
  53.     End If
  54. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement