我的想法是我在工作簿表中有一个名单列表。
添加新队友时,此列表可能会不时更新。然后,我在单独的表格上有摘要表。
我需要的是,只要列表更新,此列表就会填充摘要表的A列,如果名称在摘要表的A列的行内,则还要在其他列中应用公式。
任何帮助将不胜感激!
现在,我手动为A列的每一行添加名称,然后在其他工作表中搜索名称等等。我最终试图这样做,而不是通过代码手动添加名称,某人不熟悉VBA的人可以将名称添加到列表下的不同工作表中,然后使用该工具生成列表,然后让代码搜索这些名称并应用正确的公式。
任何帮助都会很棒!这是我现在的代码,但我希望我可以在开头包含我提到的内容(带有一个从那里拉出并可以更新的名称列表的表单)。此代码有效,但如果添加了新的队友,则需要有人更新代码...
Sub Summary()
'Summary table Team
Application.ScreenUpdating = False
Dim sheet As Worksheet
ActiveWorkbook.Sheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.name = "Summary"
'Making the Table
ActiveSheet.Range(Cells(1, "A"), Cells(13, "G")).Borders.LineStyle = xlContinuous
Cells(1, "A").Value = "Header 1"
Cells(1, "B").Value = "Header 2"
Cells(1, "C").Value = "header 3"
Cells(1, "D").Value = "header 4"
Cells(1, "E").Value = "header 5"
Cells(1, "F").Value = "header 6"
Cells(1, "G").Value = "header 7"
Cells(2, "A").Value = "John"
Cells(3, "A").Value = "Bob"
Cells(4, "A").Value = "Laura"
Cells(5, "A").Value = "Linda"
Cells(6, "A").Value = "Lucy"
Cells(7, "A").Value = "Alice"
Cells(8, "A").Value = "Margret"
Cells(9, "A").Value = "Matt"
Cells(10, "A").Value = "Steve"
Cells(11, "A").Value = "Tim"
Cells(12, "A").Value = "Luke"
Cells(13, "A").Value = "Tara"
Range("A1:I1").EntireColumn.AutoFit
'Adding the Formulas for the Table
Worksheets("Summary").Activate
ActiveSheet.Range("B2:B13").Formula = "=COUNTIFS('Sheet1'!G:G,Summary!A2)"
ActiveSheet.Range("C2:C13").Formula = "=COUNTIFS('Sheet2'!G:G,Summary!A2)"
ActiveSheet.Range("D2:D13").Formula = "=COUNTIFS('Sheet3'!G:G,Summary!A2)"
ActiveSheet.Range("E2:E13").Formula = "=COUNTIFS('Sheet4'!G:G,Summary!A2)"
ActiveSheet.Range("F2:F13").Formula = "=COUNTIFS('Sheet5'!G:G,Summary!A2)"
ActiveSheet.Range("G2:G13").Formula = "=COUNTIFS('Sheet6'!G:G,Summary!A2)"
'Adding conditional formatting
Range("B2:G13").Select
Selection.formatconditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=0"
Selection.formatconditions(Selection.formatconditions.Count).SetFirstPriority
With Selection.formatconditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.formatconditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.formatconditions(1).StopIfTrue = False
Selection.formatconditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=0"
Selection.formatconditions(Selection.formatconditions.Count).SetFirstPriority
With Selection.formatconditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.formatconditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.formatconditions(1).StopIfTrue = False
Application.ScreenUpdating = True
End Sub
对于vba也相当新,所以我知道这段代码很难看,但它的工作原理大声笑!只是寻求有关如何解决它的帮助,以便更少手动更新名称和更动态。
答案 0 :(得分:0)
我认识到你使用VBA解决这个问题的努力,但我坚信你过于复杂。与使用自定义VBA代码相比,Excel的核心功能更适合以更快,更有效的方式解决您的问题。
要解决您的问题,请创建一个动态命名范围来保存您的名称列表。这样,无论何时需要添加名称,它都将自动添加到整个工作簿中。在功能区中,单击“公式”,“名称管理器”,“添加”(我称之为“名称”,并假设您的名称列表位于Sheet1上的A列中:
=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),1)
要在其他工作表上使用该名称,只需使用=names
。
关于另一张纸上的公式,我会使用占位符。例如,您在摘要工作表的B列中使用了=COUNTIFS('Sheet1'!G:G,Summary!A2)
。使用您的公式填充B列,但在前面添加IF
语句。所以它会变成=IF($A2=0,"",COUNTIFS('Sheet1'!G:G,Summary!A2))
。这将使没有名称的行为空。
按照正常情况添加格式,无需VBA。
您现在可以通过添加名称进行测试,所有内容都会更新。如果您需要任何其他帮助,请与我们联系。