Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. subscript out of range

subscript out of range

Scheduled Pinned Locked Moved Visual Basic
data-structureshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Danisto
    wrote on last edited by
    #1

    hi, please see code below...i keep on get in a "subscript out of range" error for the array v(n), my option base is 0 even if i reset the array to v(n-1) it does not accept. is the something i am missing Public Function datevec(d1 As Variant, d2 As Variant, freq As Variant) Dim v() Dim n As Long Dim c1, c2 As Date ReDim v(n) As Variant If IsDate(d1) = True And IsDate(d2) = True Then c1 = DateSerial(Year(d1), Month(d1), Day(d1)) c2 = DateSerial(Year(d2), Month(d2), Day(d2)) n = Application.Round(VBA.DateDiff("d", c1, c2) / 365 * freq, 0) Select Case freq Case 1 For Each i In v(n) v(i) = VBA.DateAdd("yyyy", i + 1, c1) Next i Case 2 For Each i In v(n) v(i) = VBA.DateAdd("q", 2 * (i + 1), c1) Next i Case 4 For Each i In v(n) v(i) = VBA.DateAdd("q", i + 1, c1) Next i Case 12 For Each i In v(n) v(i) = VBA.DateAdd("M", i + 1, c1) Next i End Select datevec = v End If End Function

    S 1 Reply Last reply
    0
    • D Danisto

      hi, please see code below...i keep on get in a "subscript out of range" error for the array v(n), my option base is 0 even if i reset the array to v(n-1) it does not accept. is the something i am missing Public Function datevec(d1 As Variant, d2 As Variant, freq As Variant) Dim v() Dim n As Long Dim c1, c2 As Date ReDim v(n) As Variant If IsDate(d1) = True And IsDate(d2) = True Then c1 = DateSerial(Year(d1), Month(d1), Day(d1)) c2 = DateSerial(Year(d2), Month(d2), Day(d2)) n = Application.Round(VBA.DateDiff("d", c1, c2) / 365 * freq, 0) Select Case freq Case 1 For Each i In v(n) v(i) = VBA.DateAdd("yyyy", i + 1, c1) Next i Case 2 For Each i In v(n) v(i) = VBA.DateAdd("q", 2 * (i + 1), c1) Next i Case 4 For Each i In v(n) v(i) = VBA.DateAdd("q", i + 1, c1) Next i Case 12 For Each i In v(n) v(i) = VBA.DateAdd("M", i + 1, c1) Next i End Select datevec = v End If End Function

      S Offline
      S Offline
      soporificeffect
      wrote on last edited by
      #2

      Try moving your ReDim statement bellow the assignment of n like this: n = Application.Round(VBA.DateDiff("d", c1, c2) / 365 * freq, 0) ReDim v(n) As Variant

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups