2015-02-09 144 views
0

當我嘗試運行這個函數來得到字符串範圍我得到的錯誤信息:"Method Range of object _Worksheet failed. 我運行Excel 2013和MSVBA 7.1對象_Worksheet Excel的VBA方法範圍失敗

這是我的代碼:

Public Function dataRange() As Range 
    Dim activeMonth As Date, sht As Worksheet, s As String, r As Range 
    activeMonth = Format(Now, "MMM-YYYY") 
    Set sht = Sheets(Format(Now, "MMM-YYYY")) 
    s = "" 
    s = "$b$6:$bm$6,$B$9:$Bm$9,$B$12:$Bm$12,$b$15:$bm$15,$B$18:$Bm$18,$b$21:$bm$21,$b$24:$bm$24,$b$27:$bm$27,$b$30:$bm$30,$b$33:$bm$33," 
    s = s & "$B$36:$Bm$36,$B$39:$Bm$39,$B$42:$Bm$42,$B$45:$Bm$45,$B$48:$Bm$48,$B$51:$Bm$51,$B$54:$Bm$54,$B$57:$Bm$57,$B$60:$Bm$60,$B$63:$Bm$63,$B$66:$Bm$66,$B$69:$Bm$69" 
    s = s & "$B$72:$BO$72,$B$75:$BO$75,$B$78:$BO$78,$B$81:$BO$81,$B$84:$BO$84,$B$87:$BO$87,$B$90:$BO$90,$B$93:$BO$93,$B$96:$BO$96" 
    Set r = sht.Range(s) 'THIS IS WHERE I GET THE ERROR.. 

'SO I TRIED THE CODE BELOW BUT IT STILL WON'T PRINT THE WHOLE STRING IN THE DEBUG AREA!! 

    Dim r1, r2, r3 As Range 
    s = "" 
    Set r1 = sht.Range("$b$6:$bm$6,$B$9:$Bm$9,$B$12:$Bm$12,$b$15:$bm$15,$B$18:$Bm$18,$b$21:$bm$21,$b$24:$bm$24,$b$27:$bm$27,$b$30:$bm$30,$b$33:$bm$33") 
    Set r2 = sht.Range("$B$36:$Bm$36,$B$39:$Bm$39,$B$42:$Bm$42,$B$45:$Bm$45,$B$48:$Bm$48,$B$51:$Bm$51,$B$54:$Bm$54,$B$57:$Bm$57,$B$60:$Bm$60,$B$63:$Bm$63,$B$66:$Bm$66,$B$69:$Bm$69") 
    Set r3 = sht.Range("$B$72:$BO$72,$B$75:$BO$75,$B$78:$BO$78,$B$81:$BO$81,$B$84:$BO$84,$B$87:$BO$87,$B$90:$BO$90,$B$93:$BO$93,$B$96:$BO$96") 
    Set r = Union(r1, r2, r3) 
    Debug.Print r.Address 'THIS WILL PRINT THE ADDRESS PARTIALLY: 
'$B$6:$BM$6,$B$9:$BM$9,$B$12:$BM$12,$B$15:$BM$15,$B$18:$BM$18,$B$21:$BM$21,$B$24:$BM$24,$B$27:$BM$27,$B$30:$BM$30,$B$33:$BM$33,$B$36:$BM$36,$B$39:$BM$39,$B$42:$BM$42,$B$45:$BM$45,$B$48:$BM$48,$B$51:$BM$51,$B$54:$BM$54,$B$57:$BM$57,$B$60:$BM$60,$B$63:$BM$63 

    Set dataRange = r 

End Function 
+0

[Range limit conundrum]的可能重複(http://stackoverflow.com/questions/1663264/range-limit-conundrum) – 2015-02-09 19:20:47

回答

0
Public Function dataRange() As Range 
    Dim sht As Worksheet, rv As Range, r As Range 
    Dim i as long 

    Set sht = Sheets(Format(Now, "MMM-YYYY")) 

    for i=6 to 96 step 3 
     Set r = sht.Range("b" & i ":bm" & i) 
     if rv is nothing then 
      Set rv = r 
     else 
      Set rv = Application.Union(rv.r) 
     end if 
    Next i 

    Set dataRange = rv 
End Function 
0

沒有與範圍對象一些限制需要使用,你觀察Union功能。我不確定2013年到底是什麼,但我似乎記得以前版本的Excel中有255個字符限制(可能會被誤解)。無論如何,這將解釋分配給該範圍的錯誤,並且它也將解釋爲什麼r.Address似乎會截斷您的結果。

使用Union功能在一個循環中,如果你需要調試,驗證地址字符串是準確的,你將需要分配回一個字符串變量,也是一個循環在範圍內的.Areas集合:

Sub foo() 
    Dim s As String 
    Dim r As Range 
    Dim i As Integer 
    Dim a As Range 

    s = "" 


    Set r = Range("B6:BM9") 'initial range 

    For i = 9 To 96 Step 3 
     Set r = Union(r, Range("$B$" & i & ":$BM$" & i)) 
    Next 

    'If you need to *see* the range address, you'll need to reconstruct it 

    For Each a In r.Areas 
     If Not s = vbNullString Then 
      s = s & "," & a.Address 
     Else 
      s = a.Address 
     End If 
    Next 

    Debug.Print r.Address 

    Debug.Print s 

End Sub 

這將打印爲r以下(其截斷):

$B$6:$BM$9,$B$12:$BM$12,$B$15:$BM$15,$B$18:$BM$18,$B$21:$BM$21,$B$24:$BM$24,$B$27:$BM$27,$B$30:$BM$30,$B$33:$BM$33,$B$36:$BM$36,$B$39:$BM$39,$B$42:$BM$42,$B$45:$BM$45,$B$48:$BM$48,$B$51:$BM$51,$B$54:$BM$54,$B$57:$BM$57,$B$60:$BM$60,$B$63:$BM$63,$B$66:$BM$66 

這將打印爲s

以下
$B$6:$BM$9,$B$12:$BM$12,$B$15:$BM$15,$B$18:$BM$18,$B$21:$BM$21,$B$24:$BM$24,$B$27:$BM$27,$B$30:$BM$30,$B$33:$BM$33,$B$36:$BM$36,$B$39:$BM$39,$B$42:$BM$42,$B$45:$BM$45,$B$48:$BM$48,$B$51:$BM$51,$B$54:$BM$54,$B$57:$BM$57,$B$60:$BM$60,$B$63:$BM$63,$B$66:$BM$66,$B$69:$BM$69,$B$72:$BM$72,$B$75:$BM$75,$B$78:$BM$78,$B$81:$BM$81,$B$84:$BM$84,$B$87:$BM$87,$B$90:$BM$90,$B$93:$BM$93,$B$96:$BM$96 
相關問題