我正在建立一个Excel电子表格,以跟踪气象站何时报告要在Tableau中使用以创建仪表板,以便我们可以跟踪网络的通信运行状况。
这是通过API获取完成的,该获取每10分钟提取一次数据,然后获取数据时间戳和Now()函数之间的差。如果超过10分钟,则在H列中将获得0或在其下方获得1。我的代码用于将这些值从H列移至与正确时间段关联的正确列。然后,在小时的顶部将即时消息“ HourScore”移动到正确的小时列,然后使用该列来计算“ DayScore”,这只是它整天准确传达的时间百分比。
当我只跟踪一个组织时,一切都很好。现在我正在跟踪两个,在为另一个实用程序添加了第二张表后,它停止运行。
我已经尝试了几种不同的方法来使对象引用起作用,但是没有大量的VBA经验,因此我无法真正查明确切的问题。
我也尝试将它们分成不同的工作簿,但是它们都在虚拟服务器上不断运行,并且我不断出错。该代码看起来与下面的代码非常相似,但是每个If函数都被拆分了。
Sub Minute_Save()
' Refresh Queries
ActiveWorkbook.RefreshAll
' Creates a variable array from the pge_minscore Column
Dim pge_ws As Worksheet
Dim pge_minscore As Range
Dim pge_hourscore As Range
Dim sge_ws As Worksheet
Dim sce_minscore As Range
Dim sce_hourscore As Range
Set pge_ws = Worksheets("PGE")
Set pge_minscore = Range([E2], [E:E].Find("*", [E1], , , xlByRows, xlPrevious))
Set sce_ws = Worksheets("SCE")
Set sce_minscore = Range([E2], [E:E].Find("*", [E1], , , xlByRows, xlPrevious))
' Copy Data Refresh into MinuteTable for MinuteScore
If Minute(Now()) < 10 Then
pge_ws.Range([H2], [H:H].Find("*", [H1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([H2], [H:H].Find("*", [H1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
ElseIf Minute(Now()) >= 10 And Minute(Now()) < 20 Then
pge_ws.Range([I2], [I:I].Find("*", [I1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([I2], [I:I].Find("*", [I1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
ElseIf Minute(Now()) >= 20 And Minute(Now()) < 30 Then
pge_ws.Range([J2], [J:J].Find("*", [J1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([J2], [J:J].Find("*", [J1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
ElseIf Minute(Now()) >= 30 And Minute(Now()) < 40 Then
pge_ws.Range([K2], [K:K].Find("*", [K1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([K2], [K:K].Find("*", [K1], , , xlByRows, xlPrevious)).Cells.Value = sce_minscore.Cells.Value
ElseIf Minute(Now()) >= 40 And Minute(Now()) < 50 Then
pge_ws.Range([L2], [L:L].Find("*", [L1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([L2], [L:L].Find("*", [L1], , , xlByRows, xlPrevious)).Resize(sce_minscore.Rows.Count, sce_minscore.Columns.Count).Cells.Value = sce_minscore.Cells.Value
ElseIf Minute(Now()) >= 50 Then
pge_ws.Range([M2], [M:M].Find("*", [AC1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
sce_ws.Range([M2], [M:M].Find("*", [M2], , , xlByRows, xlPrevious)).Resize(sce_minscore.Rows.Count, sce_minscore.Columns.Count).Cells.Value = sce_minscore.Cells.Value
Set sce_hourscore = sce_ws.Range([N2], [N:N].Find("*", [N1], , , xlByRows, xlPrevious))
Set pge_hourscore = pge_ws.Range([N2], [N:N].Find("*", [N1], , , xlByRows, xlPrevious))
' Copy MinuteScore to pge_hourscore
If Hour(Now()) = 0 Then
sce_ws.Range([O4], [O:O].Find("*", [O1], , , xlByRows, xlPrevious)).Resize(sce_hourscore.Rows.Count, sce_hourscore.Columns.Count).Cells.Value = sce_hourscore.Cells.Value
pge_ws.Range([O4], [O:O].Find("*", [O1], , , xlByRows, xlPrevious)).Resize(sce_hourscore.Rows.Count, sce_hourscore.Columns.Count).Cells.Value = sce_hourscore.Cells.Value
-some more code that is the same as above for more hours-
End If
End If
Application.OnTime Now + TimeValue("00:10:00"), "Minute_Save"
End Sub
我收到:
运行时错误“ 1004”: 对象“ _Worksheet”的方法“范围”失败
按照AJD的指示,我将以上内容更改为以下代码:
Set pge_ws = Worksheets("PGE")
Set pge_minscore = pge_ws.Range("H2:H500")
Set sce_ws = Worksheets("SCE")
Set sce_minscore = sce_ws.Range([E2], [E:E].Find("*", [E1], , , xlByRows, xlPrevious))
' Copy Data Refresh into MinuteTable for MinuteScore
If Minute(Now()) < 10 Then
'pge_ws.Range([H2], [H:H].Find("*", [H1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
'sce_ws.Range([H2], [H:H].Find("*", [H1], , , xlByRows, xlPrevious)).Resize(pge_minscore.Rows.Count, pge_minscore.Columns.Count).Cells.Value = pge_minscore.Cells.Value
Set startRange = pge_ws.Range("H2")
Set endRange = pge_ws.Range("H:H").Find("*", [H1], , , xlByRows, xlPrevious)
pge_ws.Range(startRange, endRange) = pge_minscore.Cells.Value
这有效;但我不确定为什么。为了我的目的,我应该能够适应ADJ给我的其余内容。我认为这是因为我不太真正理解值和范围之间的差异。显然,我知道范围是单元格的集合,但这不意味着范围只是值的集合吗?无论哪种方式都超出了本文的范围,因此,感谢那些帮助我们的人们,感谢您抽出宝贵的时间。
答案 0 :(得分:1)
始终将Option Explicit
添加到任何模块的顶部。 始终。
在这种情况下,它会提醒您注意未声明的变量sce_ws
,您可以通过更改Dim sge_ws As Worksheet
tp Dim sce_ws As Worksheet
对它进行响应。
此外,以下构造可能会出现问题-如果找不到结果怎么办?另外,因为您使用的是简写形式([H2]
),您确定参数中的值是Range而不是Value?也许您认为是这个范围,因为它们不完全合格,可以参考其他表格。
pge_ws.Range([H2], [H:H].Find("*", [H1], , , xlByRows, xlPrevious))
要检验该理论,请尝试:
set startRange = pge_ws.Range("H2")
Set endRange = pge_ws.Range("H:H").Find("*", [H1], , , xlByRows, xlPrevious)
pge_ws.Range(startRange, endRange).Resize ' etc
答案 1 :(得分:0)
您已经
Dim sge_ws As Worksheet
但是在这里,您尝试将此变量设置为工作表:
Set sce_ws = Worksheets("SCE")
为什么你会得到Run-time error '1004': Method 'Range' of object '_Worksheet' failed