So far it is working great, but I was wondering if I could optimize a little since I have nearly 30 variables in code of only 100 lines.
Since I'm not a coder, I code based on how I would be doing it manually:
I have a main file that will, when complete, house a summary of all the information in my data file. I assign variables for the Main file and the data file and open the data file with the getopen code.
I have 4 variables for the specific columns I will be referencing (will probably need to add more before the end of this code). I manually assigned a letter because the columns will rarely if ever change. I then put a formula in to assign a variable to the column #.
I have variables for the last column and last row.
I insert 3 lines which will be populated with data and assign variables for the column letter, and # and populate a header for those columns.
I set variables for each range of those columns.
I sort the data based on 2 columns.
I loop through the data and populate the columns I inserted earlier.
Obviously my methodology to create this, translating how I would do it manually, isn't ideal, but are there shortcuts for any of this? Everything works fine right now, it just seems like I'm taking the long way. Do I really need to create 3 different variables for the column letter, column # and range of column?
Dim Question1 As String '/ is this the current Tab to work on?
Dim TT As String '/ Column Letter for Total Time in seconds
Dim TTN As Long '/ Column # for Total time in seconds
Dim TTR As Range '/ Column as Range Total time
Dim CN As String '/ Column Letter for Call Number
Dim CNN As Long '/ Column # for Call Number
Dim CNR As Range '/ Column as Range Call Number
Dim TY As String '/ Column letter for Type
Dim TYN As Long '/ Column # for Type
Dim TYR As Range '/ Column as range Type
Dim NC As String '/ Column letter for Number called
Dim NCN As Long '/ Column # for Number called
Dim NCR As Range '/ Column as range for Number called
Dim ER As Range '/ Range of data
Dim LRow As Long '/ Finding last Row of data
Dim LCol As Long '/ Finding last Column of data
Dim Uni As String '/ Unique Column
Dim UniN As Long '/ Column #
Dim UniR As Range '/ Column as range
Dim Dur As String '/ Duration Column
Dim DurN As Long '/ Column #
Dim DurR As Range '/ Column as range
Dim LastTFN As String '/ Last 4 digits Column
Dim LastTFNN As Long '/ Column #
Dim LastTFNR As Range '/ Column as range
Dim TyLo As Long '/ Type Loop
Set InFi = ActiveWorkbook.ActiveSheet
Question1 = MsgBox("Are you allocating data for the week of " & Replace(ActiveSheet.Name, ".", "/") & "?", vbYesNo, "")
If Question1 = vbYes Then
Set InFi = ActiveWorkbook.ActiveSheet
ElseIf Question1 = vbNo Then
MsgBox ("Goto the current week and re-run")
Exit Sub
End If
FileName = Application.GetOpenFilename
If FileName <> False Then
Workbooks.Open FileName:=FileName
End If
Set CaFi = ActiveWorkbook.Sheets("Calls")
CaFi.Activate
Cells(1, 1).Select
TT = "H"
TTN = Asc(TT) - 64 'Numerical value of TT
CN = "C"
CNN = Asc(CN) - 64 'Numerical value of CN
TY = "M"
TYN = Asc(TY) - 64 'Numerical value of TY
NC = "I"
NCN = Asc(NC) - 64 'Numerical value of NC
LRow = Cells(Rows.count, 1).End(xlUp).Row
LCol = Cells(1, Columns.count).End(xlToLeft).Column
Columns(TYN + 1).EntireColumn.Insert
Uni = "N"
UniN = Asc(Uni) - 64
Cells(1, UniN).Value = "Unique or Duplicate"
Columns(UniN + 1).EntireColumn.Insert
Dur = "O"
DurN = Asc(Dur) - 64
Cells(1, DurN).Value = "Call Durations"
Columns(DurN + 1).EntireColumn.Insert
LastTFN = "P"
LastTFNN = Asc(LastTFN) - 64
Cells(1, LastTFNN).Value = "Last 4 TFN"
With CaFi
Set TTR = .Range(.Cells(1, TTN), .Cells(1, TTN).End(xlDown))
Set CNR = .Range(.Cells(1, CNN), .Cells(1, CNN).End(xlDown))
Set TYR = .Range(.Cells(1, TYN), .Cells(1, TYN).End(xlDown))
Set NCR = .Range(.Cells(1, NCN), .Cells(1, NCN).End(xlDown))
Set ER = .Range(.Cells(1, 1), .Cells(LRow, LCol))
Set UniR = .Range(.Cells(1, UniN), .Cells(1, UniN).End(xlDown))
Set DurR = .Range(.Cells(1, DurN), .Cells(1, DurN).End(xlDown))
Set LastTFNR = .Range(.Cells(1, LastTFNN), .Cells(1, LastTFNN).End(xlDown))
End With
With CaFi.Sort
.SortFields.Clear
.SortFields.Add key:=CNR, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
.SortFields.Add key:=TTR, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortTextAsNumbers
.SetRange ER
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For TyLo = 2 To LRow
'/ Unique vs Duplicate
If Cells(TyLo, CNN).Value <> Cells(TyLo - 1, CNN).Value Then
Cells(TyLo, TYN + 1).Value = "Unique"
Else
Cells(TyLo, TYN + 1).Value = "Duplicate"
End If
'/ Total Time
If Cells(TyLo, TTN).Value > 1 And Cells(TyLo, TTN).Value < 30 Then
Cells(TyLo, TYN + 2).Value = ">1"
ElseIf Cells(TyLo, TTN).Value > 30 And Cells(TyLo, TTN).Value < 60 Then
Cells(TyLo, TYN + 2).Value = ">30"
ElseIf Cells(TyLo, TTN).Value > 60 And Cells(TyLo, TTN).Value < 600 Then
Cells(TyLo, TYN + 2).Value = ">60"
ElseIf Cells(TyLo, TTN).Value > 600 Then
Cells(TyLo, TYN + 2).Value = ">600"
Else: Cells(TyLo, TYN + 2).Value = "Missed Call"
End If
Cells(TyLo, TYN + 3).Value = Right(Cells(TyLo, NCN).Value, 4)
Next
End Sub