您好,我想将2种不同工作簿中的2张纸放入一本工作簿中,因此最后有一本工作簿中有3张纸(GrNegLos,VýsRek,NegLos)。当我尝试使用此行代码在工作表(VýsRek)中使用求和函数时:
With VýsRek
NetLossSAP = Application.WorksheetFunction.Sum(Range("DATA17"))
End With
它给了我运行时错误1004对象Global的方法范围失败。当我检查工作簿时,我可以在单击工作表(VýsRek)时看到工作表(NegLos)处于活动状态,然后它可以工作。但是我已经命名了range,并且指定了要在哪个工作表中使用sum函数。我不明白为什么会这样。
Sub Dáta()
Dim dátum As String
Dim TotalNLos, Loss10 As Integer
Dim GrossLoss, NetLossSAP As Double
Dim SAP As Workbook
Dim LORD_rep As Workbook
Dim dash As Workbook
Dim GrNegLos As Worksheet
Dim VýsRek As Worksheet
Dim NegLos As Worksheet
Dim rw As Long
Dim col As Long
Application.ScreenUpdating = False
Set dash = Workbooks.Open("")
Set GrNegLos = dash.Worksheets("Graf Neg. Loss")
Set SAP = Workbooks.Open("")
Set VýsRek = Worksheets("Výsledok rekonciliácie")
VýsRek.Move after:=GrNegLos
SAP.Close savechanges:=False
Set LORD_rep = Workbooks.Open("")
Set NegLos = Worksheets("Negative Losses")
NegLos.Move after:=GrNegLos
LORD_rep.Close savechanges:=False
Set NegLos = dash.Worksheets("Negative Losses")
Set VýsRek = dash.Worksheets("Výsledok rekonciliácie")
With VýsRek
NetLossSAP = Application.WorksheetFunction.Sum(Range("DATA17"))
End With
GrossLoss = NegLos.Range("CVNŠ")
TotalNLos = NegLos.Range("PNŠ")
Loss10 = NegLos.Range("PNŠvýš10k")
dátum = InputBox("Zadajte dátum")
rw = GrNegLos.Range("A2").End(xlDown).Offset(1, 0).Row
col = GrNegLos.Range("A2").End(xlDown).Offset(1, 0).Column
'Debug.Print rw
'Debug.Print col
GrNegLos.Cells(rw, col).Value = dátum
GrNegLos.Cells(rw, col + 1).Value = TotalNLos
GrNegLos.Cells(rw, col + 2).Value = Loss10
GrNegLos.Cells(rw, col + 3).Value = GrossLoss
GrNegLos.Cells(rw, col + 4).Value = NetLossSAP
Application.DisplayAlerts = False
On Error Resume Next
VýsRek.Delete
NegLos.Delete
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:2)
您需要完全限定命名的范围参考,以便查看正确的图纸。如果您未指定,则当前活动的工作表(ActiveSheet)将被隐式地用作父引用。如果那不是命名范围所在的正确工作表,那么您将得到观察到的错误。
始终完全限定范围,因为未引用容易发生错误,例如
Application.WorksheetFunction.Sum(VýsRek.Range("DATA17"))