Date format in Combo box - vba

Actually I am writing a code for highlight or delete the rows where column "A" has > the date selected in combo box. I am using below codes
Private Sub ComboBox1_Change()
Application.EnableEvents = False
ComboBox1.Value = Format(ComboBox1.Value, "dd-mm-yyyy")
Application.EnableEvents = True
End Sub
Private Sub ComboBox2_Change()
ComboBox2.Value = Format(ComboBox2.Value, "dd-mm-yyyy")
End Sub
Private Sub okButton_Click()
Dim i As Long
For i = 2 To 6724
If Range("A" & i).Value > ComboBox1.Value Then
With Selection.Interior
.ColorIndex = 34
.Pattern = xlSolid
End With
End If
Next
End Sub
When I execute this nothing happens.
Then print combo box value in cell so that I can find out the place where the problem is, and I found that the date format in combo box & print date is different in format.
there may be other issue with my code & enable event= false is also not working. any help will be greatly appreciated.

I'm on an iPad now and can't check what I propose, but the way I would tackle this problem is to ensure that column A has a date in it, a value like 41234, and never mind how that cell is formatted.
You can convert any date in Combobox1 using CDate(Combobox1.Value). Bear in mind that CDate requires a valid date in string format. The result of the CDate function is a Long, which is comparable with the date value in your column A. You should be able to use your existing code to act on it.

Related

Excel VBA error 424 on Filtering Issue

Awesome StackOverflow users,
I have created a textbox to search a scope of information in Excel VBA. I use function filter to do the searching, but when I type something in the textbox, the error "424" appears. What should I do with that? Here is the code down below. Content is the name of a worksheet which includes a set of data that needs to be filtered.
Private Sub TextBox1_Change()
If Len(TextBox1.Value) = 0 Then
Content.AutoFilterMode = False
Else
If Content.AutoFilterMode = True Then
Content.AutoFilterMode = False
End If
Content.Range("T11:W" & Rows.count).AutoFilter field:=1, Criterial:="*" & TextBox1.Value & "*"
End If
End Sub

Warning message in Excel macro if combobox null

I create combo box selection using userform in Excel macro.
What I want to do is, to prevent the user to click OK without selecting a value.
Here is my code, I don't know what is wrong, the message box doesn't show.
Private Sub UserForm_Initialize()
ComboBox1.RowSource = "Sheet1!G1:G" & Range("G" & Rows.Count).End(xlUp).Row
ComboBox2.RowSource = "Sheet1!G1:G" & Range("G" & Rows.Count).End(xlUp).Row
End Sub
Private Sub CommandButton1_Click()
If IsNull(ComboBox1) Then
MsgBox ("ComboBox Has Data")
End If
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("B2").Value = ComboBox1.Value
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("C2").Value = ComboBox2.Value
End Sub
Can anybody help what is wrong with my code? Sorry, I'm new to VBA.
You're not checking the Text property of your ComboBox. You should process like this.
Private Sub CommandButton1_Click()
If (ComboBox1.Text = "") Then
MsgBox "ComboBox Has No Data"
Exit Sub
End If
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("B2").Value = ComboBox1.Value
Workbooks("Select Project.xlsm").Sheets("Sheet1").Range("C2").Value = ComboBox2.Value
End Sub
What changed ?
I changed If IsNull(ComboBox1) Then with If (ComboBox1.Text = "") Then so this will check the Text property in your ComboBox.
I also added Exit Sub to leave the function if the ComboBox is empty so it doesn't commit the operation after.
IsNull(ComboBox1) and IsNull(ComboBox1).Value will both never be true. Null is a value returned from a database if a field contains no value. You have to check if the value of the ComboBox is empty. An empty string in VBA is a string with the length 0, so you have to use on of those:
If Me.ComboBox1 = "" then ...
If Me.ComboBox1.Value = "" then ...
If Me.ComboBox1.Text = "" then ...
(For the difference between value and text-property see Distinction between using .text and .value in VBA Access)
Anyhow, I would go for the solution to enable/disable the button (as Rosetta suggested). Put a event-routine to the Combobox:
Private Sub ComboBox1_Change()
Me.CommandButton1.Enabled = Me.ComboBox1.Value <> ""
End Sub

Input box vba displaying cell number instead of value

