在excel里面編輯了如下代碼,可以順利運行
Sub de()
Dim i As Integer, a As Integer, b As Integer, c As Integer, rn As Range, d As Integer
a = Sheets("Jan").Application.CountA(Range("a:a"))
For i = 1 To 3
Application.Sheets.Add(, Sheets("Jan")).Name = i & "yue"
Sheets("Jan").Range("a1:b1").Copy Sheets(i & "yue").Cells(1, 1)
For Each rn In Sheets("Jan").Range("a1:a10")
If rn = i & "yue" Then
c = Sheets(i & "yue").UsedRange.Rows.Count
Sheets("Jan").Range("a" & rn.Row & ":b" & rn.Row).Copy Sheets(i & "yue").Cells(1, 1).Offset(c)
End If
Next
Next
End Sub
但是修改成下列代碼之后(基本只修改了紅色的兩行),就無法運行(運行到紅色的兩行excel都會自動提錯),但照理來說range的變數參考是可以用cell來代替的啊?為什么無法運行呢?
Sub de()
Dim i As Integer, m As Integer, n As Integer, c As Integer, rn As Range, d As Integer
m = Sheets("Jan").Application.CountA(Range("a:a"))
n = Sheets("Jan").Application.CountA(Range("1:1"))
For i = 1 To 3
Application.Sheets.Add(, Sheets("Jan")).Name = i & "yue"
Sheets("Jan").Range("a1:b1").Copy Sheets(i & "yue").Cells(1, 1)
For Each rn In Sheets("Jan").Range(cells(1,1), cells(m,n))
If rn = i & "yue" Then
c = Sheets(i & "yue").UsedRange.Rows.Count
Sheets("Jan").Range(Cells(c, 1), Cells(c, 2)).Copy Sheets(i & "yue").Cells(1, 1).Offset(c)
End If
Next
Next
End Sub
uj5u.com熱心網友回復:
m和n的值出了問題?uj5u.com熱心網友回復:
用debug.print驗證了m和n的值沒錯。。。。所以才比較糾結~~uj5u.com熱心網友回復:
注釋掉所有On Error Resume Next陳述句,在VBA IDE中運行,出錯后點擊除錯,游標會停在出錯的那條陳述句處,
或者
事先在懷疑可能有邏輯錯誤的陳述句處設定斷點,運行經過斷點時中斷,
此時可以在立即視窗中使用
?變數名
或
?函式名(函式引數)
或
程序名(引數)
輔助除錯。
uj5u.com熱心網友回復:
具體錯誤資訊是什么啊! 不說清楚……
For Each陳述句中,In關鍵字后面應該給出一個“集合物件”吧!
而Sheets("Jan").Range(cells(1,1), cells(m,n))只是表示“一個區域”的單一物件,而不是“集合”,用在那兒肯定不合法。
那個Copy ,改成這樣的試試:
Sheets("Jan").Range(Cells(c, 1), Cells(c, 2)).Copy Sheets(i & "yue").Cells(1, 1)
uj5u.com熱心網友回復:
For Each rn In Sheets("Jan").Range(cells(1,1), cells(m,n))改成
For Each rn In Sheets("Jan").Range(Sheets("Jan").cells(1,1), Sheets("Jan").cells(m,n))
uj5u.com熱心網友回復:
提問的人沒聲音了。uj5u.com熱心網友回復:
改成 Sheets("Jan").Range(cells(1,1).address, cells(m,n).address) 試試轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/48488.html
標籤:VBA
上一篇:vb連接webservice
