Welcome to Dream.In.Code
Become a VB Expert!

Join 137,271 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,444 people online right now. Registration is fast and FREE... Join Now!




Object required error

 
Reply to this topicStart new topic

Object required error

alexcherrivan
20 Dec, 2006 - 11:27 PM
Post #1

New D.I.C Head
*

Joined: 20 Dec, 2006
Posts: 3


My Contributions
I am trying to make a program in MS Excel Macros having the one below as part of the entire program.
My problem is that, once I run the macros program, the error being displayed is "Object required" pointing to the currentCell variable.
I declared currentCell variable as String. What might be the problem?
Pls. help me. Thank you.

CODE
For ECtr = 3 To 252
    
        Set currentCell = Worksheets("Generated Report").Cells(ECtr, "E").Value
        ColumnDate(ECtr) = currentCell
        CDALen = ColumnDate(ECtr).Len
        
        For ctr = 0 To CDALen
            SingleDate(ECtr) = Mid(ColumnDate(ECtr), 0, 11)
            ActiveCell.Value = String(11, SingleDate(ECtr))
        Next ctr
        
    Next ECtr


EDIT: Code Tags -b2c-
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Object Required Error
21 Dec, 2006 - 06:15 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Where have you declared the currentCell variable...is within the scope of the code shown above?
User is online!Profile CardPM
+Quote Post

dino_rpg
RE: Object Required Error
21 Dec, 2006 - 07:24 AM
Post #3

New D.I.C Head
*

Joined: 18 Oct, 2006
Posts: 10


My Contributions
I don't know if this code is Vb.net (if that's so, maybe waht I are going to say do not apply, because I'm talking about VB6), but if "currentCell" is indeed declared as String, then the following is not valid:
CODE

Set currentCell = Worksheets("Generated Report").Cells(ECtr, "E").Value


because "Set" is used for object assignment, not for basic datatypes assigment, so you must simply type:
CODE

currentCell = Worksheets("Generated Report").Cells(ECtr, "E").Value


and it should work
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Object Required Error
21 Dec, 2006 - 07:51 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Good point, dino_rpg! I was jumping ahead for scoping problems...never even looked at the syntax! smile.gif
User is online!Profile CardPM
+Quote Post

alexcherrivan
RE: Object Required Error
21 Dec, 2006 - 11:04 PM
Post #5

New D.I.C Head
*

Joined: 20 Dec, 2006
Posts: 3


My Contributions
Thank you for replying to my question. I am using Visual Basic 6.0.
Here's what I did based on your suggestions:

CODE
Dim currentCell As String
Dim ColumnDate(), SingleDate() As String

For ECtr = 3 To 252
        
        currentCell = Worksheets("Generated Report").Cells(ECtr, "E").Value
        ColumnDate(ECtr) = currentCell
        CDALen = ColumnDate(ECtr).Len
        
        For ctr = 0 To CDALen
            SingleDate(ECtr) = Mid(ColumnDate(ECtr), 0, 11)
            ActiveCell.Value = String(11, SingleDate(ECtr))
        Next ctr
        
Next ECtr


When I tried to run the program containing the above code, here's the error message being displayed, "Subscript out of range" pointing to ColumnDate(ECtr) = currentCell.

I hope you could help me figure out the cause of the problem. Thank you so much!

EDIT: CODE Tags -b2c-
User is offlineProfile CardPM
+Quote Post

dino_rpg
RE: Object Required Error
22 Dec, 2006 - 06:48 AM
Post #6

New D.I.C Head
*

Joined: 18 Oct, 2006
Posts: 10


My Contributions
You need to initialize the arrays you're using ("ColumnDate" and "SingleDate") with the maximum number of elements they will store, in this case, you should type something like this:
CODE

Redim ColumnDate(252)
Redim SingleDate(252)


It seems like 252 is the maximum value according with that "For" loop in your code
Also, I'm not 100% sure anymore, but my experience tells me that declaring many variables inside the same "Dim" but only using one "As" like this:
CODE

Dim ColumnDate(), SingleDate() As String


Just makes the last variable (SingleDate) of the specified datatype, but any previous variables will be declared as Variants (ColumnDate), so in this case, you have that:
SingleDate is a String Array
ColumnDate is a Variant Array

It doesn't matter, because Variant can store any basic datatype, but I think it's better for performance issues to use specific datatype whenever it's possible, instead Variant

This post has been edited by dino_rpg: 22 Dec, 2006 - 06:50 AM
User is offlineProfile CardPM
+Quote Post

alexcherrivan
RE: Object Required Error
1 Jan, 2007 - 09:24 PM
Post #7

New D.I.C Head
*

Joined: 20 Dec, 2006
Posts: 3


My Contributions
CODE
Dim ColumnDate(252) As String
Dim SingleDate(252) As String
Dim currentCell As String
Dim ctr, ECtr, CDALen As Integer

For ECtr = 4 To 252

currentCell = Worksheets("Generated Report").Cells(ECtr, "E").Value
ColumnDate(ECtr) = currentCell
CDALen = Len(ColumnDate(ECtr))

For ctr = 0 To CDALen
SingleDate(ECtr) = Mid(ColumnDate(ECtr), 0, 11)
ActiveCell.Value = String(11, SingleDate(ECtr))
Next ctr

Next ECtr

The problem now is that it displays a different error which is "Invalid procedure call or argument" pointing to SingleDate(ECtr) = Mid(ColumnDate(ECtr), 0, 11) statement.

What might be the problem?
Please help. Thank you.

EDIT: IL and CODE tags -b2c-
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Object Required Error
2 Jan, 2007 - 01:53 AM
Post #8

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
SingleDate(ECtr) = Mid(ColumnDate(ECtr), 0, 11)


You're getting the error because the second parameter of the Mid() Function expects a number not less than 1.

This isn't C/C++, so you have to replace 0 with 1 to start extracting from the 1st character.

Have a Happy New Year and Welcome to </dic> smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 07:56PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month