Object required error from If syntax? - object

The below syntax:
Private Sub UserForm_Activate()
If Hour(Now) < 7 Then MonthView1.Value = Date - 1
If Hour(Now) >= 7 Then MonthView1.Value = Date
WorksOrder.SetFocus
End Sub
generates the error: Object required
Highlighting the "MonthView1.Value = Date" from the 3rd line
Can anyone see the issue please?
Using Excel 2007

Related

Getting Type Mismatch Error while calling sub procedure

I am taking two input from USERFORM "CheckIn Time (hh:mm format)" and "Checkout Time (hh:mm format)" through TextBox, and trying to get immediate output in 3rd Text Box (hh:mm format) which shows difference between check in and checkout.
To achieve this I am using Call Sub procedure method.
However I am getting error
"Run time 13: Type mismatch".
I am new to VBA and I am literally stuck at this since 2 weeks. Please help.
I tried changing DATA type but didn't get any positive result.
Private Sub CHKIN_Change() 'Calling GetTime
Call GetTime
End Sub
Private Sub CHKOUT_Change() 'Calling GetTime
Call GetTime
End Sub
Private Sub TOTALHRS_Change() 'Calling GetTime
Call GetTime
End Sub
'below Calculating difference b/t Checkin & Checkout, displaying in "TOTALHRS" textbox
Sub GetTime()
Dim Time1, Time2, TotalTime As Date
Time1 = CHKIN.Text
Time2 = CHKOUT.Text
TotalTime = DateDiff("s", Time1, Time2)
TotalTime = Totalhour / 86400
TOTALHRS.Text = Format(Totalhour, "hh:mm")
End Sub
'Using below code to convert checkin and checkout input in hh:mm format.
Private Sub CHKIN_AfterUpdate()
Dim tDate As Date
Dim tString As String
With CHKIN
'Check if user put in a colon or not
If InStr(1, .Value, ":", vbTextCompare) = 0 Then
'If not, make string 4 digits and insert colon
tString = Format(.Value, "0000")
tDate = TimeSerial(Left(tString, 2), Right(tString, 2), 0)
CHKIN.Value = Format(tDate, "HH:MM")
Else
'Otherwise, take value as given
.Value = Format(.Value, "hh:mm")
End If
End With
Exit Sub
End Sub
Desired result wanted: checkout - checkin = hh:mm
Example: 23:20 - 9:35 = 13:45

Stop changing date value in VBA

Excel keeps changing my date to US format. I have a user form where the user inputs a date. As an example, I enter 01/11/2018 (1st November 2018). I then put this value into a cell however it changes automatically to 11/01/2018 or 11th January 2018.
I've so far tried using the following code to force it to use UK format;
SHT_data_TASKS.Cells(1 + tableRows, 8).NumberFormat = "dd/mm/yyyy;#"
SHT_data_TASKS.Cells(1 + tableRows, 8) = form_addTask_Date.Value
But it does not help. I've also tried using CDate function but still no luck. I've checked my computer regional settings and they are all correct for the UK.
If I right click on the cell where the date is placed and chose format it shows as already being formatted to UK date! I'm not sure what is causing it to change.
This is the date code I use on control containing UK dates:
Private Sub UserForm_Initialize()
Me.txtDate = Format(Date, "dd-mmm-yyyy")
End Sub
On the AfterUpdate event for the date control:
Private Sub txtDate_AfterUpdate()
With Me
FormatDate .txtDate
End With
End Sub
In a normal module:
Public Sub FormatDate(ctrl As Control)
Dim dDate As Date
Dim IsDate As Boolean
If Replace(ctrl.Value, " ", "") <> "" Then
On Error Resume Next
dDate = CDate(ctrl.Value)
IsDate = (Err.Number = 0)
Err.Clear
On Error GoTo 0
If IsDate Then
ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
ctrl.BackColor = RGB(255, 255, 255)
Else
ctrl.BackColor = RGB(255, 0, 0)
End If
End If
End Sub
Then I transfer it to the worksheet (on a Save button) using the below code:
shtRawData.Cells(rLastCell.Row, CLng(lCol)) = CDate(ctrl.Value)
When you're about to write your date to the sheet:
Verify that it's being stored as a date (or date equivalent) with isdate()
Assign it via Range.FormulaLocal instead of value or value2.
Untested, but i.e.
Range("A1").FormulaLocal = #10/31/2018#

Dates from Task Form show US format unless date does not exist

