當我嘗試運行這個函數來得到字符串範圍我得到的錯誤信息:"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
[Range limit conundrum]的可能重複(http://stackoverflow.com/questions/1663264/range-limit-conundrum) – 2015-02-09 19:20:47