Advertisement
cs007-123

Excel Macro - Reformat Data to New Sheet

Apr 10th, 2023 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub MapData()
  2.     'Declare variables
  3.    Dim srcSheet As Worksheet
  4.     Dim destSheet As Worksheet
  5.     Dim srcLastRow As Long
  6.     Dim destLastRow As Long
  7.     Dim i As Long
  8.     Dim j As Long
  9.     Dim k As Long
  10.     Dim arrSourceColumns As Variant
  11.     Dim arrDestColumns As Variant
  12.     Dim arrUnknownColumns As Variant
  13.     Dim arrDegreeTypes() As Variant
  14.     Dim strPOINames As String
  15.     Dim strPOIDescriptions As String
  16.     Dim strPOIType As String
  17.     Dim strPOIName As String
  18.     Dim strPOIDescription As String
  19.     Dim strDegreeType As Variant
  20.    
  21.    
  22.     'Set variables
  23.    Set srcSheet = Worksheets("Catalog")
  24.     Set destSheet = Worksheets("vbiz-Catalog")
  25.     srcLastRow = srcSheet.Cells(srcSheet.Rows.Count, 1).End(xlUp).Row
  26.     destLastRow = destSheet.Cells(destSheet.Rows.Count, 1).End(xlUp).Row + 1
  27.     arrSourceColumns = Split("DivisionID, Division Description, DisciplineID, Discipline Description, Division Type, POI ID, POI ID SF, POI Name (Marketing), UNKNOWN, Course Length, Has Pre Reqs, Total Courses Required, UNKNOWN, List Price, UNKNOWN, UNKNOWN", ", ")
  28.     arrDestColumns = Split("DivisionID, DivisionDescription, DisciplineID, DisciplineDescription, POI_Type, POIID, POI_Code, POIName, Degree_Type, POILength(weeks), Individual Course (Y/N), TotalCoursesRequired, TotalCoursesAvailable, Tuition, MaterialFee, POI_Guide_URL, POI_Detail_Description", ", ")
  29.     arrUnknownColumns = Split("UNKNOWN, UNKNOWN, UNKNOWN", ", ")
  30.     arrDegreeTypes = Split("MS, MBA, MA, Master, Masters, Master’s", ", ")
  31.    
  32.     'Copy data
  33.    For i = 2 To srcLastRow
  34.         'Copy columns with known names
  35.        For j = 0 To UBound(arrSourceColumns)
  36.             destSheet.Cells(destLastRow, j + 1).Value = srcSheet.Cells(i, WorksheetFunction.Match(arrSourceColumns(j), srcSheet.Rows(1), 0)).Value
  37.         Next j
  38.        
  39.         'Copy columns with unknown names
  40.        For k = 0 To UBound(arrUnknownColumns)
  41.             destSheet.Cells(destLastRow, UBound(arrSourceColumns) + 1 + k).Value = srcSheet.Cells(i, WorksheetFunction.Match(arrUnknownColumns(k), srcSheet.Rows(1), 0)).Value
  42.         Next k
  43.        
  44.         'Check for POI Type
  45.        strPOIType = srcSheet.Cells(i, WorksheetFunction.Match("POI Type", srcSheet.Rows(1), 0)).Value
  46.        
  47.         'Set POI Type in destination sheet
  48.        If strPOIType Like "*Degree*" Then
  49.             destSheet.Cells(destLastRow, WorksheetFunction.Match("POI_Type", destSheet.Rows(1), 0)).Value = "Degree"
  50.            
  51.             'Check POI Names and Descriptions for degree type keywords
  52.            strPOINames = srcSheet.Cells(i, WorksheetFunction.Match("POI Name (Marketing)", srcSheet.Rows(1), 0)).Value
  53.             strPOIDescriptions = srcSheet.Cells(i, WorksheetFunction.Match("POI Description", srcSheet.Rows(1), 0)).Value
  54.            
  55.             'Set degree type in destination sheet
  56. For Each strDegreeType In arrDegreeTypes
  57.     If InStr(strPOIType, "Degree") > 0 Then
  58.         For Each strDegreeName In arrDegreeNames
  59.             If InStr(strPOIName, strDegreeName) > 0 Or InStr(strPOIDesc, strDegreeName) > 0 Then
  60.                 wsDest.Cells(destRow, 9).Value = strDegreeType
  61.                 Exit For
  62.             End If
  63.         Next strDegreeName
  64.     End If
  65. Next strDegreeType
  66.  
  67.            
  68.             'Set value for Degree_Type column based on POI_Type and POI Name/Description
  69.            If Not IsEmpty(POIType) And Not IsEmpty(POIName) Then
  70.                 If InStr(1, POIType, "Degree", vbTextCompare) > 0 And _
  71.                 (InStr(1, POIName, "MS", vbTextCompare) > 0 Or _
  72.                 InStr(1, POIDesc, "MS", vbTextCompare) > 0 Or _
  73.                 InStr(1, POIName, "MBA", vbTextCompare) > 0 Or _
  74.                 InStr(1, POIDesc, "MBA", vbTextCompare) > 0 Or _
  75.                 InStr(1, POIName, "MA ", vbTextCompare) > 0 Or _
  76.                 InStr(1, POIDesc, "MA ", vbTextCompare) > 0 Or _
  77.                 InStr(1, POIName, "Master's", vbTextCompare) > 0 Or _
  78.                 InStr(1, POIDesc, "Master's", vbTextCompare) > 0 Or _
  79.                 InStr(1, POIName, "Masters", vbTextCompare) > 0 Or _
  80.                 InStr(1, POIDesc, "Masters", vbTextCompare) > 0) Then
  81.                     wsDest.Cells(destRow, 9).Value = "Master’s"
  82.                 End If
  83.             End If
  84.            
  85.             'Increment the destination row for the next record
  86.            destRow = destRow + 1
  87.         Next i
  88.     End If
  89.     Next strDivisionType
  90.     MsgBox "Data has been successfully mapped.", vbInformation
  91.    
  92. End Sub
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement