我认为这更多是excel选项和问题的问题,但我不确定。我的代码的描述:它需要在工作表的前8-9个选项卡中写入时间序列数据(每个选项卡是一个不同的指示符),并将其显示在一行中,以便不像时间序列格式那样写入数据(每个指标1-1-2000到1-1-2015)所有指标(有三个滞后以及GGR标签的7个前向滞后)都写入一行中的给定日期这是我的代码:
Sub stuff()
Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range, rng6 As Range, rng7 As Range, rng8 As Range
Dim datenum As Long, Row As Integer, sorteddate As Variant, loc As Integer
Row = 2
For j = 2 To 53
For i = 8 To 275
If Not (IsEmpty(Cells(i, j).Value)) Then
Sheets("Sheet1").Cells(Row, 8) = Sheets("GGR").Cells(i - 1, j).Value
Sheets("Sheet1").Cells(Row, 9) = Sheets("GGR").Cells(i - 2, j).Value
Sheets("Sheet1").Cells(Row, 10) = Sheets("GGR").Cells(i - 3, j).Value
Sheets("Sheet1").Cells(Row, 29) = Sheets("GGR").Cells(i, j).Value
Sheets("Sheet1").Cells(Row, 30) = Sheets("GGR").Cells(i + 1, j).Value
Sheets("Sheet1").Cells(Row, 31) = Sheets("GGR").Cells(i + 2, j).Value
Sheets("Sheet1").Cells(Row, 32) = Sheets("GGR").Cells(i + 3, j).Value
Sheets("Sheet1").Cells(Row, 33) = Sheets("GGR").Cells(i + 4, j).Value
Sheets("Sheet1").Cells(Row, 34) = Sheets("GGR").Cells(i + 5, j).Value
Sheets("Sheet1").Cells(Row, 35) = Sheets("GGR").Cells(i + 6, j).Value
Sheets("Sheet1").Cells(Row, 36) = Sheets("GGR").Cells(i + 7, j).Value
datenum = Sheets("GGR").Cells(i, 1).Value
Sheets("Sheet1").Cells(Row, 1).Value = datenum
Set rng1 = Sheets("CPIC").Range("A1:A408")
sorteddate = rng1.Value
loc = BinarySearch(rng1, datenum)
Sheets("Sheet1").Cells(Row, 2) = Sheets("CPIC").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 3) = Sheets("CPIC").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 4) = Sheets("CPIC").Cells(loc - 2, j).Value
Set rng2 = Sheets("GBGT").Range("A1:A71")
sorteddate = rng2.Value
loc = BinarySearch(rng2, datenum)
Sheets("Sheet1").Cells(Row, 5) = Sheets("GBGT").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 6) = Sheets("GBGT").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 7) = Sheets("GBGT").Cells(loc - 2, j).Value
Set rng3 = Sheets("GFCF").Range("A5:A264")
sorteddate = rng3.Value
loc = BinarySearch(rng3, datenum)
Sheets("Sheet1").Cells(Row, 11) = Sheets("GFCF").Cells(loc, j).Value
Sheets("testsheet").Cells(1, 1).Value = loc
Sheets("Sheet1").Cells(Row, 12).Value = Sheets("GFCF").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 13).Value = Sheets("GFCF").Cells(loc - 2, j).Value
Set rng4 = Sheets("M1").Range("A1:A700")
sorteddate = rng4.Value
loc = BinarySearch(rng4, datenum)
Sheets("Sheet1").Cells(Row, 14) = Sheets("M1").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 15) = Sheets("M1").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 16) = Sheets("M1").Cells(loc - 2, j).Value
Set rng5 = Sheets("M2").Range("A1:A676")
sorteddate = rng5.Value
loc = BinarySearch(rng5, datenum)
Sheets("Sheet1").Cells(Row, 17) = Sheets("M2").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 18) = Sheets("M2").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 19) = Sheets("M2").Cells(loc - 2, j).Value
Set rng6 = Sheets("CSP").Range("A1:A264")
sorteddate = rng6.Value
loc = BinarySearch(rng6, datenum)
Sheets("Sheet1").Cells(Row, 20) = Sheets("CSP").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 21) = Sheets("CSP").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 22) = Sheets("CSP").Cells(loc - 2, j).Value
Set rng7 = Sheets("UNR").Range("A1:A272")
sorteddate = rng7.Value
loc = BinarySearch(rng7, datenum)
Sheets("Sheet1").Cells(Row, 23) = Sheets("UNR").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 24) = Sheets("UNR").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 25) = Sheets("UNR").Cells(loc - 2, j).Value
Set rng8 = Sheets("MKT").Range("A1:A223")
sorteddate = rng8.Value
loc = BinarySearch(rng8, datenum)
Sheets("Sheet1").Cells(Row, 26) = Sheets("MKT").Cells(loc, j).Value
Sheets("Sheet1").Cells(Row, 27) = Sheets("MKT").Cells(loc - 1, j).Value
Sheets("Sheet1").Cells(Row, 28) = Sheets("MKT").Cells(loc - 2, j).Value
Row = Row + 1
End If
Next i
Next j
End Sub
Function BinarySearch(rng As Range, searchValue As Long) As Integer
'dimension these as long to avoid possible integer
'overflow errors for large lists
Dim curIndex As Long
Dim firstIndex As Integer
Dim lastIndex As Integer
Dim nextMiddle As Long
Dim strValue As Long
Dim MyCell As Variant
Dim i As Integer
i = 0
For Each MyCell In rng
If MyCell < searchValue Then
i = i + 1
End If
Next MyCell
BinarySearch = i
End Function
我理解我的代码不是最有效的,我编写得很快,而且不是VBA中最了解的。我也尝试过二分搜索而不是线性搜索,但我一直在犯错,所以我只是使用线性搜索,因为速度不是问题。无论如何,当我尝试运行我的代码时,偶尔(即每20次尝试)它运行并发出错误。错误不是我所关心的。但是,当我经常运行它时它不会运行。我花了大约30分钟让调试器向我显示运行时错误。当我按下VBA上的运行按钮时,通常它会退出。我尝试了一步,它突出显示第一行(sub stuff())然后代码退出而不通过其余的代码。我已经尝试过在excel中允许使用宏。我已经运行其他代码简单的1行打印语句,这是有效的。我也尝试将其复制并粘贴到另一个excel中,但没有任何区别。
答案 0 :(得分:0)
我同意Jeeped的观点:
If Not (IsEmpty(Cells(i, j).Value)) Then
您应该引用Cell引用的工作表。否则Excel默认为活动工作表,因此如果您在工作表之间切换,则参考文件无法按预期工作。
答案 1 :(得分:0)
是的我弄清楚了或至少找到了解决方法。出于某种原因,当活动工作表为“Sheet1”时,我的代码将无法运行。当我制作活动表“MKT”时,它出于某种原因起作用。现在仍然存在线性搜索的错误,如有人提到的那样返回零,虽然它不应该因为它搜索所有的值都是日期而我用零填充所有初始字符串,但这是我可以处理的错误。谢谢大家的帮助,
卡梅伦