我在这里看到了同样类型的问题,但无法解决......请帮忙。Hai 先生,我目前正在一个项目中将 2003 年格式中的所有 excel 宏转移到 2007.我面临的问题是对象_global的方法范围失败请为我提供一个解决方案。
请参阅代码...有范围("相位wt")没有更新....本工作簿中的所有命名范围都没有更新,到处都显示错误 1004:我正在使用现在的办公室 2007。
Private Sub FormatRowsAndSum(ByVal TotRows As Integer, StartRow As Integer)
Dim rngFormat As Range
'adds formatting
If TotRows > 2 Then
Set rngFormat = Range(Cells(StartRow + 2, 2), Cells(TotRows + StartRow + 1, 8))
'Range("B11:H11").Select
Range(Cells(StartRow + 1, 2), Cells(StartRow + 1, 8)).Select
Selection.Copy
rngFormat.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
'adds formula
Set rngFormat = Range(Cells(StartRow + 2, 11), Cells(TotRows + StartRow, 12))
'Range("K11:L11").Select
Range(Cells(StartRow + 1, 11), Cells(StartRow + 1, 12)).Select
Selection.Copy
rngFormat.Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
End If
ActiveSheet.PageSetup.FitToPagesWide = 1
Cells(TotRows + StartRow + 1, 2) = "TOTALS"
Cells(TotRows + StartRow + 1, 2).Font.Bold = True
Cells(TotRows + StartRow + 1, 2).RowHeight = 20
Range(Cells(TotRows + StartRow + 1, 2), Cells(TotRows + StartRow + 1, 8)).Select
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
End With
'writes total weight
With Cells(TotRows + StartRow + 1, 6)
.Value = "=SUM(K" & StartRow & ":K" & TotRows + StartRow & ")"
.Font.Bold = True
.NumberFormat = "#,##0.00"
End With
If Range("ReleaseTo") <> "STR" Then
Range("PhaseWt") = Cells(TotRows + StartRow + 1, 6) ' writes total wt in cover page
End If
'writes total cladding Area
With Cells(TotRows + StartRow + 1, 7)
.Value = "=SUM(L" & StartRow & ":L" & TotRows + StartRow & ")"
.Font.Bold = True
.NumberFormat = "#,##0.00"
End With
If Cells(TotRows + StartRow + 1, 7) > 0 Then
If Range("ReleaseTo") <> "STR" Then
Range("PhaseArea") = Cells(TotRows + StartRow + 1, 7) ' writes total Area in cover page
End If
End If
'writes total quantity
With Cells(TotRows + StartRow + 1, 3)
.Value = "=SUM(C" & StartRow & ":C" & TotRows + StartRow & ")"
.Font.Bold = True
.NumberFormat = "#,##0"
End With
Range("B4").Select
End Sub
感谢您的关注。
显示"对象_global失败"错误,因为 Excel 在工作簿中找不到给定的范围,因此请确保在文件的某处定义了范围"ReleaseTo"
、"PhaseWt"
、"PhaseArea"
。
编辑:尝试从这里调用您的代码:
Sub test()
With ActiveSheet
.Range("B13").Name = "jobno"
.Range("D13").Name = "bldgno"
.Range("F13").Name = "phaseno"
.Range("D15").Name = "project"
.Range("D16").Name = "Client"
.Range("J15").Name = "location"
.Range("K12").Name = "PhaseWt"
.Range("K13").Name = "PhaseArea"
End With
Call FormatRowsAndSum(1, 10)
End Sub