我有一个代码,可以创建一些格式的工作表。我有一个问题,格式化不能正常工作(例如,它改变不同范围的颜色或不合并单元格)。
我想也许我做错了顺序或类似的事情所以我开始按F8从开始到结束。在这样做的同时,它完全像我想要的那样。
我的代码很长,因为里面有很多子,所以我会尝试编写它的工作方式并插入重要的部分。如果没有,我会把剩下的代码放在这里。
接下来,它调用3个子组件,创建3个工作表:ResourceSheet,DesignExecutionSheet和RisksSheet。 所有这些子代码都有代码来创建和格式化创建的工作表。 第一个子(ResourceSheet)正常工作,格式化就像我想要的那样。 问题出在子DesignExecutionSheet和RisksSheet上。当它经过F5时格式不好。
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
Sheets(sDesignSheetName).Activate
Cells.EntireColumn.Hidden = False
Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
Columns("C:C").ColumnWidth = 3
Columns("D:D").ColumnWidth = 25
Rows("8:8").RowHeight = 25
Rows("12:12").RowHeight = 25
Rows("17:17").RowHeight = 25
Range("C8:E8,C12:E12,C17:E17").Select
With Selection
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").Select
Selection.RowHeight = 20
Range("C9:C10,C13:C15,C18:C20").Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Range("C9:E10,C13:E15,C18:E20").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("A6").Select
Range("D9:D10,D13:D15,D18:D20").Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Selection.Font.Bold = True
Range("D9").Value = "ASSIGNED TO IT&V:"
Range("D10").Value = "COVERED BY IT&V:"
Range("D13").Value = "EXECUTED:"
Range("D14").Value = "PASSED:"
Range("D15").Value = "FAILED:"
Range("D18").Value = "OPEN:"
Range("D19").Value = "CLOSED:"
Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
与RisksSheet()有类似的问题所以我不把这个子代码放在这里。
调用此潜点后,代码中只有这个:
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
编辑:
我像LubošSuk建议的那样编辑了我的代码。现在问题不同了。通过按F8它再次正常工作,但按F5键出错“应用程序定义或对象定义错误”行
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
此外,当我在主要子行中设置断点
时,不会出现错误 Call DesignExecutionSheet
并按两次F5。
我还想到,当我没有多个范围时(例如只有“C8:E8”而不是“C8:E8,C12:E12,C17:E17”),它也有效。 但我内部有很多范围,如果我将所有范围分开,代码将会很长。
这是我的代码:
Private Sub DesignExecutionSheet()
Application.PrintCommunication = True
' Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'On Error Resume Next
bSheetFound = False
For Each wsSheet In ActiveWorkbook.Worksheets
'wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
Sheets.Add.Name = sResourcesProjectName & "_Design_Execution"
'Sheets(sDesignSheetName).Activate
With Sheets(sDesignSheetName)
.Cells.EntireColumn.Hidden = False
.Cells.EntireRow.Hidden = False
Captions sResourcesProjectName & " Design & Execution", RGB(235, 241, 222)
.Columns("C:C").ColumnWidth = 3
.Columns("D:D").ColumnWidth = 25
.Rows("8:8").RowHeight = 25
.Rows("12:12").RowHeight = 25
.Rows("17:17").RowHeight = 25
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17")
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
.Font.Bold = True
End With
With Sheets(sDesignSheetName).Range("C8:E8,C12:E12,C17:E17").Font
.Name = "Calibri"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.Color = RGB(118, 147, 60)
End With
Sheets(sDesignSheetName).Range("C8:E8").FormulaR1C1 = "STATUS OF REQUIREMENTS"
Sheets(sDesignSheetName).Range("C12:E12").FormulaR1C1 = "TEST EXECUTION"
Sheets(sDesignSheetName).Range("C17:E17").FormulaR1C1 = "VIR/SCR"
Sheets(sDesignSheetName).Range("9:9,10:10,13:13,14:14,15:15,18:18,19:19,20:20").RowHeight = 20
With Sheets(sDesignSheetName).Range("C9:C10,C13:C15,C18:C20").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = RGB(235, 241, 222)
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalDown).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlDiagonalUp).LineStyle = xlNone
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
With Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ThemeColor = 1
.TintAndShade = -0.349986266670736
.Weight = xlThin
End With
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideVertical).LineStyle = xlNone
Sheets(sDesignSheetName).Range("C9:E10,C13:E15,C18:E20").Borders(xlInsideHorizontal).LineStyle = xlNone
' .Range("A6").Select
With Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20")
.HorizontalAlignment = xlRight
.VerticalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
.Font.Color = RGB(89, 89, 89)
End With
Sheets(sDesignSheetName).Range("D9:D10,D13:D15,D18:D20").Font.Bold = True
Sheets(sDesignSheetName).Range("D9").Value = "ASSIGNED TO IT&V:"
Sheets(sDesignSheetName).Range("D10").Value = "COVERED BY IT&V:"
Sheets(sDesignSheetName).Range("D13").Value = "EXECUTED:"
Sheets(sDesignSheetName).Range("D14").Value = "PASSED:"
Sheets(sDesignSheetName).Range("D15").Value = "FAILED:"
Sheets(sDesignSheetName).Range("D18").Value = "OPEN:"
Sheets(sDesignSheetName).Range("D19").Value = "CLOSED:"
Sheets(sDesignSheetName).Range("D20").Value = "VERIFIED:"
Sheets(sDesignSheetName).Visible = xlSheetHidden
'Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
答案 0 :(得分:1)
所以你的主要问题就在这里,你激活工作表然后再使用它。
For Each wsSheet In ActiveWorkbook.Worksheets
wsSheet.Activate
sDesignSheetName = sResourcesProjectName & "_Design_Execution"
If wsSheet.Name = sDesignSheetName Then ' --- if that sheet already exists, then exit sub and go to next project
bSheetFound = True
Exit Sub
End If
Next wsSheet
我建议你在代码中使用更好的aproach。正确声明变量,并使用表格和单元格,如相对对象和 AVOID .activate和.select
简而言之,使用类似的东西。这种方法对您来说更加健壮且可读。
Sub doSomething()
Dim myCuteSheet As Worksheet
Set myCuteSheet = Sheets("pinkRidignSheet")
With myCuteSheet
.Range(.Cells(1,1),.Cells(5,5)) 'then do something with range
.Cells(15,20) 'do something with cell
.Columns ("F") 'do something with column
End With
End Sub