Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub main() 'This is our main function
  2.    Dim name As String 'We'll start defining our name string, this will be the name of the sheet we'll be dealing
  3.    name = "ND2" 'Setting our worksheet as 'ND2'
  4.    Copy_Table (name) 'This function will copy the data from the worksheet 'name WIP' so in this case is ND2 WIP and paste in the correct fields
  5.    Repair_data 'This function is to repair the data, because some tests in ALM use different names for each stream, for example.
  6.    Apply_filter (name) 'This is a function to apply a filter in our sheet.
  7.    
  8.     name = "NDA" 'Setting our worksheet as 'NDA'
  9.    Copy_Table (name) 'Copying the data from NDA WIP to our sheet
  10.    Repair_data 'Treating our data to standardize it
  11.    Apply_filter (name) 'Lastly filtering from A-z
  12.        
  13.     name = "Overall Graph" 'Setting our worksheet as 'Overall Graph'
  14.    Dim linha As Long 'Defining linha as the line where we should write the next result
  15.    linha = getFreeLine(name, "B") 'This function will get the first free line on the B column
  16.    Call Update_Graph(name, linha) 'Now we call a function to update our graph
  17.    
  18.     Save 'We save the template with the current editions
  19.    Save_As (getFileName(linha)) 'We save the new file using this function as name
  20. End Sub
  21.  
  22. Public Sub Apply_filter(name As String)
  23.     Dim wks As Worksheet
  24.     Set wks = ThisWorkbook.Worksheets(name & " Status") 'get the NAME STATUS worksheet
  25.    wks.Activate 'activate the worksheet (this is to prevent macro from executing in the wrong file or worksheet)
  26.    
  27.    
  28.     wks.AutoFilter.Sort.SortFields.Clear 'clear filter
  29.    wks.AutoFilter.Sort.SortFields.Add Key:= _
  30.         Range("B1:B30000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption _
  31.         :=xlSortNormal 'apply filter on column B using ascending order and normal sorting
  32.    With wks.AutoFilter.Sort
  33.         .Header = xlYes
  34.         .MatchCase = False
  35.         .Orientation = xlTopToBottom
  36.         .SortMethod = xlPinYin 'defining sort based on header and other configurations
  37.        .Apply 'Apply the given filter
  38.    End With
  39. End Sub
  40.  
  41. Public Sub Save()
  42.     ThisWorkbook.Save 'Save our workbook
  43.    MsgBox "Os dados foram importados e a tabela foi salva com sucesso!.", vbInformation, "Save completed." 'Send a message to user informing it was finished
  44. End Sub
  45.  
  46.  
  47. Public Sub Repair_data()
  48.     Dim LastRow As Long 'This variable is to store the last row
  49.    Dim i As Long
  50.     LastRow = Range("A" & Rows.Count).End(xlUp).Row 'Well get the last row from the column A
  51.    For i = 2 To LastRow 'Iterate through all lines
  52.        If Range("B" & i).Value = "AMM" Or Range("B" & i).Value = "TP" Or Range("B" & i).Value = "TNP-AMM" Then 'If B = AMM or TP or TNP-AMM'
  53.            Range("B" & i).Value = "T&P" 'change it to T&P
  54.        ElseIf Range("B" & i).Value = "TNP-QM" Then 'If B = TNP-QM
  55.            Range("B" & i).Value = "QM" 'change it to QM
  56.        End If
  57.         If Range("D" & i).Value = "Passed" Then'If the status is PASSED
  58.            Range("E" & i).Value = "" 'Clear the ART ANALYSIS (we don't need to send our previous analysis if everything is right
  59.        ElseIf Range("D" & i).Value = "Not Completed" Then 'If is not completed
  60.            Range("D" & i).Value = "Failed" 'change it to failed
  61.        End If
  62.     Next i
  63. End Sub
  64.  
  65. Public Sub Copy_Table(name As String)
  66.     Dim wkb As Workbook
  67.     Dim wks As Worksheet
  68.     'Or_wkb = ThisWorkbook.Path & "\" & ThisWorkbook.Name
  69.    Set wkb = Workbooks.Open(ThisWorkbook.path & "\" & name & "_WIP.xlsx") 'Open the NAME_WIP document from the same folder as this workbook
  70.  
  71.     vLinha = 2 'Number of the first line
  72.    Do While Range("C" & vLinha).Value <> "" 'Iterate through all the lines of the C column while they aren't blank
  73.        vLinha = vLinha + 1
  74.     Loop
  75.     vLinha = vLinha - 1 'Eliminate 1 since we don't want the first line (header)
  76.    Range("A2:D" & vLinha).Copy 'Copy everything in the rage of A2 to D:vLinha, where vLinha is the last not blank line
  77.    Set wks = ThisWorkbook.Worksheets(name & " Status") 'Go to NAME Status sheet
  78.    wks.Activate
  79.     wks.Range("B2:E" & vLinha).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 'Use Paste special > values Only - this is to prevent ruining our formating
  80.    wkb.Save 'Save the workbook
  81.    wkb.Close SaveChanges:=False 'Close without saving - I don't remember why I needed to close without saving but trust me, it's necessary.
  82. End Sub
  83.  
  84. Public Function getFreeLine(wrksName As String, field As String) As Long 'This works in a very similar logic as the function above
  85.    Dim wks As Worksheet
  86.     Set wks = ThisWorkbook.Worksheets(wrksName)
  87.     wks.Activate
  88.  
  89.     vLinha = 2
  90.     Do While Range(field & vLinha).Value <> ""
  91.         vLinha = vLinha + 1
  92.     Loop 'Gets the last line that isn't blank
  93.    getFreeLine = vLinha 'returns vLinha
  94. End Function
  95.  
  96. Public Function IsNDA(linha As Long) As Boolean 'This function is how I managed to decide wheter it's just ND2 execution or ND2-NDA
  97.    IsNDA = False 'Start by saying it's not NDA
  98.    If linha Mod 2 = 1 Then 'If our line is odd then it's NDA because NDA is only executed every two weeks so if the first line of NDA is odd, all of them are gonna be.
  99.        IsNDA = True
  100.     End If
  101. End Function
  102.  
  103. Public Sub Save_As(fname As String)
  104.     Dim path As String
  105.  
  106.     path = Application.ActiveWorkbook.path 'Get our workbook path
  107.    Application.ActiveWorkbook.SaveAs Filename:=path & "\" & fname & ".xlsm", _
  108.         FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False 'Save it as new name (fname)
  109. End Sub
  110.  
  111. Public Function getFileName(linha As Long) As String 'This function is to get our new file name, it always has the format 'DATE_EXECUTION Exectuion Status'
  112.    Dim data As String 'first we get the date
  113.    data = Format(Range("A" & linha).Value, "dd.mm.yyyy") 'Format the last line as date (with dots)
  114.    Dim NDA As Boolean
  115.     NDA = IsNDA(linha)
  116.     If NDA Then
  117.         getFileName = data & "_ND2_NDA Execution Status" 'If it's NDA execution
  118.    Else
  119.         getFileName = data & "_ND2 Execution Status" 'If it's not
  120.    End If
  121. End Function
  122.  
  123. Public Sub Update_Graph(name As String, linha As Long)
  124.     If Range("A" & linha).Value = "" Then 'If it runs out of dates
  125.        Range("A" & linha).Value = Date 'It will write todays date in the next line
  126.    End If
  127.     Dim NDA As Boolean
  128.     NDA = IsNDA(linha)
  129.    
  130.     If NDA Then
  131.         Range("C2").Select 'If it's NDA we get the value of ND2-NDA
  132.    Else
  133.         Range("D2").Select 'Else we get the value from just ND2
  134.    End If
  135.     Selection.Copy 'We copy the value
  136.    Range("B" & linha).Select 'Go to line
  137.    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  138.     :=False, Transpose:=False 'Use paste values only (otherwise it will paste the formula)
  139.    
  140.     ActiveSheet.ChartObjects("Chart 7").Activate 'Activate our graph
  141.    ActiveChart.ChartGroups(1).FullCategoryCollection(linha - 1).IsFiltered = False 'Insert the element corresponding line - 1 in our graph
  142.     'This is to show the element we have just inserted in our graph
  143.  
  144. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement