excel vba方法当范围字符串太长时,对象_worksheet的范围失败

时间:2015-02-09 18:56:44

标签: excel vba excel-vba

当我尝试运行此函数以获取字符串范围时,我收到错误消息:"Method Range of object _Worksheet failed. 我正在运行excel 2013和MSVBA 7.1

这是我的代码:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

范围对象存在一些限制,需要在观察时使用Union函数。我不确定2013年到底是什么,但我似乎回想起之前版本的Excel中有255个字符的限制(可能会有误)。在任何情况下,这都可以解释分配给范围的错误,并且还解释了为什么r.Address似乎会截断您的结果。

在循环中使用Union函数,如果需要调试以验证地址字符串是否准确,则需要分配回字符串变量,也可以在范围{{{ 1}}集合:

.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