I'm trying to open UserForms based on the values of cells in one row of a sheet. There are 17 UserForms so I don't want to have to use 17 if statements for each form like this:
If ActiveCell.Value = 1 Then
UserForm1.Show
End If
Is there a way that I can use a variable to show the forms?
I was thinking something along the lines of:
Dim i
Do
If ActiveCell.Value = "" Then
Exit DO
End If
i = ActiveCell.Value
UserForms("UserForm" & i).Show ****THIS is what doesn't work
ActiveCell.Offset(0,1).Select
Loop
Paste the code from the link Harvey provided, then adjust this line in your code:
UserForms("UserForm" & i).Show ****THIS is what doesn't work
to:
ShowAnyForm ("UserForm" & i)
That's a great link Harvey, I've bookmarked it!
You can use the often overlooked VBA.UserForms object. See this link which fully descibes what you need to do.
There's no point in my explaining it here.
Harvey
I've never come accross the method mentioned by #Harvey (I like it, though) so would have used some sort of Select Case statement:
Select Case .Cells(1,1).Value
Case 1:
FormOne.Show
Case 2:
FormTwo.Show
' And so on and do forth...
Case Else:
MsgBox ("Invalid entry")
End Select
Simpler than 17 If statements, at least.
The easiest way is this one:
Dim activeuf as Object
Set activeuf = UserForm & i
activeuf.show
Perhaps it will not work for the person who asked, but I'm sure it will help people who check this question in the future
Get UserForm object defined by its string name
Function Form(Name As String) As Object
Set Form = CallByName(UserForms, "Add", VbMethod, Name)
End Function
Sub Test()
Dim strFormName As String
strFormName = "UserForm1" ' <-- replace by your lookup code instead
Form(strFormName).Show
End Sub
Here a "faster" code to open the form (Rao-Haribabu evolution):
Dim forMy
Set forMy = CallByName(UserForms, "Add", VbMethod, formName) ' formName is the form name to open
forMy.Show
Related
I have a combobox with list of page size in excel. i want to change page sizes with combobox selection change.
Following is not working
Public Sub UpdateSize()
Dim Papersizetext As String
Papersizetext = "xlPaper" & Worksheets("Static").Range("B7").value 'A4 is the value in cell B7
shgenerate.PageSetup.PaperSize = Papersizetext 'not working
shgenerate.PageSetup.PaperSize = "xlPaper" & Combobox1.value 'this also not working
shgenerate.PageSetup.PaperSize = xlPaperA4 'is working - i want above to work.
'shgenerate is sheet name
End sub
You could whip up your own function to parse the input and return the correct enum member:
Private Function PaperSize(ByVal rawSize As String) As Long
Select Case rawSize
Case "A4"
PaperSize = xlPaperA4
Case "A3"
PaperSize = xlPaperA3
Case "A5"
PaperSize = xlPaperA5
Case Else
PaperSize = xlPaperUser ' or some other default
End Select
End Function
For more complex paper sizes, such as "Letter 8.5"x11" 22x28cm" or "Legal 8.5"x14" 22x36cm", you could possibly use InStr to test of the existence of "Letter" and "Legal" to return xlPaperLetter and xlPaperLegal.
Regex might be another approach to parse out the dimensions.
Without more detail though, it's hard to give a satisfactory answer, as this question is actually quite a broad topic.
I have a UserForm which lets the user input a count of product defects into a textbox. This is done as part of monthly reporting, so I have option buttons to select the Month (12 options). There are also option buttons for selecting Product Type. The code basically evaluates what options are selected and copies the textbox values (defect counts) into specific cells in another spreadsheet (for reporting purposes). Not all TextBoxes are required to have values entered by the User.
You can check out a screenshot of the UserForm https://imgur.com/a/6QefjCp.
As you can see from the code, I'm using a bunch of IF statements to perform the decision making - I would like to reduce the length of this code, but I don't know where to start.
I have never really used VBA prior to this, so haven't really attempted a solution. In its current state, the code works flawlessly. Just looking to reduce and clean-up.
Private Sub OKButton_Click() 'This is the button the user clicks to finalize
'the data entry
'Calling the Product type modules
Call Product1Module
Call Product2Module
Call Product3Module
End Sub
Sub Product1Module() 'All product modules will look almost exactly like this
'except the cell ranges will be different
If UserForm.Product1Button.Value = True Then 'Checking for Product1 Option button
If UserForm.JANButton.Value = True Then
'Record value to textbox if JAN is selected
Sheets("Sheet1").Range("B1107").Value = UserForm.TextBox1.Value
Sheets("Sheet1").Range("B1115").Value = UserForm.TextBox2.Value
Sheets("Sheet1").Range("B1108").Value = UserForm.TextBox3.Value
Sheets("Sheet1").Range("B1116").Value = UserForm.TextBox4.Value
Sheets("Sheet1").Range("B1109").Value = UserForm.TextBox5.Value
Sheets("Sheet1").Range("B1117").Value = UserForm.TextBox6.Value
Sheets("Sheet1").Range("B1111").Value = UserForm.TextBox7.Value
ElseIf UserForm.FEBButton.Value = True Then
Sheets("Sheet1").Range("C1107").Value = UserForm.TextBox1.Value
Sheets("Sheet1").Range("C1115").Value = UserForm.TextBox2.Value
Sheets("Sheet1").Range("C1108").Value = UserForm.TextBox3.Value
Sheets("Sheet1").Range("C1116").Value = UserForm.TextBox4.Value
Sheets("Sheet1").Range("C1109").Value = UserForm.TextBox5.Value
Sheets("Sheet1").Range("C1117").Value = UserForm.TextBox6.Value
Sheets("Sheet1").Range("C1111").Value = UserForm.TextBox7.Value
...
End If
End If
End Sub
Give each of your option buttons a Tag property value - e.g. make JANButton.Tag be "B", then make FeBButton.Tag be "C", etc.
Then you can do this:
Dim targetColumn As String
Select Case True
Case UserForm.JANButton
targetColumn = UserForm.JANButton.Tag
Case UserForm.FEBButton
targetColumn = UserForm.FEBButton.Tag
'...
End Select
With Worksheets("Sheet1") '<~ which workbook is that in? whatever is active?
.Range(targetColumn & "1107").Value = UserForm.TextBox1.Value
.Range(targetColumn & "1115").Value = UserForm.TextBox2.Value
'...
End With
I'm analyzing survey responses and just want to recode them from the assigned numerical value back to their original meanings using a command button (is this even the most efficient format?). I'd like to escape the tedium of lots of clicking and "Find/Replace". Here's a sample of what I'm working with:
Sub Button1_Click()
Dim response As Integer, result As String
response = ActiveSheet.Range("A3:A58").Value
If response = 1 Then result = "Individual Public School"
...
If response = 8 Then result = "Museum (or other science-rich institution)"
ActiveSheet.Range("B3:B58").Value = result
End Sub
Excel is stopping me at the third line, prompting me with a debug option. Where am I going wrong? Is there a more elegant solution for this?
I'm on a Mac computer, and I'm using Microsoft Office 2010 version, if this helps at all.
Any and all tips are welcome! Thanks.
You need something like:
Sub Button1_Click()
For Each r In Range("A3:A58")
If r.Value = 1 Then r.Offset(0, 1) = "Individual Public School"
'...
If r.Value = 8 Then r.Offset(0, 1) = "Museum (or other science-rich institution)"
Next r
End Sub
I used Data Validation to create some drop down lists. Anyone know how to use VBA to select the first iterm in a drop down list?
I tried 'Split' function:
cell.Value = Split(cell.Validation.Formula1, ",")(0)
but it did not work well, it will only work if I put like "option1, option2" in the source in Data Validation window. If I refer the source to a range of options, then it will return with errors.
I guess there should some smarter ways.
Thanks!
Sub test()
Dim adr As String
With Range("c4")
adr = Mid(.Validation.Formula1, 2)
Debug.Print Range(adr).Cells(1, 1)
End With
End Sub
so your answer is:
set c = range("c4")
c.Value = Range(Mid(c.Validation.Formula1, 2)).Cells(1, 1).Value
Im 'trying' to make a message box that display how many present and absents there are in a column so the user can either click ok and the data being copied or pressing cancel and ending the code.
The problem is the i cant seem to get the CountIf part to work where it counts the number of absents and presents before displaying them in the Message Box.
Im Pretty new to coding so its probably a real mess but any help and id be grateful:)
Sub SubmitAttendance()
Dim Response As String
Dim Question As String
Dim PresentNumber As String
Dim AbsentNumber As String
Function As Integer
PresentNumber = Countif(Range("E:E"), Present)
AbsentNumber = Countif(Range("E:E"), Absent)
End Function
Question = "PresentNumber Present and AbsentNumber Absent"
Response = MsgBox(Question, vbOKCancel, "Register Totals")
If Response = vbOK Then
Range("E:E").Select
Selection.Copy
Range("F:ZZ").Find("").Select
Selection.PasteSpecial
Else
Exit Function
End If
End Sub
Excel functions are available in WorkSheetFunctions module, like:
WorksheetFunctions.CountIf(Arg1 As Range, Arg2)
Also, pass your second argument as string, not as a variable (VB's hectic nature will declare an uninitialized variable of that name for you, which is probably not what you want.
So your CountIf line should be like:
WorksheetFunction.CountIf( Range("E:E") , "Present")