Afternoon guys,
I cannot find a way to code the following and I do not have any code to show.
I have a small and simple userform that displays a permit expiry date based on a combo box selection using vlookup.
Screen grab of userform
What I need is to be able to change the date and write that new date back to appropriate cell in the database when hitting the command button on the left. The command button on the right just unloads the form. The form will be used to change the permit expiry date, which is required in a different database to control competition points allocation.
The code that I do have for this userform is as follows (This code is working fine):
Private Sub CmdChangedate_Click()
'This is where the code will be
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
'This is where the name is selected from a combobox and the date is located
Private Sub Hengelaar_Change()
With Me
On Error Resume Next
.Nuwepermitdatum = Application.WorksheetFunction.VLookup(CStr(Me.Hengelaar), Worksheets("Lede Lys").Range("A:J"), 10, 0)
On Error GoTo 0
End With
End Sub
Private Sub Nuwepermitdatum_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
Dim lastrow As Integer
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Me.Nuwepermitdatum.Value = Date
Hengelaar.List = Worksheets("Lede Lys").Range("a3:a" & lastrow).Value
End Sub
Maybe this is what you’re after:
Private Sub CmdChangedate_Click()
With Worksheets("Lede Lys")
.Cells(Application.Match(Me.Hengelaar, .Range("A:A"),0), "J") = Me.Nuwepermitdatum
End With
End Sub
Related
I have an ActiveX Combobox in one of my main sheet which control/update a series of charts.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
End Sub
I noticed that when I click the ComboBox and select one of its content, it leaves a blue highlight on the selection. So to prevent that, I added:
Private Sub cmBoxSelect_DropButtonClick()
Application.ScreenUpdating = False
ActiveCell.Activate
Application.ScreenUpdating = True
End Sub
It successfully removed the highlight.
However, it has a weird drawback. cmbSelect doesn't close automatically once user didn't select anything (once the combobox is active and the user click any cell in the sheet, it doesn't close out). It was working before I added the DropButtonClick event.
Did I missed anything or any wrong steps above? Thanks for your inputs!
EDIT#1
Seems I already found a solution by trial and error. I only added a blank Label and select it to remove the focus out of the ComboBox whenever there is a change. I also changed the DropButtonClick to LostFocus.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_LostFocus()
ActiveCell.Select
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
Me.Label1.Select
End Sub
You need to put the SelLength to 0 in multiple events to avoid highlighting:
so:
Me.cmBoxSelect.SelLength = 0
in:
Private Sub cmBoxSelect_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_LostFocus()
Private Sub cmBoxSelect_DropButtonClick()
Private Sub cmBoxSelect_Change()
Private Sub cmBoxSelect_GotFocus()
(you could add also Me.cmBoxSelect.SelStart = 0 )
Lets try this:
Not Event-triggered by a change, but by the dropbuttonclick
Private Sub changingComboBox(String s)
'series of codes which manipulates the charts, based on selection...
End Sub
Private Sub cmBoxSelect_DropButtonClick()
Dim s As String
s = cmBoxSelect.SelText
If (cmBoxSelect.SelText = cmBoxSelect.Value) Then
cmBoxSelect.Value = ""
cmBoxSelect.Value = s
Else
call changingComboBox(cmBoxSelect.Value)
End If
End Sub
How about that ?
I have a list box not linked to any range.
I want to click on a row and remove it. If I step through the code below, the ListBox1_Click() function ends up being called twice for some reason and the application produces an "Unspecified Error" on the second run
My entire code:
Private Sub ListBox1_Click()
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub
Private Sub UserForm_Initialize()
For i = 0 To 10
ListBox1.AddItem ("A" & Str(i))
Next i
End Sub
If you make a button about it, then this solution would be quite ok there:
Private Sub CommandButton1_Click()
Dim cnt As Long
For cnt = Me.ListBox1.ListCount - 1 To 0 Step -1
If Me.ListBox1.Selected(cnt) Then
Me.ListBox1.RemoveItem cnt
Exit Sub
End If
Next cnt
End Sub
it's kinda similar to a loop but i want to have an input after every increment.
in short, i have a textbox linked to a cell in Excel. i wrote up a script that compares the value in the textbox to the sheet to tally and put a tick in the cell.
however after that i want to move a cell down as well as checking the next value.
so far, i've been trying for, while.. loops but it just keeps looping before i had a chance to enter the next value to compare.
so is there a way i can code it to make the loop pause, and then cont after i enter the value and comparing it?
something like this
Option Explicit
Public lngRowNumber As Long
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TESTING lngRowNumber
lngRowNumber = lngRowNumber + 1
End Sub
Private Sub UserForm_Initialize()
lngRowNumber = 1
End Sub
Private Sub TESTING(ByVal lngRowNumber As Long)
MsgBox "Updated Row : " & lngRowNumber
End Sub
I'm wondering how to reuse the VBA code that I have to create multiple Calendar date pickers on the same worksheet that will input to different cells. I have tried changing the name of the .frm file and re-importing it to simply change the cell reference output, but Excel rejects this every time and says the name is already in use. I'm closely following this example.
Bonus points if you know code for how to hide the clickable image until the cell with the date is selected.
Here is the code that I am working with so far in frmCalendar:
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
On Error Resume Next
Dim cell As Object
For Each cell In Range("C18")
cell.Value = DateClicked
Next cell
Unload Me
End Sub
Private Sub UserForm_Initialize()
If IsDate(ActiveCell.Value) Then
Me.MonthView1.Value = Range("C18")
End If
End Sub
...and my code for Module1:
Sub Sample()
frmCalendar.Show
End Sub
So basically, I have one calendar that unloads into C18. However, I would like to have up to 10 calendar buttons in my worksheet that have different output cells so they can all have different dates.
Here is an example where the calendar button outputs to C18. It has been assigned the Macro "Sample"
So how can I reuse my code for multiple calendar buttons? Bonus if there is code to hide the calendar buttons until the cell is selected.
You can store the selected cell when the form opens and use that when you close it.
Dim cell As Range 'selected cell when launched
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
On Error Resume Next
cell.Value = DateClicked
Unload Me
End Sub
Private Sub UserForm_Initialize()
Set cell = Selection.Cells(1)
If IsDate(cell.Value) Then
Me.MonthView1.Value = cell.Value
End If
End Sub
Purpose: Click on a cell in a range (Range: Column K:K on excel worksheet). Once you click on a specific cell in column K, userform pops up with cell value using following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("K:K")) Is Nothing Then
Credit_Information.TextBox1.Value = Target.Value
Credit_Information.Show
End If
End Sub
My question, is depending on where I click on column K, I want to use two buttons on my userform (Previous and Next) to move up and down column K and see the values of the cell dynamically change on my userform. Is this possible? Please let me know if any clarification is needed.
Just add the two command buttons to your userform.
Name one of the buttons cmdNext and give it a caption of "Next".
Name the other button cmdPrev and give it a caption of "Previous".
Then, in the userform code module, place these routines:
Private Sub cmdNext_Click()
ActiveCell(2).Select
End Sub
Private Sub cmdPrev_Click()
If ActiveCell.Row > 1 Then ActiveCell(0).Select
End Sub
That's it.
Note: if you want you can add code to ensure that the ActiveCell is in column K before allowing the new selections:
If ActiveCell.Column = 11 Then ...
Perfect, Thanks!
I also found out that using Offset worked for me too in this manner. I'm not sure however if I'm breaking any conventions by doing this.
Private Sub CommandButton1_Click()
ActiveCell.Offset(-1).Activate
End Sub
Private Sub CommandButton2_Click()
ActiveCell.Offset(1).Activate
End Sub
It is possible, but I would create another procedure for that. What you could do is declare a public variable in your userform & set it equal to the range Target. Then you could call another procedure from the userform on each button click and redefine the selected range after each click.
So, at the top of your userform do this:
Public selected_cell as Range
Then for the up button:
Private Sub ButtonUp.Click()
If selected_cell.Row < 2 Then Exit Sub
selected_cell.Rows(0).Select
Set selected_cell = selected_cell.Rows(0)
me.TextBox1.Value = selected_cell
End Sub
And the down button would be:
Private Sub ButtonDown.Click()
selected_cell.Rows(2).Select
Set selected_cell = selected_cell.Rows(2)
me.TextBox1.Value = selected_cell
End Sub
Now let's make your code like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("K:K")) Is Nothing Then
With Credit_Information
Set .selected_cell = target
.TextBox1.Value = Target.Value
.Show
End With
End If
End Sub