Thanks to you guys and some developers at my work that have helped, I am almost done with a project my boss gave me. I'm a QA -- not a developer -- so my VB scripting expertise is non-existent.
Here's my problem. I have the script below that will take master data from a sales budgeting spreadsheet for every customer in every account in every region. The code below create a new worksheet for every account and save it by the account name. In that worksheet, it will create a new sheet for every salesman. The two issues I am running in to are that the first column (called Rank) is filtering by descending order instead of ascending order. for example A:2 is 44 where A:2 should be 1, A:3 should be 2, A:4 should be 3, A:5 should be 4, etc..
That leads to my second question. How do I get the first row in each spreadsheet to be a header? I want everything in Row 1 of the source worksheet to be row 1 in every worksheet it creates. Here are the rows I want:
Rank
CUSTOMER_SEGMENT
ALIAS_NAME (Branch)
SUPERVISOR_NAME
Salesrep Name
MAIN_CUSTOMER_NK
Customer
Sales
Inv Cost GP
Inv Cost GP%
Sales Growth
"GP Point Change"
YTDLY_SALES
YTDLY_INVOICE_COST_GP
I did a search before posting this, and I found two links that might help me. However, I am a novice and can't understand where I would insert the code in to my existing script below.
Setting Column Headers When Adding Columns To a Table Via Macro in Excel
Add headers to column data using a macro
Please forgive me if the code doesn't come over right. I'm new to stackoverflow's formatting.\
' get a named worksheet from specified workbook, creating it if required
Public Function GetSheet(ByVal Name As String, ByVal Book As Workbook, Optional ByVal Ignore As Boolean = False) As Worksheet
Dim Sheet As Worksheet
Dim Key As String
Dim Result As Worksheet: Set Result = Nothing
Key = UCase(Name)
' loop over all the worksheets
For Each Sheet In Book.Worksheets
' break out of the loop if the sheet is found
If UCase(Sheet.Name) = Key Then
Set Result = Sheet
Exit For
End If
Next Sheet
' if the sheet isn't found..
If Result Is Nothing Then
If Ignore = False Then
If Not GetSheet("Sheet1", Book, True) Is Nothing Then
' rename sheet1
Set Result = Book.Worksheets("Sheet1")
Result.Name = Name
End If
Else
' create a new sheet
Set Result = Book.Worksheets.Add
Result.Name = Name
End If
End If
Set GetSheet = Result
End Function
Sub Main()
Dim Source As Worksheet
Dim Location As Workbook
Dim Sales As Worksheet
Dim LocationKey As String
Dim SalesKey As String
Dim Index As Variant
Dim Map As Object: Set Map = CreateObject("Scripting.Dictionary")
Dim Row As Long
Set Source = ThisWorkbook.ActiveSheet
Row = 2 ' Skip header row
Do
' break out of the loop - assumes that the first empty row signifies the end
If Source.Cells(Row, 1).Value2 = "" Then
Exit Do
End If
LocationKey = Source.Cells(Row, 3).Value2
' look at the location, and find the workbook, creating it if required
If Map.Exists(LocationKey) Then
Set Location = Map(LocationKey)
Else
Set Location = Application.Workbooks.Add(xlWBATWorksheet)
Map.Add LocationKey, Location
End If
SalesKey = Source.Cells(Row, 5).Value2
' get the sheet for the salesperson
Set Sales = GetSheet(SalesKey, Location)
' insert a blank row at row 1
Sales.Rows(1).Insert xlShiftDown
' populate said row with the data from the source
Sales.Cells(1, 1).Value2 = Source.Cells(Row, 1)
Sales.Cells(1, 2).Value2 = Source.Cells(Row, 2)
Sales.Cells(1, 3).Value2 = Source.Cells(Row, 4)
Sales.Cells(1, 4).Value2 = Source.Cells(Row, 6)
Sales.Cells(1, 5).Value2 = Source.Cells(Row, 7)
Sales.Cells(1, 6).Value2 = Source.Cells(Row, 8)
Sales.Cells(1, 7).Value2 = Source.Cells(Row, 9)
Sales.Cells(1, 8).Value2 = Source.Cells(Row, 10)
Sales.Cells(1, 9).Value2 = Source.Cells(Row, 11)
Sales.Cells(1, 10).Value2 = Source.Cells(Row, 12)
'increment the loop
Row = Row + 1
Loop
' loop over the resulting workbooks and save them - using the location name as file name
For Each Index In Map.Keys
Set Location = Map(Index)
Location.SaveAs Filename:=Index
Next Index
End Sub
Here is the sample data from a CSV:
Rank,CUSTOMER_SEGMENT,ALIAS_NAME (Branch),SUPERVISOR_NAME,Salesrep Name,MAIN_CUSTOMER_NK,Customer,Sales,Inv Cost GP,Inv Cost GP%,Sales Growth,"GP Point Change",YTDLY_SALES,YTDLY_INVOICE_COST_GP 1,TOP 20,Branch1,super1,SR1,416469,3456,886394.26,211430.39,24%,-16%,1%,1056822.44,243333.25 2,TOP 20,Branch1,super1,SR1,223391,3456789,840048.49,112204.26,13%,26%,-4%,667457.3,115063.42 3,TOP 20,Branch1,super1,SR1,10299,9876,695652.09,88839.65,13%,7%,-2%,648249.35,95599.75 4,TOP 20,Branch1,super1,SR1,430884,23489,677324.34,91479.62,14%,190%,-2%,233935.32,36550.6 5,TOP 20,Branch2,super2,SR2,415886,89,430334.02,54701.73,13%,-22%,-2%,551546.33,80682.7 6,TOP 20,Branch2,super2,SR2,48793,234679,349611.36,61979.82,18%,-6%,2%,370575.07,59370.36 7,TOP 20,Branch2,super2,SR2,433979,2389,323587.09,49952.25,15%,-25%,3%,431745.94,53394.42 8,TOP 20,Branch2,super2,SR2,417290,3565850,304622.89,76255.75,25%,6%,5%,287953.73,57085.9 9,TOP 20,Branch2,super2,SR2,416986,9880,302111.92,45050.53,15%,46%,-1%,207067.31,32645.16 10,TOP 20,Branch2,super2,SR2,415811,8364859,252760.38,51374.19,20%,-7%,2%,271975.58,49567.85 11,TOP 20,Branch3,super3,SR3,428608,7369,238166.05,37761.17,16%,-24%,-1%,314515.42,54352.07 12,TOP 20,Branch3,super3,SR3,416363,980897987,237122.47,33682.5,14%,18%,-6%,201038.61,39941.88 13,TOP 20,Branch3,super3,SR3,428631,2345689,216378.99,25943.35,12%,-37%,-4%,340909.56,54078.63 14,TOP 20,Branch3,super3,SR3,423212,123456789,193417.5,37101.67,19%,21%,1%,160318.29,29070.35