Sub Macro1()
'
' Macro1 Macro
'
Dim dateToUse As String
Dim dateOverride As String
Dim worksheetName As String
Select Case Weekday(Now)
' Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7
Case 1
' Get Friday's date(subtract 2 days from today)
dateToUse = Format(DateAdd("d", -2, CDate(Now)), "mm-dd-yyyy")
Case 2
' Get Friday's date(subtract 3 days from today)
dateToUse = Format(DateAdd("d", -3, CDate(Now)), "mm-dd-yyyy")
Case 3, 4, 5, 6, 7
' Get Previous days date
dateToUse = Format(DateAdd("d", -1, CDate(Now)), "mm-dd-yyyy")
End Select
dateOverride = ""
If dateOverride = "" Then
worksheetName = dateToUse
Else
worksheetName = dateOverride
End If '
'
Dim numOfRows As Integer
Dim numOfCols As Integer
' Get the number of rows in the spreadsheet
numOfRows = Worksheets(1).UsedRange.Rows.Count
numOfCols = Worksheets(1).UsedRange.Columns.Count
' Loop through each row and convert UPCs to 12 digit UPCs
Dim rowIndex As Integer
Dim colIndex As Integer
Dim currentCell As String
' Loop through each row
For rowIndex = 2 To numOfRows
' Loop through each column
For colIndex = 1 To numOfCols
' Reset the currentUPC variable
currentCell = ""
' If the header for the current column is CreditDebitNum, InvoiceNum, PONum then convert the cell to text
' If the header for the current column starts with UPC and the current cell contains a value check to see if it needs to be fixed
If (Cells(1, colIndex).Value = "CreditDebitNum" Or Cells(1, colIndex).Value = "InvoiceNum" Or Cells(1, colIndex).Value = "PONum") And Trim(Cells(rowIndex, colIndex).Value) <> "" Then
' Copy the current cell content to the clipboard
currentCell = Trim(Cells(rowIndex, colIndex).Value)
' Change the format of the current cell to Text
Cells(rowIndex, colIndex).NumberFormat = "@"
' Update the current cell with the fixed UPC
Cells(rowIndex, colIndex).Value = currentCell
ElseIf Left(Cells(1, colIndex).Value, 3) = "UPC" And Trim(Cells(rowIndex, colIndex).Value) <> "" Then
' If the length of the current cell is greater than 10 then this should be a 12 digit UPC, else leave it alone
If Len(Trim(Cells(rowIndex, colIndex).Value)) > 10 Then
' Update the current cell to a 12 digit UPC and save it to a temporary variable
currentCell = Right("000000000000" & Trim(Cells(rowIndex, colIndex).Value), 12)
' Change the format of the current cell to Text
Cells(rowIndex, colIndex).NumberFormat = "@"
' Update the current cell with the fixed UPC
Cells(rowIndex, colIndex).Value = currentCell
End If
End If
Next colIndex
Next rowIndex
'
Cells.Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=RIGHT($C1,1)=""B"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.Color = 65280
.TintAndShade = 0
End With
Cells.Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=LEFT($C1,1)=""R"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.Color = 13882323
.TintAndShade = 0
End With
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=LEFT($C1,1)=""V"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.Color = 9419919
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Cells.Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=LEFT($C1,1)=""T"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.Color = 13408767
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.CutCopyMode = False
Application.CutCopyMode = False
Selection.FormatConditions(1).StopIfTrue = False
Application.CutCopyMode = False
Application.CutCopyMode = False
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=COUNTIF(1:1,""*9m*"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Cells.Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=RIGHT($C1,1)=""c"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.Color = 16776960
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Cells.Select
Range("A352").Activate
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=AND(LEFT($C1,1)=""R"",RIGHT($C1,1)=""B"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399945066682943
End With
' Autofit all the columns in the worksheet
Range(Columns(1), Columns(numOfCols)).AutoFit
Sheets.add After:=ActiveSheet
[B]Sheets("All Vendors 07-09-2019").Sele[/B]ct
Cells.Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Paste
Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Range("A3").Select
Columns("E:F").Select
Range("F1").Activate
Selection.NumberFormat = "0.00"
Range("F4").Select
Columns("g:h").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Windows("PERSONAL.XLSB").Activate
Range("H2:I2").Select
Selection.Copy
[B]Windows("All Vendors 07-09-2019").Activate[/B]
Range("G2").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("G2:H4000")
Range("G2:H4000").Select
Columns("G:G").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("H:H").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Cells.Select
ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("sheet1").Sort.SortFields.add Key:=Range("c2:c4000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("sheet1").Sort
.SetRange Range("A1:AQ4000")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub