Trying to use excel vba userforms to automate many tasks in a spreadsheet. There is a Button1 on Sheet1 that has two commands.
sub Button1_click()
sheet1.range("a3").select
userform1.show
end sub
As an example there is a data entry worksheet (sheet2) that we want to switch to to input data values to a list.
To simplify in this example and to show my issue the userform has one button
sub CommandButton1_click()
userform1.hide ' hide the form
sheet2.activate
sheet2.range("b2").select
end sub
What I want to be able to do is use the user form button to switch to sheet2, select b2, and be able to enter data starting there immediately.
What I've been getting is a selection box on sheet2.range("b2") BUT I show color starting at sheet1!a3, then sheet1!b4,... I have entry occurring on sheet1!b2 etc.
Shows beginning and entry
Entry colors from sheet1 showing on visible sheet2 - data not appearing
Actual data is entered on sheet1 not sheet2 -
The problem seems to be (as you mention in the comments) that, at the end of Button1_Click, the focus returns to the sheet containing the button even though the ActiveSheet is now a different sheet.
For the moment (until someone comes up with a better solution) a "workaround" is to allow the Button1_Click event to finish running before showing the Form. That can be achieved by changing Button1_Click to something like:
Sub Button1_click()
Sheet1.Range("a3").Select
Application.OnTime Now(), "BypassBug"
End Sub
Sub BypassBug()
UserForm1.Show
End Sub
Related
I have a small issue, but I cannot find the code for it. For simplicity I made a simple example.
I made a validation userform with a modeless listbox (lsb_dataval_man). This listbox is populated with named ranges with only the names that have an error or are empty. Because there are many tabs, the user can navigate quickly to the sheets and cells which need attention and modify or populate them.
When you doubleclick a named range in the listbox, it will go to that reference and users need to enter data directly. See the simplified code for this double click below.
With the command Application.Goto ThisWorkbook.names(named_range).RefersToRange it will select the cell, but I am not able to type in the cell directly as like when you would do a select. A user needs to click the cell again or click in the formula bar and enter data. How can I change my code so after going to the cell of the named range, the user can type directly? Perhaps this is related to the modeless form which is still active?
The code:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
'select the named range cell
Application.Goto ThisWorkbook.names(named_range).RefersToRange
End sub
This should work:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
AppActivate ThisWorkbook.Windows(1).Caption 'switches focus from userform to workbook
ThisWorkbook.Names(named_range).RefersToRange.Select 'selects cell
End sub
I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.
To simplify use of a spreadsheet and avoid anyone just adding or deleting rows as they wish (and therefore messing up the formulae) I have used a series of userforms. The first Userform gives the user the option to either 'Cancel', 'Delete Row', or 'Insert Row'. Selecting 'Cancel' unloads the form, and selecting 'Delete Row' unloads the original form and shows another Userform - this allows the user to select the row number they want to delete, and pressing OK deletes it. This all works fine.
The issue I have is with the 'Insert Row' button - this should unload the first Userform and show another userform. This new 'Insert Row' Userform allows the user to input the row number after which the new row is to be inserted. The user has the option to input further detail in various textboxes and comboboxes which will populate the new empty row - these include start and end dates.
I was having integer vs string issues with the option for manual input of the dates, so I opted to go with another 2 userforms, opened from the start and end dates which gave a calendar (select the date you want, press OK and the date is inserted in the relevant textbox in the 'Insert Row' userform). I tested these all the way through to iron out the wrinkles, and everything seemed to be working.
I then opened the first userform from the command button on the spreadsheet,
Selected 'Cancel' and 'Delete Row' to check they still worked, then selected 'Insert Row' and got 'Runt-time error '1004' Application-defined or object-defined error'. When I went to debug, it highlights the line: userform1.show
The only thing I can think of that could be a problem (and the only significant change I've made) are the two calendars, which reference userform1. I've carried out multiple searches, but haven't found anything which even looks like my problem - I just don't know what the issues are with having multi-layered userforms. I've included some of the code by way of further explanation:
Private Sub InsertRowButton_Click()
'unload the first userform when the user selects "Insert Row"
Unload ModifyProjectUserForm1
''This line highlights as the problem
UserForm1.Show
End Sub
'''the following subs are embedded in "UserForm1" (the one to Insert Row), allowing the user to select a date:
Private Sub InsertRowNumberStartDateCommandButton_Click()
frmCalendar1.Show
End Sub
Private Sub InsertRowNumberEndDateCommandButton_Click()
frmCalendar2.Show
End Sub
'''The following are the subs used to take the dates and place them in "UserForm1":
Private Sub StartDateCalendarOK_Click()
On Error Resume Next
UserForm1.InsertRowNumberStartDateTextBox.Value = frmCalendar1.Calendar1.Value
Unload frmCalendar1
End Sub
Private Sub EndDateCalendarOK_Click()
On Error Resume Next
UserForm1.InsertRowNumberEndDateTextBox.Value = frmCalendar2.Calendar2.Value
Unload frmCalendar2
End Sub
I expected the 'Insert Row' userform to open, allowing me to put a new row in, with associated data, and dates. What I got was a runtime error, and I'm at a complete loss.
I don't know if I'm going down completely the wrong route, but I'm loathe to post my entire code, because there's a lot of it!
Can anyone help?
I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub
I have this code which fills a combobox on Sheet1 with the Name column of Table1 on Sheet2.
Public Sub Worksheet_Activate()
Me.ComboBox1.List = Worksheets("Sheet2").ListObjects("Table1")_
.ListColumns("Name").DataBodyRange.Value
End Sub
Works fine but it has a weird effect when I click off the combobox onto the sheet. The selected entry in the box quickly flashes to the previous entry. For example, the currently selected item is "b" and then I select "c". If I click on the worksheet the entry in the box quickly flashes to "b" before going back to "c".
I've put this code alone in a new file and I still get the same effect. Has anyone else seen this?
Edit regarding reason for Public Sub:
Forgot to include the Workbook_Open code so that Sheet1 is considered Activated when you open the Workbook. But it doesn't matter if I keep that code or not, I still see the effect.
Private Sub Workbook_Open()
Call ActiveSheet.Worksheet_Activate
End Sub
Adding a LostFocus event with code that selects a cell on your worksheet should cause the flicker not to happen when you select a cell after changing the ComboBox's value.
Like the following:
Private Sub ComboBox1_LostFocus()
ActiveSheet.Range("A1").select
End Sub