• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Compiler Error variable not defined

Wasif85

New Member
Hi All

I have prepare code that will store data in the array & then will get the data from the array, which will be stored in variable.
My code have 2 variant arrays and code work fine for one array but for 2nd array, it give error that variable not defined. Kindly check the code that where it has error. I am attaching the code.

code is given below.

>>> use code - tags <<<
Code:
Option Explicit

Sub course()

Dim lecttime(), stdntdat() As Variant
Dim lrw, lcl, lrow, lcol As Long, a, b, c, d As Long
Dim str As Variant



'cal last row
lrw = Sheet1.Cells.Find(What:="*", lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'cal last column
lcl = Sheet1.Cells.Find(What:="*", lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column

ReDim lecttime(lrw, lcl)
'MsgBox lecttime(lrw, lcl)

For a = 1 To lrw

   For b = 1 To lcl
  
   lecttime(a - 1, b - 1) = Sheet1.Cells(a + 1, b).Value
    b = b + 1
   Next
a = a + 1
Next

ReDim Preserve lecttime(lrw, lcl)

Sheet1.Cells(12, 8).Value = lecttime(0, 0)

'cal last row
lrow = Sheet2.Cells.Find(What:="*", lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'cal last column
lcol = Sheet2.Cells.Find(What:="*", lookat:=xlPart, LookIn:=xlFormulas, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
ReDim stdntdat(lrow, lcol)


  For c = 1 To lrow

   For d = 1 To lcol
  
   stdntdat(c - 1, d - 1) = Sheet2.Cells(c + 1, d).Value
  
   Next
  
Next

ReDim Preserve stdndat(lrow, lcol)  ' Error line'

c = 0
d = 0

Dim str As Variant
steet.Cells(22, 10).Value = stdndat(c, d) ' Error line'

End Sub
 

Attachments

  • VBA Code.txt
    1.5 KB · Views: 0
Last edited by a moderator:
Wasif85
You have there two times Dim str As Variant
One time is enough ... but
str is as written

What would be Your steet?
steet.Cells(22, 10).Value = stdndat(c, d) ' Error line'

Note: Without a sample Excel-file, there are challenges to test and verify everything.
 
Wasif85
You have there two times Dim str As Variant
One time is enough ... but
str is as written

What would be Your steet?
steet.Cells(22, 10).Value = stdndat(c, d) ' Error line'

Note: Without a sample Excel-file, there are challenges to test and verify everything.

Kindly check the attachment which shows the error.
 

Attachments

  • error.png
    error.png
    101.6 KB · Views: 1
Wasif85
Your the attachment isn't any Excel-file.
Why do You have two times stdntdat?

ReDim stdntdat(lrow, lcol)
ReDim Preserve stdndat(lrow, lcol) ' Error line'

Where have You find Your syntaxes for later stdndat?
Code:
Sub TestReDim()
'declare the string array
   Dim strNames() As String
'resize the string array to be able to hold 3 values
   ReDim strNames(1 to 3)
'populate the array
   strNames(1) = "Mel"
   strNames(2) = "Steve"
   strNames(3) = "Bob"
'show the result in the immediate window
   Debug.Print Join(strNames, vbCrLf)
'redim but preseve the data
   ReDim Preserve strNames(1 to 4)
   strNames(4) = "Fred"
'show the result in the immediate window
   Debug.Print Join(strNames, vbCrLf)
End Sub
 
Wasif85
Your the attachment isn't any Excel-file.
Why do You have two times stdntdat?

ReDim stdntdat(lrow, lcol)
ReDim Preserve stdndat(lrow, lcol) ' Error line'

Where have You find Your syntaxes for later stdndat?
Code:
Sub TestReDim()
'declare the string array
   Dim strNames() As String
'resize the string array to be able to hold 3 values
   ReDim strNames(1 to 3)
'populate the array
   strNames(1) = "Mel"
   strNames(2) = "Steve"
   strNames(3) = "Bob"
'show the result in the immediate window
   Debug.Print Join(strNames, vbCrLf)
'redim but preseve the data
   ReDim Preserve strNames(1 to 4)
   strNames(4) = "Fred"
'show the result in the immediate window
   Debug.Print Join(strNames, vbCrLf)
End Sub

thanks for the support i got it. Thumbs up for You bro.
 
Back
Top