I am trying to get a User form to appear if a cell in my spread sheet states either "2SQ" or "2SO" - both require the same User form. There is one text field on the User Form for the user to either input a number for example "0001" or a word such as "Various". Once the User enters in the data, there is an "OK" Command Button (CommandButton1) to press. I then want the data they put in the User form to go into another cell of the spread sheet.
I have already created the User form (UserForm1) however cannot get it to appear when require and obviously, I can't get it to transfer the data entered into another cell.
I would love to give you examples of what I have already tried however they are just copy and pastes from websites to try and make it happen with no luck
Try some VBA likme this in the Worksheet code:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.cells(1, 1).Value
Case "2SQ", "2SO"
UserForm1.Show Modal:=True
Sheet.CellWhereYouWantItToGo.Value = UserForm1.TextBox1.Text
End Select
End Sub
The Modal part means the code will wait until the form is closed (i.e. in your ComandButton1 click event you must Me.Hide)
Related
I want to call/ activate a button at the end of a Sub. I know the cell address of the command button, but I don't know the name/ID of the button.
How do I select/activate the button without looping through each button on active sheet?
https://stackoverflow.com/a/30600479/13049793
I have created my buttons on multiple rows with each assigned to the same macro, from my understanding, I cannot call the macro the button is assigned to because the macro uses the button's relative position, below is a simple example to illustrate the use of the buttons relative position:
Sub ExampleButtonClick()
Dim Cellvalue As String
Cellvalue = ActiveSheet.Buttons(Application.Caller).TopLeftCell.Offset(0, -1).Value
Msgbox (Cellvalue)
End Sub
i assume you want to run one macro that at the end initiates a different macro, i also assume the reason the button is rather not as a sub that is just called is because it has its independent function that can be used without the other sub
assuming that you used a command button as an activeX control, why not just use a private sub that the button performs and place that in an individual module and at the end of your current sub;
call module1 'assuming the private sub exists in module 1
or rather include the code even if duplicated in the first sub, searching for a button seems the circular route unless there is another intention you wish to pursue.
maybe elaborate on the task at hand or post the current code you are working on?
I'm creating a system that allows an individual to select module choices for their degree.
The users have to navigate between multiple forms (e.g. semester 1 & 2, user info, confirmation pages etc.). I want the users to be able to return to a previous form to make changes after progressing (i.e. return to semester 1 choices after moving onto semester 2 choices) and be able to still edit all the data they inputted into the first form.
I have tried using the hide and show methods but I keep getting an error (Run Time error '400' - Form already displayed, cannot show modally)
'on the AM1 form
AM2.Show
AM1.Hide
'(first form I want to close)
'on the AM2 form
Unload me
AM1.Show
'(I want to return to the first form and close the second)
I want to be hide the first form (AM1) and keep all the info available to be re-edited when successfully returning to it.
Try hiding AM1 first before showing AM2 and that should fix your issue.
Private Sub CommandButton1_Click()
UserForm1.Hide
UserForm2.Show
End Sub
Also I think using the multipage control is probably better than using multiple userforms.
I'm sorry if this question is vague or answer exists, but struggling to find what I'm looking for.
I have created a sheet (VBA generated) where the user is required to fill in certain cells in a table. As multiple users are going to be editing the document, I want to automate a 'flow chart' type answer to standardize the document.
I.e. if in the first cell the user input is "No", then the remaining cells in the table default to "N/A". If the input is "Yes", then the user moves on to filling out the next box in the table. I was also going to use a drop down list to ensure the correct user input.
Does anyone have any suggestions of what I can research for a method to achieve this? Was thinking of having the cell formula, but this will delete after user input so wanted a method where the formula would remain even if the user input is deleted.
Thanks for any help.
The answer would be to use the worksheet_change event. However your problem is you are creating the sheet programatically. If you are using a template for your sheet you can just attach the code to the template, other wise you will have to create the code in your workbook and use the Workbook_sheetchange event
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Name = "sheet1" Then
If Target.Address = "$A$5" Then 'I'm assuming cell A5 is your first cell, change as required
If UCase(Sh.Range("a5")) = "NO" Then
Sh.Range("a10") = "#N/A" 'repeat for all cells you want to be NA
End If
End If
End If
End Sub
My macro is going to compare a sheet with another sheet. This second sheet needs the user to paste data in there. (Note: The data being copied is not in Excel).
One way is to run the macro, and end it by prompting the user to paste the data in, then run "Macro2". However, I'd like to keep it all in one macro, so have found a way to wait for user input before continuing. This seems to work for me, so my main question is:
How stable is doing it this way?
...macro stuff above here
MsgBox ("[Please copy the data into the new sheet, after clicking 'OK']")
Do While WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1
DoEvents
Loop
...then after the user pastes info, continue on, using the data that's been pasted.
The idea is that DoEvents just runs and runs while my sheet is blank. Right after the user pastes the data into the newWS, the macro continues on (since it will see data in column 7)...
Is this an okay method, or is it a bad idea to use like that? I've never really used DoEvents, so don't know if it's doing something in the background that could cause issues.
Edit: The data is in Lotus Notes, which I can export to Excel. However, that takes a few more steps (and I'd rather not create some new temporary excel files), so copy/pasting is my preferred method. This question is half practical, and half theoretical. Sorry for any confusion!
Probably not the best idea. Instead, allow them to select the data and perform the copy, all through VBA:
MsgBox ("[Please select data to copy into the new sheet, then press 'OK']")
newWs.Cells(1,1).PasteSpecial '### Modify to your specific location to paste the data
'Here you can add logic to validate that they have pasted enough data,
' and use control statement to prompt them to paste more data, etc.,
' if necessary, or exit sub early
'For example:
If WorksheetFunction.CountA(newWS.Cells(1, 7)) < 1 Then
MsgBox "Try again!"
Exit Sub
End If
Alternatively, you can use a DataObject:
Dim dataObj As New MSForms.DataObject
dataObj.GetFromClipboard
newWs.Cells(1,7).Value = dataObj.GetText
You could restructure the code so that it lives inside of a userform with ShowModal set to false (in the properties). Code prior to when you want the user to gather data can be put in the useform's initialize event. Then the userfrom shows (with a simple label caption and an okay button). Since it is modeless the user can copy data from an external program and paste it in. Then the rest of the code runs after the user hits okay. The form itself can be hidden during this phase. As proof of concept I created the following form:
with the following code:
Private Sub CommandButton1_Click()
Me.Hide
MsgBox Range("A1").Value
Unload Me
End Sub
Private Sub UserForm_Initialize()
'macro code can go here
'it runs before the form shows
'e.g.
MsgBox "Initializing"
End Sub
I launch the form on a blank sheet. First a message box appears before the code (confirming that code can run while the form is being initialized but before it is visible) then the form shows:
I go to an open instance of Notepad which contain a sentence and, while the form is still open -- paste it into A1:
Finally I press okay and the userform then hides itself but continues to run code (which now has access to the copied data):
Remember to unload the form at the end.
Background Details
I have an excel spreadsheet with Activex dropdown (combobox) objects which help the user to know what options are available. I did this because the data validation list dropdowns are way too small in font size, and were gathering a lot of complaints.
So my solution was to add combobox objects which allow the user to select from a range of options. However, I have to link the comboboxes to a cell with the linkedcell property, so that both the user and various formulas can see what has been chosen. I also set up the combobox to disappear when it's not in use (much in the same way as the data validation dropdown button only appears when you select the relevant cell).
Here is the problem:
I don't want the users to edit the value in the linked cell, so I make sure the linked cell is locked whenever the combobox is not selected:
Private Sub comboBox1_GotFocus()
Call unlockComboBoxTargetCell(comboBox1)
End Sub
the procedure above does this:
If (targetComboBox.LinkedCell <> "") Then
Dim targetCell As Variant
Set targetCell = Range(targetComboBox.LinkedCell)
If Not targetCell Is Nothing And targetCell.Locked <> False Then
unlockSheet (activesheet.Name)
targetCell.MergeArea.Locked = False
lockSheet (activesheet.Name)
End If
End If
Equivalent procedures exist to lock the target cell.
However, whenever you do a "Save As" action on the workbook, it seems that the linked and locked cells create a problem: Excel gives this error out of the blue:
"The cell or chart you are trying to change is protected and therefore read-only..."
This error comes up about twice or three times for each cell that is locked and is the linkedcell for a combobox.
Is there a good way to overcome this problem? Right now my best solution is to leave the cells unlocked and place data validation on the cell, so that if the user edits the cell they will at least be refused when they type something invalid. I could make sure that the combobox covers up the linked cell whenever it is selected, but sometimes that means having a very large, annoying combo box with a very tiny dropdown button on its right side.
Perhaps I am being a bit too particular about the user interface?
Thanks in advance for reading this long and involved post.
In the "lockSheet" procedure you have created, the code to 'protect' the worksheet needs an additional parameter, UserInterfaceOnly, set to true.
I imagine the LockSheet sub is something like this;
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect
end sub
Try this:
sub lockSheet(strSheetName as string)
thisworkbook.sheets(strSheetName).Protect, UserInterfaceOnly=True
end sub
UserInterfaceOnly allows programmatic changes to the protected sheet.
Bill