我認爲這是更多的問題與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中,這沒有什麼區別。感謝您的幫助,
卡梅倫
對不起顯然我在格式化堆棧溢出可怕的(我的第一篇文章,雖然我經常訪問的網站) 這是代碼的一部分: 子的東西( ) 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整數 行= 2 並且在rng5之後沒有>>> As – www3
當y ou正在使用代碼'If Not(IsEmpty(Cells(i,j).Value))''Cells(i,j)'是指什麼工作表?幾乎所有其他[Range.Cells屬性](https://msdn.microsoft.com/en-us/library/office/ff196273.aspx)引用都有父級工作表,但是這不是。也許它引用了錯誤的工作表,並且每個單元都被認爲是空的。 – Jeeped
如果找不到項目BinarySearch正在返回您正在用作行號的零。因此,您需要測試BinarySearch是否在您將其用作行號之前返回零。 – jdweng