下面是一个示例Excel报告,其中显示了每个办事处的销售数字。 A列列出了年,周号和销售人员。如果该周没有某个特定销售人员的销售,则该周没有列出该销售人员-但我需要这样。我想运行一个脚本以在未列出销售人员的任何地方插入一行作为占位符。
A | B | C
| |
2018 | Date1 | Date2
| |
Week 1 | |
| |
Garcia, J | 89 | 72
Lesh, P | 64 | 91
Total | 153 | 163
| |
Week 2 | |
| |
Anastasio, T | 40 | 44
Garcia, J | 62 | 58
Houser, M | 28 | 32
Rhoads, R | 437 | 658
Total | 657 | 792
| |
但是我需要列出所有销售人员。所以我需要显示的是:
A | B | C
| |
2018 | Date1 | Date2
| |
Week 1 | |
| |
Anastasio, T | |
Garcia, J | 89 | 72
Houser, M | |
Lesh, P | 64 | 91
Rhoads, R | |
Total | 153 | 163
| |
Week 2 | |
| |
Anastasio, T | 40 | 44
Garcia, J | 62 | 58
Houser, M | 28 | 32
Lesh, P | |
Rhoads, R | 437 | 658
Total | 657 | 792
我是VBA的新手,所以我找不到查找代码来检查A列和下面的单元格中的文本,并且A列中的文本是“ Anastasio,T”而下面的单元格中的文本是“ Garcia,J”,则无济于事,否则在“ Anastasio,T”下方插入行,并在“ Anastasio,T”下方的单元格中输入“ Garcia,J”,然后继续列表中的每个销售人员。任何建议都将不胜感激。
答案 0 :(得分:0)
一种解决方法是,在列出的每个销售人员的基础上创建自己的模板报告,然后将VBA脚本编写为:
您的第一步是创建模板,然后使用Excel中的宏录制功能,或对“ For Each”和“ If”循环进行一些研究。这应该可以帮助您入门:
Sub salesChecker()
Dim templatePerson As Range
Dim templateSalespeople As Range ' Set up a range covering all names on the template
Dim reportSalespeople As Range ' Set up a range covering all names on the report
Dim reportPerson As Range
' Set where these ranges apply
Set templateSalespeople = ThisWorkbook.Worksheets(1).Range("A2:A20") 'Example only: you'll need to set ranges
Set reportSalespeople = ThisWorkbook.Worksheets(2).Range("A2:A20") 'Example only: you'll need to set ranges
' Loop though each person on the template
For Each templatePerson In templateSalespeople
' Check name against each person on the report
For Each reportPerson In reportSalespeople
' If the names match then...
If reportPerson.Value = templatePerson.Value Then
' Copy the value in the cell next to the person's name and paste into template
reportPerson.Offset(0, 1).Copy Destination:=templatePerson.Offset(0, 1)
End If
Next
End Sub