感谢你们和一些在我工作中提供帮助的开发人员,我几乎完成了老板给我的项目。我是一名 QA——不是开发人员——所以我的 VB 脚本专业知识是不存在的。
这是我的问题。我有下面的脚本,它将从销售预算电子表格中为每个地区的每个帐户中的每个客户获取主数据。下面的代码为每个帐户创建一个新工作表并按帐户名称保存。在该工作表中,它将为每个推销员创建一个新工作表。我遇到的两个问题是第一列(称为排名)按降序而不是升序过滤。例如 A:2 是 44,其中 A:2 应该是 1,A:3 应该是 2,A:4 应该是 3,A:5 应该是 4,等等。
这就引出了我的第二个问题。如何让每个电子表格中的第一行成为标题?我希望源工作表第 1 行中的所有内容在它创建的每个工作表中都是第 1 行。这是我想要的行:
秩
CUSTOMER_SEGMENT
ALIAS_NAME(分支)
SUPERVISOR_NAME
销售代表姓名
MAIN_CUSTOMER_NK
顾客
销售量
投资成本 GP
投资成本 GP%
销售增长
“GP点变更”
YTDLY_SALES
YTDLY_INVOICE_COST_GP
在发布之前我进行了搜索,我发现了两个可能对我有帮助的链接。但是,我是新手,无法理解将代码插入到下面现有脚本的位置。
如果代码不正确,请原谅我。我是 stackoverflow 格式的新手。\
' 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
以下是来自 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,11226%,-4.26,13%,2 %,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.3 ,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,29389,323587. ,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,Branch603,super3,SR3,24 ,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,Branch13,super3,SR3,4567832 ,193417.5,37101.67,19%,21%,1%,160318.29,29070.352%,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,-45,12%,-3% ,340909.56,54078.63 14,TOP 20,Branch3,super3,SR3,423212,123456789,193417.5,37101.67,19%,21%,1%,160318.29,29070.352%,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,-45,12%,-3% ,340909.56,54078.63 14,TOP 20,Branch3,super3,SR3,423212,123456789,193417.5,37101.67,19%,21%,1%,160318.29,29070.35