VBA: compiler error: expected array OR expression not defined in context

yky

Well-known Member
Joined
Jun 7, 2011
Messages
1,881
Office Version
  1. 2010
Platform
  1. Windows
I'm having a problem with the code I'm writing. It doesn't compile. Compiler gives an error message saying "expected array" at the following line:

VoucherYear = Trim(Str(Year(VoucherDate)))

The code first opens a user form. I trace the code and everything works fine in the user form. After the user form, the control is transferred to add_voucher() in the main module. Once the control is transferred to the add_voucher() in the main module, in the debug window VoucherDate still shows as 30/4/2013 but Year(VoucherDate) shows "expression not defined in context" while in the userform1, the same function Year(VoucherDate) in the debug windows shows 2013.

What is going on here? Any help would be greatly appreciated.

Code:
Public VoucherDate As Date <- declared in the main module above any subroutines

' in userform1 code
............
VoucherDate = Format(TextBox1.Text, "dd-mm-yyyy")
End Sub '<- control goes to add_voucher() in the main module
' at this stage, trim(str(year(VoucherDate))) gives the expected string "2013"

Public Sub add_voucher()

Dim wb As Workbook
Dim thisWorkbookName As String

thisWorkbookName = ActiveWorkbook.Name

Call process_userform1_selection 
......

Sub process_userform1_selection()

VoucherYear = Trim(Str([B][COLOR=#ff0000]Year[/COLOR][/B](VoucherDate))) '<- cannot compile the code. The word "Year" is highlighted.
VoucherMonth = Trim(MonthName(month(VoucherDate)))
......
 
Last edited:

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What type of object is VoucherYear defined as in the process_userform1_selection sub?
 
Upvote 0
Have you declared a variable or function called Year anywhere in your code?
 
Upvote 0
It is defined as a string.

Dim VoucherYear As String
 
Upvote 0
Have you declared a variable or function called Year anywhere in your code?

Yes. Can variable and function not have same name?
 
Last edited:
Upvote 0
Yes. Can variable and function not have same name?

They can but its generally not a good idea - you have to be really careful about variable scope and qualifying function calls. Either cajnge the variable name or specify VBA.Year instead of just Year
 
Upvote 0

Forum statistics

Threads
1,215,834
Messages
6,127,164
Members
449,368
Latest member
JayHo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top