njadvani
04-03-2024, 07:28 PM
Hi Fellow users,
I have a code with self-learning and bit of chat GPT.
The code and userform runs alright. Just a tiny bit of thing worries me. That No matter what I change or do, when I enter date in Start time and Stop time field, it always displays in mm-dd-yyyy format.
Now, before you shoot me with obvious reason, I will tell you all the things I have tried:-
- Checked my computers system Date setting.
- Checked excel's date default setting.
- Changed Formating of column to dd-mm-yyyy (custom formatting)
- Tried changing input to text instead of format now.
Can someone please help out?
Here's the code:
Private Sub cmddelete_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
Rows(y).Delete
End If
Next y
'Clear Boxes
Me.txtsearch.Value = ""
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.ComboBox1.Value = ""
MsgBox "Data has been deleted", vbInformation
End Sub
Private Sub cmdexit_Click()
If MsgBox("Do you want to exit this form?", vbQuestion + vbYesNo, "Confirmation") = vbYes Then
Unload Me
End If
End Sub
Private Sub cmdreset_Click()
Unload Me
UserForm1.Show
End Sub
Private Sub cmdsave_Click()
Dim sh As Worksheet
Dim lr As Long
Dim dataRange As Range
' Set worksheet and find the last row
Set sh = ThisWorkbook.Sheets("Worksheet")
lr = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
' Define the range containing the existing data
Set dataRange = sh.Range("A8:J" & lr)
' Insert a new row at the top of the table
sh.Rows("8:8").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'VALIDATION
If Me.txtwtg.Value = "" Then
MsgBox "Please enter the WTG No.", vbCritical
Exit Sub
End If
If IsNumeric(Me.txtwindspeed) = False Then
MsgBox "Please enter the wind speed", vbCritical
Exit Sub
End If
If IsNumeric(Me.txtalarmcode) = False Then
MsgBox "Please enter the alarm code", vbCritical
Exit Sub
End If
'Add Data in Excel Sheet
With sh
' Copy format from the row below the inserted row
dataRange.Rows(2).Copy
.Rows(8).PasteSpecial Paste:=xlPasteFormats
' Copy values from the userform to the inserted row
.Cells(8, "A").Value = Me.cmbwf.Value
.Cells(8, "B").Value = Me.txtwtg.Value
.Cells(8, "C").Value = Me.txtwindspeed.Value
.Cells(8, "D").Value = Me.txtalarmcode.Value
.Cells(8, "E").Value = Me.txtalarmdes.Value
.Cells(8, "F").Value = Me.txtstoptime.Value
.Cells(8, "G").Value = Me.txtstarttime.Value
.Cells(8, "H").Value = Me.ComboBox1.Value
.Cells(8, "I").Value = Me.cmballocation.Value
.Cells(8, "J").Value = Me.cmbattend.Value
End With
'Clear Boxes
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.txtdowntime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.txtnotes.Value = ""
Me.ComboBox1.Value = ""
Call Refresh_data
MsgBox "Data has been added to the worksheet", vbInformation
Call Refresh_data
End Sub
Sub Refresh_data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Worksheet")
Dim lr As Long
lr = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
If lr = 7 Then lr = 8
With Me.ListBox
.ColumnCount = 10
.ColumnHeads = True
.ColumnWidths = "90, 30,30,30,100,80,80,80,90,100"
.RowSource = "Worksheet! A8:J" & lr
End With
End Sub
Private Sub cmdsearch_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
cmbwf = Sheets("Worksheet").Cells(y, 1).Value
txtwtg = Sheets("Worksheet").Cells(y, 2).Value
txtwindspeed = Sheets("Worksheet").Cells(y, 3).Value
txtalarmcode = Sheets("Worksheet").Cells(y, 4).Value
txtstoptime = Sheets("Worksheet").Cells(y, 5).Value
txtstarttime = Sheets("Worksheet").Cells(y, 6).Value
ComboBox1 = Sheets("Worksheet").Cells(y, 7).Value
cmballocation = Sheets("Worksheet").Cells(y, 8).Value
cmbattend = Sheets("Worksheet").Cells(y, 9).Value
End If
Next y
End Sub
Private Sub cmdupdate_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
Sheets("Worksheet").Cells(y, 1).Value = cmbwf
Sheets("Worksheet").Cells(y, 2).Value = txtwtg
Sheets("Worksheet").Cells(y, 3).Value = txtwindspeed
Sheets("Worksheet").Cells(y, 4).Value = txtalarmcode
Sheets("Worksheet").Cells(y, 5).Value = txtstoptime
Sheets("Worksheet").Cells(y, 6).Value = txtstarttime
Sheets("Worksheet").Cells(y, 7).Value = ComboBox1
Sheets("Worksheet").Cells(y, 8).Value = cmballocation
Sheets("Worksheet").Cells(y, 9).Value = cmbattend
End If
Next y
Me.txtsearch.Value = ""
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.txtdowntime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.ComboBox1.Value = ""
MsgBox "Data has been updated in the worksheet", vbInformation
End Sub
Private Sub txtstarttime_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Check if the user pressed Ctrl + ;
If KeyCode = 186 And (Shift And 2) Then
' Input today's date and current time into the text box
Me.txtstarttime.Value = Format(Now, "dd/mm/yyyy hh:mm")
End If
End Sub
Private Sub txtstoptime_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Check if the user pressed Ctrl + ;
If KeyCode = 186 And (Shift And 2) Then
' Input today's date and current time into the text box
Me.txtstoptime.Value = Format(Now, "dd/mm/yyyy hh:mm")
End If
End Sub
Private Sub UserForm_Activate()
cmbwf.List = Array("North Brown Wind Farm", "The Bluff Wind Farm", "Hallet Wind Farm", "Hallet Hill Wind Farm", "Oaklands Hill Wind Farm", "Snowtown Wind Farm", "Clements Gap Wind Farm")
cmballocation.List = Array("Manufacturer", "Owner")
cmbattend.List = Array("Nandit", "Dhruv", "Pragnesh", "Janak", "Other")
ComboBox1.List = Array("Reset by Suzlon Monitoring Centre", "Reset by Site tech", "Reset by India Team", "Breakdown", "Repetitive Alarm-Technician has to attend", "")
Call Refresh_data
End Sub
Private Sub txtalarmcode_AfterUpdate()
Dim ws As Worksheet
Dim lookupValue As String
Dim resultValue As Variant
Dim lastRow As Long
Dim i As Long
' Set the reference to the "alarmcode" sheet
Set ws = ThisWorkbook.Sheets("alarmcode")
' Find the last row of data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Get the value from txtalarmcode
lookupValue = Me.txtalarmcode.Value
' Loop through the rows of the "alarmcode" sheet to find the corresponding value
For i = 2 To lastRow
If ws.Cells(i, 1).Value = lookupValue Then
' If a match is found, assign the corresponding values to txtalarmdes and txtnotes
Me.txtalarmdes.Value = ws.Cells(i, 2).Value
Me.txtnotes.Value = ws.Cells(i, 3).Value
Exit Sub
End If
Next i
' If no corresponding value is found, clear txtalarmdes and txtnotes
Me.txtalarmdes.Value = ""
Me.txtnotes.Value = ""
Call Refresh_data
End Sub
I have a code with self-learning and bit of chat GPT.
The code and userform runs alright. Just a tiny bit of thing worries me. That No matter what I change or do, when I enter date in Start time and Stop time field, it always displays in mm-dd-yyyy format.
Now, before you shoot me with obvious reason, I will tell you all the things I have tried:-
- Checked my computers system Date setting.
- Checked excel's date default setting.
- Changed Formating of column to dd-mm-yyyy (custom formatting)
- Tried changing input to text instead of format now.
Can someone please help out?
Here's the code:
Private Sub cmddelete_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
Rows(y).Delete
End If
Next y
'Clear Boxes
Me.txtsearch.Value = ""
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.ComboBox1.Value = ""
MsgBox "Data has been deleted", vbInformation
End Sub
Private Sub cmdexit_Click()
If MsgBox("Do you want to exit this form?", vbQuestion + vbYesNo, "Confirmation") = vbYes Then
Unload Me
End If
End Sub
Private Sub cmdreset_Click()
Unload Me
UserForm1.Show
End Sub
Private Sub cmdsave_Click()
Dim sh As Worksheet
Dim lr As Long
Dim dataRange As Range
' Set worksheet and find the last row
Set sh = ThisWorkbook.Sheets("Worksheet")
lr = sh.Cells(sh.Rows.Count, "A").End(xlUp).Row
' Define the range containing the existing data
Set dataRange = sh.Range("A8:J" & lr)
' Insert a new row at the top of the table
sh.Rows("8:8").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'VALIDATION
If Me.txtwtg.Value = "" Then
MsgBox "Please enter the WTG No.", vbCritical
Exit Sub
End If
If IsNumeric(Me.txtwindspeed) = False Then
MsgBox "Please enter the wind speed", vbCritical
Exit Sub
End If
If IsNumeric(Me.txtalarmcode) = False Then
MsgBox "Please enter the alarm code", vbCritical
Exit Sub
End If
'Add Data in Excel Sheet
With sh
' Copy format from the row below the inserted row
dataRange.Rows(2).Copy
.Rows(8).PasteSpecial Paste:=xlPasteFormats
' Copy values from the userform to the inserted row
.Cells(8, "A").Value = Me.cmbwf.Value
.Cells(8, "B").Value = Me.txtwtg.Value
.Cells(8, "C").Value = Me.txtwindspeed.Value
.Cells(8, "D").Value = Me.txtalarmcode.Value
.Cells(8, "E").Value = Me.txtalarmdes.Value
.Cells(8, "F").Value = Me.txtstoptime.Value
.Cells(8, "G").Value = Me.txtstarttime.Value
.Cells(8, "H").Value = Me.ComboBox1.Value
.Cells(8, "I").Value = Me.cmballocation.Value
.Cells(8, "J").Value = Me.cmbattend.Value
End With
'Clear Boxes
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.txtdowntime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.txtnotes.Value = ""
Me.ComboBox1.Value = ""
Call Refresh_data
MsgBox "Data has been added to the worksheet", vbInformation
Call Refresh_data
End Sub
Sub Refresh_data()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Worksheet")
Dim lr As Long
lr = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
If lr = 7 Then lr = 8
With Me.ListBox
.ColumnCount = 10
.ColumnHeads = True
.ColumnWidths = "90, 30,30,30,100,80,80,80,90,100"
.RowSource = "Worksheet! A8:J" & lr
End With
End Sub
Private Sub cmdsearch_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
cmbwf = Sheets("Worksheet").Cells(y, 1).Value
txtwtg = Sheets("Worksheet").Cells(y, 2).Value
txtwindspeed = Sheets("Worksheet").Cells(y, 3).Value
txtalarmcode = Sheets("Worksheet").Cells(y, 4).Value
txtstoptime = Sheets("Worksheet").Cells(y, 5).Value
txtstarttime = Sheets("Worksheet").Cells(y, 6).Value
ComboBox1 = Sheets("Worksheet").Cells(y, 7).Value
cmballocation = Sheets("Worksheet").Cells(y, 8).Value
cmbattend = Sheets("Worksheet").Cells(y, 9).Value
End If
Next y
End Sub
Private Sub cmdupdate_Click()
Dim x As Long
Dim y As Long
x = Sheets("Worksheet").Range("A" & Rows.Count).End(xlUp).Row
For y = 8 To x
If Sheets("Worksheet").Cells(y, 1).Value = txtsearch.Text Then
Sheets("Worksheet").Cells(y, 1).Value = cmbwf
Sheets("Worksheet").Cells(y, 2).Value = txtwtg
Sheets("Worksheet").Cells(y, 3).Value = txtwindspeed
Sheets("Worksheet").Cells(y, 4).Value = txtalarmcode
Sheets("Worksheet").Cells(y, 5).Value = txtstoptime
Sheets("Worksheet").Cells(y, 6).Value = txtstarttime
Sheets("Worksheet").Cells(y, 7).Value = ComboBox1
Sheets("Worksheet").Cells(y, 8).Value = cmballocation
Sheets("Worksheet").Cells(y, 9).Value = cmbattend
End If
Next y
Me.txtsearch.Value = ""
Me.cmbwf.Value = ""
Me.txtwtg.Value = ""
Me.txtwindspeed.Value = ""
Me.txtalarmcode.Value = ""
Me.txtalarmdes.Value = ""
Me.txtstoptime.Value = ""
Me.txtstarttime.Value = ""
Me.txtdowntime.Value = ""
Me.cmballocation.Value = ""
Me.cmbattend.Value = ""
Me.ComboBox1.Value = ""
MsgBox "Data has been updated in the worksheet", vbInformation
End Sub
Private Sub txtstarttime_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Check if the user pressed Ctrl + ;
If KeyCode = 186 And (Shift And 2) Then
' Input today's date and current time into the text box
Me.txtstarttime.Value = Format(Now, "dd/mm/yyyy hh:mm")
End If
End Sub
Private Sub txtstoptime_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
' Check if the user pressed Ctrl + ;
If KeyCode = 186 And (Shift And 2) Then
' Input today's date and current time into the text box
Me.txtstoptime.Value = Format(Now, "dd/mm/yyyy hh:mm")
End If
End Sub
Private Sub UserForm_Activate()
cmbwf.List = Array("North Brown Wind Farm", "The Bluff Wind Farm", "Hallet Wind Farm", "Hallet Hill Wind Farm", "Oaklands Hill Wind Farm", "Snowtown Wind Farm", "Clements Gap Wind Farm")
cmballocation.List = Array("Manufacturer", "Owner")
cmbattend.List = Array("Nandit", "Dhruv", "Pragnesh", "Janak", "Other")
ComboBox1.List = Array("Reset by Suzlon Monitoring Centre", "Reset by Site tech", "Reset by India Team", "Breakdown", "Repetitive Alarm-Technician has to attend", "")
Call Refresh_data
End Sub
Private Sub txtalarmcode_AfterUpdate()
Dim ws As Worksheet
Dim lookupValue As String
Dim resultValue As Variant
Dim lastRow As Long
Dim i As Long
' Set the reference to the "alarmcode" sheet
Set ws = ThisWorkbook.Sheets("alarmcode")
' Find the last row of data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Get the value from txtalarmcode
lookupValue = Me.txtalarmcode.Value
' Loop through the rows of the "alarmcode" sheet to find the corresponding value
For i = 2 To lastRow
If ws.Cells(i, 1).Value = lookupValue Then
' If a match is found, assign the corresponding values to txtalarmdes and txtnotes
Me.txtalarmdes.Value = ws.Cells(i, 2).Value
Me.txtnotes.Value = ws.Cells(i, 3).Value
Exit Sub
End If
Next i
' If no corresponding value is found, clear txtalarmdes and txtnotes
Me.txtalarmdes.Value = ""
Me.txtnotes.Value = ""
Call Refresh_data
End Sub