I have a small user interface, using Excel's Form functionality, which allows users to input start and finish dates for projects.
If the user puts in dates that are available in both the US and UK formats (e.g. 7th May 2018, 5/7/2018) the variable will come out in the US format (e.g. 5th July 2018, 5/7/2018). However, if the dates are not available in both formats (e.g. 31st August 2018, 31/8/2018), the variable will return in the (correct) UK format.
My current code for the particular variables in the form is as follows:
Private Sub Calculate_Click()
sDate = CDate(sDate.Text)
eDate = CDate(eDate.Text)
If sDate = vbNullString Then
sDate = Now()
End If
If eDate = vbNullString Then
eDate = Now() + 40
End If
If HoursPD = vbNullString Then
HoursPD = 6
ElseIf HoursPD > 7.5 Then
HoursPD = 7.5
End If
Me.Hide
End Sub
Is there any way to ensure that the variable format is fixed to the UK version?
I'd use a separate procedure to check any dates on the form.
This also colours the control red if a non-valid date is entered.
Formatting the date as dd-mmm-yyyy makes it easier to spot a date in the wrong format as month is written in full(ish).
Public Sub FormatDate(ctrl As Control)
Dim dDate As Date
Dim IsDate As Boolean
On Error GoTo ERR_HANDLE
If Replace(ctrl.Value, " ", "") <> "" Then
On Error Resume Next
dDate = CDate(ctrl.Value)
IsDate = (Err.Number = 0)
On Error GoTo 0
On Error GoTo ERR_HANDLE
If IsDate Then
ctrl.Value = Format(ctrl.Value, "dd-mmm-yyyy")
ctrl.BackColor = RGB(255, 255, 255)
Else
ctrl.BackColor = RGB(255, 0, 0)
End If
End If
EXIT_PROC:
On Error GoTo 0
Exit Sub
ERR_HANDLE:
'Error Handling routines.
'DisplayError Err.Number, Err.Description, "mdl_FormatDate.FormatDate()"
Resume EXIT_PROC
End Sub
This is then called on the AfterUpdate event of the control:
Private Sub txtDate_AfterUpdate()
On Error GoTo ERR_HANDLE
With Me
FormatDate .txtDate
End With
EXIT_PROC:
On Error GoTo 0
Exit Sub
ERR_HANDLE:
'Error Handling routines.
'DisplayError Err.Number, Err.Description, "Data_Entry_Form.txtDate_AfterUpdate()"
Resume EXIT_PROC
End Sub
You could do something like below, but your users should know to enter the dates in the correct (UK) format:
sDate = Format(CDate(sDate.Text), "dd/mm/yyyy")
EDate = Format(CDate(EDate.Text), "dd/mm/yyyy")

VBA Excel - Mantaining Public Variables With Button Press

I'm trying to Create a document that has a bunch o constant strings.
I've declared then Public in a Module like this:
Public Abc As String
In "ThisWorkbook" I run the following code to initialize de variable
Private Sub Workbook_Open()
Abc = "C5"
End Sub
I have Buttons coded to change some values like:
If Range(Abc) = "" Then
Range(Abc) = 1
Else
Range(Abc) = Range(Abc) + 1
End If
When I run a button with this code:
Sub BotaoNovoDia()
i = 3
While i <= 33
If Cells(i, 11) = "" Then
Cells(i, 11) = Range(Apresentacao)
Cells(i, 12) = Range(Aceitacao)
Cells(i, 13) = Range(Aceitou)
Cells(i, 31) = Range("D41")
Cells(i, 11).Interior.Color = Range(Apresentacao).Interior.Color
Cells(i, 12).Interior.Color = Range(Aceitacao).Interior.Color
If Range("K34") < 0.65 Then
Range("K34").Interior.Color = vbRed
Else
Range("K34").Interior.Color = vbGreen
End If
If Range("L34") < 0.45 Then
Range("L34").Interior.Color = vbRed
Else
Range("L34").Interior.Color = vbGreen
End If
Range(Aceitou) = 0
Range(Rejeitou) = 0
Range(NaoApres) = 0
End
Else
i = i + 1
End If
Wend
End Sub
And i try to run the first button again I get and error saying: "Run-Time error '1004': Method 'Range' of object '_Global' failed"
the debug button takes me to the fisrt line that tries to access to the public variables value. What can i do to mantain the values in the Public variables?
When you call End (By itself, not as part of End If, etc) you clear your Globals.
Don't use End.
Using a named range is a great idea; but there's no reason why your public declaration shouldn't work - except for that pesky End statement. (I missed that first read through...)
However, your scope isn't clear in each of your functions, e.g. which worksheet is the Range referring to, so if one function works on another worksheet, pushing the button that fires "Change" could refer to a different place that doesn't like that reference.
e.g. your ranges should be something like SomeWorkbook.TheWorksheet.Range(<range>) and when you're changing the cell value, you should use .Value to ensure there's no ambiguity - you'll know from searching through here that error 1004 is the least descriptive error code...

Error 424 on Excel

I'm working with exceland I try to run this command and I get the 424 Error.... The code is very simple and yet I can't find a solution.
Private Sub Run_Click()
Dim Weekly As Worksheet
Set Weekly = ThisWorkbook.Sheets("Weekly")
Todaydate = Range("D4").Value
foo = WorksheetFuncton.Match(Todaydate, Weekly.Range("A1489:A1499"), 0)
End Sub
EDIT:
I am now getting Runtime error 1004
Private Sub Run_Click()
Dim Weekly As Worksheet
Set Weekly = ThisWorkbook.Sheets("Weekly")
Todaydate = Range("D4").Value
foo = WorksheetFunction.Match(Todaydate, Weekly.Range("A1489:A1499"), 0)
End Sub
EDIT:
Fixed it!
I was mising a ".Value"
foo = WorksheetFunction.Match(Todaydate, Weekly.Range("A1489:A1499").Value, 0)
Check your spelling:
WorkSheetFunct i on

Resources