Pastebinuser1993

Forecasting

Jun 16th, 2023
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub forecasting()
  2.    
  3.  Dim last_ie As Long, supplier() As String, a As Long, supplier_range As Range
  4.  
  5.     If ThisWorkbook.Worksheets("Macro").Range("BA1").Value = "All suppliers not mapped" Then
  6.         MsgBox "Please refer to the supplier mapping sheet. All suppliers are not mapped."
  7.         Exit Sub
  8.     End If
  9.   Worksheets("Final FC").Range("A2:A10000").ClearContents
  10. Application.ScreenUpdating = False
  11. Application.DisplayAlerts = False
  12. 'IE
  13.    ThisWorkbook.Worksheets("IE").Activate
  14.  
  15.     last_ie = ThisWorkbook.Worksheets("IE").Cells(Rows.Count, 1).End(xlUp).Row
  16.    
  17.     ThisWorkbook.Worksheets("IE").Range("B1").EntireColumn.Insert
  18.     ThisWorkbook.Worksheets("IE").Range("H1").EntireColumn.Insert
  19.     ThisWorkbook.Worksheets("IE").Range("AF2:AF" & last_ie).Copy
  20.     ThisWorkbook.Worksheets("IE").Range("Q2:Q" & last_ie).PasteSpecial Paste:=xlPasteValues
  21.  
  22.     ThisWorkbook.Worksheets("IE").Range("AG2:AG" & last_ie).Copy
  23.     ThisWorkbook.Worksheets("IE").Range("H2:H" & last_ie).PasteSpecial Paste:=xlPasteValues
  24.    
  25.     ThisWorkbook.Worksheets("IE").Range("AC2:AC" & last_ie).Copy
  26.         ThisWorkbook.Worksheets("IE").Range("B2:B" & last_ie).PasteSpecial Paste:=xlPasteValues
  27.     Application.CutCopyMode = False
  28.    
  29.    
  30.     If ActiveSheet.AutoFilterMode = False Then
  31.         ThisWorkbook.Worksheets("IE").Range("$A$1:$AJ$" & last_ie).AutoFilter
  32.     End If
  33.    
  34.         ActiveSheet.Range("$A$1:$AJ$" & last_ie).AutoFilter Field:=17, Criteria1:="1"
  35.   On Error Resume Next
  36.     ActiveSheet.Range("$A$1:$AJ$" & last_ie).Offset(1, 0).SpecialCells _
  37.     (xlCellTypeVisible).EntireRow.Delete
  38.    
  39.   On Error GoTo 0
  40.    
  41.   If ActiveSheet.FilterMode = True Then
  42.     ActiveSheet.ShowAllData
  43.   End If
  44.     last_ie = ThisWorkbook.Worksheets("IE").Cells(Rows.Count, 1).End(xlUp).Row
  45.  i = 0
  46. For a = 2 To last_ie
  47.  
  48.     If InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "/") Then
  49.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "/", " ")
  50.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "\") Then
  51.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "\", " ")
  52.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, """") Then
  53.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, """", " ")
  54.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, ":") Then
  55.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, ":", " ")
  56.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "*") Then
  57.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "*", " ")
  58.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "?") Then
  59.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "?", " ")
  60.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, ">") Then
  61.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, ">", " ")
  62.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "<") Then
  63.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "<", " ")
  64.     ElseIf InStr(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "|") Then
  65.         ThisWorkbook.Worksheets("IE").Cells(a, 15).Value = Replace(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, "|", " ")
  66.     End If
  67.    
  68.   If i = 0 Then
  69.     ReDim supplier(0)
  70.         supplier(0) = ThisWorkbook.Worksheets("IE").Cells(a, 15).Value
  71.         i = i + 1
  72.   ElseIf i <> 0 Then
  73.     If IsError(Application.Match(ThisWorkbook.Worksheets("IE").Cells(a, 15).Value, supplier, 0)) Then
  74.        ReDim Preserve supplier(0 To i)
  75.        supplier(i) = ThisWorkbook.Worksheets("IE").Cells(a, 15).Value
  76.        i = i + 1
  77.     End If
  78.   End If
  79. Next
  80.  
  81. For a = 0 To i - 1
  82. If supplier(a) <> "" Then
  83.   Workbooks.Open ThisWorkbook.Path & "\Working template.xlsx"
  84.   ActiveWorkbook.SaveAs ThisWorkbook.Path & "\Templates\" & supplier(a) & ".xlsx"
  85.   If ThisWorkbook.Worksheets("IE").FilterMode = True Then
  86.     ThisWorkbook.Worksheets("IE").ShowAllData
  87.   End If
  88.     ThisWorkbook.Worksheets("IE").Range("$A$1:$AJ$" & last_ie).AutoFilter Field:=15, Criteria1:=supplier(a)
  89.    ThisWorkbook.Worksheets("IE").Range("A2:Q" & last_ie).SpecialCells(xlCellTypeVisible).Copy
  90.    rowz = ThisWorkbook.Worksheets("IE").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Cells.Count - 1
  91.    Workbooks(supplier(a)).Activate
  92.    Workbooks(supplier(a)).Worksheets(1).Range("A4:Q" & 3 + rowz).PasteSpecial
  93.    
  94.    ThisWorkbook.Worksheets("IE").Range("R2:T" & last_ie).SpecialCells(xlCellTypeVisible).Copy
  95.    Workbooks(supplier(a)).Activate
  96.    Workbooks(supplier(a)).Worksheets(1).Range("V4:X" & 3 + rowz).PasteSpecial
  97.    
  98.    For b = 4 To 3 + rowz
  99.     If Workbooks(supplier(a)).Worksheets(1).Cells(b, 17).Value = "2" Then
  100.         Workbooks(supplier(a)).Worksheets(1).Cells(b, 17).Value = "TBC"
  101.     End If
  102.    Next
  103.    
  104.    Workbooks(supplier(a)).Worksheets(1).Range("A" & 4 + rowz & ":AD100").ClearContents
  105.    Workbooks(supplier(a)).Save
  106.    Workbooks(supplier(a)).Close
  107.    End If
  108. Next
  109.  
  110. ThisWorkbook.Worksheets("Final FC").Range("A2:A" & i + 1).Value = Application.Transpose(supplier)
  111. ThisWorkbook.Worksheets("IE").Activate
  112.  
  113. Set supplier_ie = ThisWorkbook.Worksheets("Final FC").Range("A2:A" & i + 1)
  114.   If ActiveSheet.FilterMode = True Then
  115.     ActiveSheet.ShowAllData
  116.   End If
Add Comment
Please, Sign In to add comment