Can anyone tell me why is this appening?

KhallP

Board Regular
Joined
Mar 30, 2021
Messages
157
Office Version
  1. 2016
Platform
  1. Windows
VBA Code:
Private Sub UserForm_Initialize()

Call CallTime_Msg
lb_hello.Caption = msg
lb_tec.Caption = Tec

End Sub



Sub CallTime_Msg()

Dim mHour As Integer
Dim msg As String

mHour = hour(Now)
If mHour < 12 Then
   msg = "Good Morning"
ElseIf mHour < 18 Then
   msg = "Good Afternoon"
Else
   msg = "Good Evening"
End If

End Sub


Error:

Capturar.JPG



"Expected Array" (???)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
It would seem you have declared something called 'hour' somewhere. To confirm, change the code to use VBA.Hour - if it works, then my suspicion is correct.
 
Upvote 0
It would seem you have declared something called 'hour' somewhere. To confirm, change the code to use VBA.Hour - if it works, then my suspicion is correct.
Yeah, the error has gone, but doesn´t appear text in the label:

Capturar.JPG





The message needs to appear there:

Capturar2.JPG



VBA Code:
Private Sub UserForm_Initialize()

Call CallTime_Msg
lb_hello.Caption = msg
lb_tec.Caption = Tec

End Sub



Sub CallTime_Msg()

Dim mHour As Integer
Dim msg As String

mHour = VBA.hour(Now)
If mHour < 12 Then
   msg = "Good Morning"
ElseIf mHour < 18 Then
   msg = "Good Afternoon"
Else
   msg = "Good Evening"
End If

End Sub
 
Last edited by a moderator:
Upvote 0
Please use code tags when posting code.

Your msg variable is declared inside the CallTime_Msg routine, so only exists in that routine - it has no value or meaning anywhere else. You need to move the Dim msg as string line to the top of the module above any routines, or change that sub into a function that returns the relevant caption, like this:

VBA Code:
Private Sub UserForm_Initialize()

lb_hello.Caption = CallTime_Msg
lb_tec.Caption = Tec

End Sub



Function CallTime_Msg() as String

Dim mHour As Integer
Dim msg As String

mHour = VBA.hour(Now)
If mHour < 12 Then
   msg = "Good Morning"
ElseIf mHour < 18 Then
   msg = "Good Afternoon"
Else
   msg = "Good Evening"
End If
CallTime_Msg = msg
End Function
 
Upvote 0
Please use code tags when posting code.

Your msg variable is declared inside the CallTime_Msg routine, so only exists in that routine - it has no value or meaning anywhere else. You need to move the Dim msg as string line to the top of the module above any routines, or change that sub into a function that returns the relevant caption, like this:

VBA Code:
Private Sub UserForm_Initialize()

lb_hello.Caption = CallTime_Msg
lb_tec.Caption = Tec

End Sub



Function CallTime_Msg() as String

Dim mHour As Integer
Dim msg As String

mHour = VBA.hour(Now)
If mHour < 12 Then
   msg = "Good Morning"
ElseIf mHour < 18 Then
   msg = "Good Afternoon"
Else
   msg = "Good Evening"
End If
CallTime_Msg = msg
End Function
Thank you very much man!!
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,623
Latest member
Techenthusiast

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