I have the following code which prompts the user to click on a cell value.
Dim sDate As Range
On Error Resume Next
Application.DisplayAlerts = False
Set sDate = Application.InputBox(Prompt:= _
"Please select start date.", _
Title:="Start Date", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If sDate Is Nothing Then
Exit Sub
Else
sDate.Font.Bold = True
End If
End Sub
The input box however once a value is selected lets say for example I click on b3 displays $b$3. I want to display the value that's inside $b$3. For example if 17-Jun was inside $b$3 it should display 17-Jun and not $b$3 in the input box.
Another answer is to use a UserForm.
Create a userform, such as this:
Notes: The "Ok" button is named "Ok", and the white text box is "dateBox"
For the Form Code, use:
Private Sub Ok_Click()
ActiveCell.Font.Bold = True
UserForm1.Hide
End Sub
Then in the Worksheet module, put this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then UserForm1.dateBox.Value = Target.Value
End Sub
Sub bold_Date2()
UserForm1.Show vbModeless
End Sub
Then run the bold_date2():
What I understand that you need to take input from user, and assuming that only single cell will be provided. And you need to retrieve selected cell's value and set it's font style to bold.
You can achieve this by first get selected cell reference, use Font property to set any font styles and read its Value property to get its content.
Set sDate = Application.InputBox(Prompt:= _
"Please select start date.", _
Title:="Start Date", Type:=8)
sDate.Font.Bold = True
MsgBox ("Selected cell's value is: " & sDate.Value)
How does this work for you?
Sub bold_Date()
Dim sDate As Range
On Error Resume Next
Application.DisplayAlerts = False
Set sDate = Application.InputBox(Prompt:="Please select start date.", Title:="Start Date", Type:=8)
If sDate.Cells.Count > 1 Then Set sDate = sDate.Cells(1, 1)
MsgBox ("Date is: " & sDate.Text)
Application.DisplayAlerts = True
If sDate Is Nothing Then
Exit Sub
Else
sDate.Font.Bold = True
End If
End Sub
It will put a messagebox with the date, after selecting a range. Also, it has a check for multiple cells. If multiple cells are selected, it uses the first cell in that range as your new sDate.

Excel VBA - Insert Username ONLY when cell is changed

Here's my problem: I have working code to insert a username and timestamp when a user makes a change anywhere in a row. Great! So my code works and I answered my own question, right? Nope! There's a tiny issue which, while it doesn't break the code, does lead to a user having their username input as having made a change when a change was not made.
Here's my code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
ThisRow = Target.Row
'protect Header row from any changes
If (ThisRow = 1) Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Header Row is Protected."
Exit Sub
End If
For i = 1 To 61
If Target.Column = i Then
' time stamp corresponding to cell's last update
Range("BK" & ThisRow).Value = Now
' Windows level UserName | Application level UserName
Range("BJ" & ThisRow).Value = Environ("username")
Range("BJ:BK").EntireColumn.AutoFit
End If
Next i
End Sub
Here's how it happens: A user decides they want to make a change to a cell, so they double click the cell. Now, if they push the escape key, nothing happens and everything is hunky dory. But, if they double click the cell, then click outside of the cell to another cell to leave that cell, the system logs that as a change even though no change was made and the user's username is put into column 62. This is no bueno, because someone could be held responsible for a mistake that another individual has made if they're incorrectly put down as the last person to change something in that row.
Conversely - it might be worthwhile to create a comment in a cell which is changed by a user, but I reckon I'd have the same issue with double-clicking a cell, so I'd still have to account for it.
Thoughts?
Edit: Full disclosure, I found this code elsewhere and adapted it to my purposes.
You can test to see if the old value and the new value are the same. I use "new" loosely, meaning excel things that the cell was edited so it's a "new" value in terms of the Worksheet_Change event understanding.
I also got rid of your For loop as it seemed very unnecessary. If I am mistaken, I apologize.
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim ThisRow As Long ' make sure to declare all the variables and appropiate types
ThisRow = Target.Row
'protect Header row from any changes
If (ThisRow = 1) Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Header Row is Protected."
Exit Sub
End If
If Target.Column >= 1 And Target.Column <= 61 Then
Dim sOld As String, sNew As String
sNew = Target.Value 'capture new value
With Application
.EnableEvents = False
.Undo
End With
sOld = Target.Value 'capture old value
Target.Value = sNew 'reset new value
If sOld <> sNew Then
' time stamp corresponding to cell's last update
Range("BK" & ThisRow).Value = Now
' Windows level UserName | Application level UserName
Range("BJ" & ThisRow).Value = Environ("username")
Range("BJ:BK").EntireColumn.AutoFit
End If
Application.EnableEvents = True
End If
End Sub

Changing value of cell based on Category in a budget

I'm trying to make a simple budget in excel and I want a macro to see which category I have made changes to and then add the change to an overall overview of the money used in that category.
That is I have at the top of my sheet a cell for the category "Food".
Further down the sheet I put in the following:
Date; Category; Amount of money used.
My code has been used by a button until now.The button checks if the cell 5 places to the left has the value "Dagligvare" and then changes a value somewhere else in the sheet.
My problem is that sometimes the Button recognize the word and sometimes it doesn't. So my question is: Can anybody see why it only works sometimes even though I don't change anything?
Private Sub CommandButton1_Click()
Dim celltxt As String
celltxt = ActiveCell.Offset(0, -5).Text
If InStr(1, celltxt, "Dagligvare") Then
Range("C9") = 3
Else
MsgBox ("Nope")
End If
End Sub
You could add a further msgbox to ensure user has the correct activecell
Private Sub CommandButton1_Click()
Dim celltxt As String
if (activecell.column = 10) then '<<change 10 to appropriate column
celltxt = ActiveCell.Offset(0, -5).Text
If InStr(1, celltxt, "Dagligvare") Then
Range("C9") = 3
Else
MsgBox ("Nope")
End If
else
MsgBox ("Oi! You need to have a cell in column 10 selected")
end
End Sub