I don't know how to take action with the VBA Userform here.
I have a userform which allows users add more textboxes and type the value to the textboxes. I want to copy those values from each additional textbox to separate cells. This is what I did, but not working.
Label "Add textbox after clicking_THIS ONE IS WORKING"
Private Sub CommandButton2_Click()
x = Me.Controls.count + 1
Set xx = Controls.Add("Forms.TextBox.1", "CtrlName" & x)
xx.Top = x * 20 - 108
xx.Left = 396
xx.Width = 288
End Sub
Label "Get value from textboxes to cell_THIS IS NOT WORKING"
Private Sub CommandButton1_Click()
Dim count as Integer
Dim i as Integer
count = Me.Controls.count - 9 ("I have 9 other controls, so need to - 9 to count the number of textboxes")
For i = 1 To count
Cells(i, 1).Select
ActiveCell.Value = Me.Controls("TextBox" & i).Value
Next i
End Sub
The problem is the Me.Controls ("TextBox" & I).Value. I tried different funtions here, but still couldn't solve that.
You have named your textboxes CtrlName10, CtrlName11, etc. You must use those names, not TextBox1 etc. Which means that the value is Me.Controls("CtrlName" & (i + 9)).Value.
Hint: In error box saying Could not find the specified object press the button Debug. This will return you to the code editor, with the offending statement highlighted, and you can examine the variables in the Locals pane (on the View menu).
Related
I am new to Excel VBA. I wanted a code for searching for a word in a listbox that has data drawn from a worksheet. I came across a video on YouTube (Link: https://youtu.be/IuOYQASY3p4), which describes a code for running a search in a Listbox for a keyword typed into a Textbox on the same Userform. The code seemed perfect for my project. But I found it works perfectly for a data set of 6 columns, but when I change the code and corressponding control properties for a data set of 11 columns, the code throws up Run-Time Errors 380 and 381 in the two For... Next loops in the code.
I am attaching an Excel file I prepared you experts here to review the code I am using. I would highly appreciate it if you would tell me where I am going wrong. I have indicated with comments where the errors occur in the code.
I am using Excel 2013 on Windows 10.
My code:
Private Sub TextBox2_AfterUpdate()
With Me.ListBox2
.Clear
For ColHead = 1 To 11
.AddItem
.List(0, ColHead - 1) = Sheet2.Cells(1, ColHead).Value '<<< 'Run-Time Error 380, Invalid Property Value
Next ColHead
' If I comment out the For... Next loop above the code runs up to the next For... Next loop below _
and there it throws up run-time error 381. I have marked it there with a comment.
ListRow = 1
If IsDate(Me.TextBox2) Then
FindVal = CDate(Me.TextBox2)
ElseIf IsNumeric(Me.TextBox2) Then FindVal = Val(Me.TextBox2)
Else
FindVal = "*" & Me.TextBox2 & "*"
End If
LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
For ShRow = 2 To LastRow
FindRow = Application.WorksheetFunction.CountIf(Sheet2.Rows(ShRow).EntireRow, FindVal)
If FindRow > 0 Then
For ListCol = 1 To 11
.AddItem
.List(ListRow, ListCol - 1) = Sheet2.Cells(ShRow, ListCol).Value '<<< 'Run-Time Error 381, Invalid Property Value
Next ListCol
ListRow = ListRow + 1
End If
Next ShRow
End With
End Sub
Thank you for your help.
There is a limit of 10 columns if you use unbound data sources for a listbox:
From the official documentation:
Setting ColumnCount to 0 displays zero columns, and setting it to -1 displays all the available columns. For an unbound data source, there is a 10-column limit (0 to 9).
Update: There are two ways to fill a listbox:
The first is to assign a Range to the listbox (the property is called RowSouce). With that, you can have more than 10 columns, and you can have a header row. This is called "bound" data source (because the listbox data is bound to a range).
The second is to fill the list manually (as you do), by calling AddItem for every row you want to display. As the data is not bound to anything, this is called "unbound", and for whatever reason there are a max of 10 columns supported (0..9) and you can't have a header row.
The property BoundColumn defines which column is seen as "value". If you define 1 as boundColumn and check the value-property of the listbox (eg in a change event), the value of the first column of the selected row is used. This is true for bound and unbound lists.
I am following a tutorial to populate the textboxes on my userform depending on the number selected from the combobox (the name of the combobox is cmbClientName) but I keep receiving a runtime error '-2147024809(8000700057)' Could not find the specific object.
Could someone help me to solve why this happens and also if i wanted to use string instead of numbers how can i modify. Thanks for the help. The data is on Sheet3(ClientList)
Here is my code below:
Private Sub cmbClientName_Change()
Dim q, p As Long
q = Application.WorksheetFunction.CountA(Sheet3.Range("A:A"))
For p = 1 To 7
Me("Textbox" & p).Value = Application.WorksheetFunction.VLookup(CLng(Me.cmbClientName.Value), _
Sheet3.Range("A" & 2, "H" & q), p + 1, 0)
Next p
End Sub
I will attach photos of my project relevant to the question.
Code
Userform
data
I have a Combobox where as you type, the dropdown list dynamically populates with results that match the input that was typed.
The issue that i am facing is that as you type in the combobox you cannot see what you have typed. in the image below you can see that there is a cell with "Build" - this is the Linked cell i have temporarily set to show whats been typed.
The white box between the above cell and the list is where i am typing. But as you can see there is no text visible.
After clicking elsewhere on the sheet the typed text appears
What i want to happen (and im sure used to) is that while i am typing the text in the combobox should show what is being typed as it is typed.
On the assumption that this is related to the vba code i have set up i have copied that below and the properties of the combobox.
Having searched for hours, i havent come across a relevent post or solution to this issue.
Private Sub OccupationComboBox_Change()
Call OccupationComboBox_Populate
OccupationComboBox.DropDown
End Sub
Private Sub OccupationComboBox_DropButtonClick()
Call OccupationComboBox_Populate
OccupationComboBox.DropDown
End Sub
Private Sub OccupationComboBox_Click()
Call OccupationComboBox_Populate
OccupationComboBox.DropDown
End Sub
Private Sub OccupationComboBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call OccupationComboBox_Populate
OccupationComboBox.DropDown
End Sub
Sub OccupationComboBox_Populate()
Dim arrIn As Variant, arrOut As Variant
Dim i As Long, j As Long
arrOccupation = Sheets("Occupation classes").Range("B2:B2000")
arrOccupationKeys = Sheets("Occupation classes").Range("C2:C2000")
arrOccupationCode = Sheets("Occupation classes").Range("A2:A2000")
ReDim arrCodeOut(1 To UBound(arrOccupation), 1 To 1)
ReDim arrOut(1 To UBound(arrOccupation), 1 To 1)
For i = 1 To UBound(arrOccupationKeys)
If LCase(CStr(arrOccupationKeys(i, 1))) Like "*" & LCase(OccupationComboBox.Text) & "*" Then
'If CStr(arrIn(i, 1)) Like OccupationComboBox.Text & "*" Then
j = j + 1
arrOut(j, 1) = arrOccupation(i, 1)
arrCodeOut(j, 1) = arrOccupationCode(i, 1)
End If
Next
OccupationComboBox.List = arrOut
Range("G18") = arrCodeOut(1, 1)
If Range("D18").Value = "" Then
Range("G18") = ""
End If
ActiveSheet.Calculate
End Sub
As an update, it appears that the same thing happens with a text box. after selecting the text box i start typing but i am unable to see what has been typed until i select another cell in the worksheet . nor in eithercase can a highlight the text in either the combobox or text box
It turns out that there is sometimes a bug relating to multiple monitors.
after changing my pc's projection mode to this screen only everything worked as intended.
If your having this same issue try
dragging the s/s you are working on to your number 1 monitor or
changing your projection mode to this screen only
Thank you very much. I would have never figured out that this issue is related to multiple monitors. By the way. This can be resolved by using the same "scaling" for both monitors (e.g. 125%).
The code returning an error is the first line of the following:
While StrComp(selectedRecipe, dataSheet.Cells(i, 1)) <> 0
recipeRow = recipeRow + 1
i = i + 1
Wend
The debug I'm getting has issues with the While statement line itself. This code is contained under an OK Button click event on a userform, with selectedRecipe defined as a public string variable in the main worksheet sub. "i" is defined as an integer in this private sub. Basically the code is to find which row of the sheet holds the string value contained in selectedRecipe after selectedRecipe is selected from a drop-down combo box (selectedRecipe returns correctly and has no issues associated with it). I assume I need to have some sort of "converting" command in front of "dataSheet.Cells(i,1)" to reinforce the cell value as a string, but am not sure. Thanks!
1) Make sure dataSheet is actually set to a valid sheet. 2) as Comintern said, you need to begin at 1 as Excel is 1 based not zero based. 3) You need to make sure you don't overflow the number of rows:
Public Sub CheckRecipe()
Dim selectedRecipe As String
Dim i As Long
selectedRecipe = "Test"
i = 1
While StrComp(selectedRecipe, ThisWorkbook.ActiveSheet.Cells(i, 1).Value) <> 0 _
And i < ThisWorkbook.ActiveSheet.UsedRange.Rows.Count
i = i + 1
Wend
End Sub
I tried to search this problem but found no similar issue.
I am still newbie in VBA and I'm trying to create macro which chooses range of cells depending on the user's input and then creates an outlined grid out of those selected cells.
I have two ActiveX buttons in my Excel workbook which let the user to input how big the grid is they want to use (Width & Height). I am struggling to include the above mentioned width and height to my code. Here is the code for the buttons (nothing unclear about them):
Private Sub Height_Click()
Dim Height As Integer
Height = InputBox("Syötä ruudukon korkeus", "Ruudukon korkeus", "Syötä tähän")
Range("E5") = Height
End Sub
And width button:
Private Sub Width_Click()
Dim Width As Integer
Width = InputBox("Syötä ruudukon leveys", "Ruudukon leveys", "Syötä tähän")
Range("E2") = Width
End Sub
I want my grid to start from cell "G2" and expand right&down from there and change the size of the selected cells. However the code I have written isn't working at all (as I thought). Here is the code:
Private Sub CreateGrid_Click()
Columns("G:G+E2").Select
Selection.ColumnWidth = 1
Rows("2:2+E5").Select
Selection.RowHeight = 1
End Sub
Cells "E2" and "E5" have the values of width and height printed, respectively. Nothing happens when I click the CreateGrid-button. Any ideas how I can make this code work? Thanks a lot for all answers.
-Teemu
The trick is use the record macro button.
This function will record all instruction that you are doing with the excel book while is recording
Example:
1.- Start the record macro and type a name for your macro.
2.- Select any cell and type a value
3.- select a range of cells that you want
4.- Press the stop macro recording.
5.- Press Alt +F11 and you will see that excel generate a code of what you did
in excel while the macro recording was turned on, even you can know how to type a value inside a cell or select a range of it.
EDIT:
Private Sub CreateGrid_Click()
Range("G2:" & Range("G2").Offset(Range("E5").Value,Range("E2").Value).Addresslocal).Select
End Sub
If this doesn't do what you expect, please let me know and I will try to help correct it.
the command you're looking for is Range().Select but you'll need to create the string that goes into the parenthesis. So if I understand correct you have stored in variables the number of rows and number of columns you want to offset from G2 right ?
In order to get the column letter you can use the function supplied by microsoft here
Function ConvertToLetter(iCol As Integer) As String
Dim iAlpha As Integer
Dim iRemainder As Integer
iAlpha = Int(iCol / 27)
iRemainder = iCol - (iAlpha * 26)
If iAlpha > 0 Then
ConvertToLetter = Chr(iAlpha + 64)
End If
If iRemainder > 0 Then
ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
End If
End Function
but since you're starting from G you'll have to call the function like this:
ConvertToLetter(7+numerColumns)
your final code will look like
Range("G" & 2 + numberLines & ":" & ConvertToLetter(7+numberCols) & numberLines).Select
where numberLines is the number of lines to offset and numberCols the number of columns.
EDIT:
A shorter way, as pointed out by #Kyle, would be to use:
Range("G2").Offset(numberLines,numberCols).Select