Just came back to you… a bit long, but here’s the general thoughts…
This all goes in the code for the ActiveX Control:
Option Explicit
Sub ListBox1_Click()
Dim rw As Range, strtext As String
Dim arr As Variant, ai As Long, aj As Long
Dim brr As Variant, bi As Long, bj As Long
strtext = "a" 'I used this when i did my testing
ReDim arr(11, 0)
For Each rw In Range("rng")
If InStr(LCase(rw.Value), strtext) Then
aj = findaj(arr)
If Not IsEmpty(arr(1, aj)) Then
aj = aj + 1
ReDim Preserve arr(11, aj)
End If
For ai = 1 To 11
arr(ai, aj) = Cells(rw.Row, ai + 1).Value
Next ai
End If
Next rw
ReDim brr(aj, 11)
For bi = 0 To aj
For bj = 1 To 11
brr(bi, bj) = arr(bj, bi)
Next bj
Next bi
ListBox1.ColumnCount = 11
ListBox1.List = brr
End Sub
Private Function findaj(ByVal brr As Variant)
Dim j As Long, meow As String
j = 0
Do While True
On Error GoTo toll
j = j + 1
meow = brr(1, j)
Loop
toll:
findaj = j - 1
End Function
So there’s a lot going on here… I use two separate arrays, due to how redimming arrays works in VBA. You can only update the second element of the array, so arr(ai,aj)
can only have aj
updated when I redim preserve
while adding a new row to my array.
So we make an array (arr
) that captures the data based on VBA’s limitations. Within that array, we use a function, findaj
, which intentionally traps an error to determine the appropriate last column in arr
(i italicized the use of column, as it’s not truly the case, but it makes sense spatially when thinking about it).
You then convert the array arr
to brr
in the appropriate order of columns/rows.
Afterwards, you make your .list = brr
.
- Remove From My Forums
-
Question
-
I have a workbook with a user form containing a lot of different text boxes, combo boxes and check boxes all linked to cells in a worksheet with the ControlSource property.
TextBox1.ControlSource = Sheet1.Range("A1").Address
At first when the workbook is opened and the the user form is opened everything works well and all the controls are linked correctly to the cells in the worksheet. The problem arise when the workbook has been open for a while and you
try to open the user form and then it won’t work anymore and the following error message is given:Run-time error '380': Could not set the ControlSource property. Invalid property value.
So the problem is that the error doesn’t occur all the time the user form is opened and initiated it occurs after a while, and when the error has occurred the workbook has to be closed and reopened for the user form to work again. So as far as I can
see the ControlSource property is not given an invalid property value.Does anyone recognize this error and know how to fix it?
Since it doesn’t occur all the time I had a thought that it might be a bug in Excel, could it be like that?
Very grateful for
answers,Mårten Fredriksson
Answers
-
Please download the sample file from this link
http://wikisend.com/download/513014/Sample.xls
This sample file uses absolutely a different approach which I have also covered in this link.
Topic: VBA – Control Arrays
Link:
http://siddharthrout.wordpress.com/2011/08/05/vba-control-arrays/Code Used
In Class Module
Public WithEvents TextBoxEvents As MSForms.TextBox Public WithEvents CheckBoxEvents As MSForms.CheckBox Public TBIndex As Long, CBIndex As Long Private Sub TextBoxEvents_Change() Sheets("Sheet1").Range("A" & TBIndex).Value = TextBoxEvents.Text End Sub Private Sub CheckBoxEvents_Click() Sheets("Sheet1").Range("B" & CBIndex).Value = CheckBoxEvents.Value End Sub
In Userform
Option Explicit Dim Text1Array() As New Class1 Dim Text2Array() As New Class1 Private Sub UserForm_Initialize() Dim i As Long, j As Long, TBCtl As Control For Each TBCtl In Me.Controls If TypeOf TBCtl Is MSForms.TextBox Then i = i + 1 ReDim Preserve Text1Array(1 To i) Set Text1Array(i).TextBoxEvents = TBCtl Text1Array(i).TBIndex = i End If If TypeOf TBCtl Is MSForms.CheckBox Then j = j + 1 ReDim Preserve Text2Array(1 To j) Set Text2Array(j).CheckBoxEvents = TBCtl Text2Array(j).CBIndex = j End If Next TBCtl Set TBCtl = Nothing End Sub
Sid (A good exercise for the Heart is to bend down and help another up)
Please do not email me your questions. I do not answer questions by email unless I get paid for it If you want, create a thread in Excel forum and email me the link and I will help you if I can.
-
Marked as answer by
Wednesday, August 24, 2011 2:50 AM
-
Marked as answer by
Помогаю со студенческими работами здесь
Ошибка: Run-time error ‘5’
Доброго времени суток!
Совсем недавно занялась изучением VBA и столкнулась с проблемой.
Имеется…
Ошибка Run-time error 1004
Добрый день!
Вылетает ошибка после строки:
Set y = Workbooks.Open("K:Bond Prod.MXMXmx.xlsm",…
Ошибка run-time error 1004
Sub pract()
korp = Val(InputBox("Введите номер столбца, где находятся адреса: ", "Столбец", 5))…
Ошибка 424 run-time error
Добрый день, написала макрос, все работало, потом открыла этот файл на компьютере с англ. версией…
Искать еще темы с ответами
Или воспользуйтесь поиском по форуму:
3
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
- Pick a username
- Email Address
- Password
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
bdautrich
New Member
- Joined
- Sep 22, 2022
- Messages
- 19
- Office Version
-
- 2013
- Platform
-
- Windows
-
#1
Working on Excel VBA app. And I am getting an error I can not figure out.
The app is displaying a range from a spreadsheet in a list box on a user form, The first time it does this, things work fine. The user form has some text input boxes that allows the user to input a new row of data unto the spreadsheet. After the user clicks on the save button the VBA code calls the subroutine to reset the list box and the second time through, I get the 380 error. I put in a msgBox to see that the only parameter which is the row number of the last row is valid and it is so I do not have an idea what is happening. The strange this I have nearly identical VBD code that works the same way with a different spreadsheet, and I am not experiencing the error there.
Here is the code that
MsgBox («In reset and iRow = » & iRow)
If iRow > 1 Then
.lstJoints.RowSource = «List1!A2:I» & iRow ‘ this is the line that generates the error
Else
.lstJoints.RowSource = «List1!A1:I1»
End If
Any help would greatly be appreciated.
Excel Facts
Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
RoryA
MrExcel MVP, Moderator
- Joined
- May 2, 2008
- Messages
- 40,684
- Office Version
-
- 365
- 2016
- Platform
-
- Windows
- MacOS
-
#2
Are there any error values on the worksheet?
bdautrich
New Member
- Joined
- Sep 22, 2022
- Messages
- 19
- Office Version
-
- 2013
- Platform
-
- Windows
-
#3
Are there any error values on the worksheet?
Not that I know of
RoryA
MrExcel MVP, Moderator
- Joined
- May 2, 2008
- Messages
- 40,684
- Office Version
-
- 365
- 2016
- Platform
-
- Windows
- MacOS
-
#4
Are you sure iRow is valid when the error occurs? How is it declared and what is its actual value?
bdautrich
New Member
- Joined
- Sep 22, 2022
- Messages
- 19
- Office Version
-
- 2013
- Platform
-
- Windows
-
#5
I am printing out iRow and it is a valid value and it is declared as integer.
Are you sure iRow is valid when the error occurs? How is it declared and what is its actual value?
bdautrich
New Member
- Joined
- Sep 22, 2022
- Messages
- 19
- Office Version
-
- 2013
- Platform
-
- Windows
RoryA
MrExcel MVP, Moderator
- Joined
- May 2, 2008
- Messages
- 40,684
- Office Version
-
- 365
- 2016
- Platform
-
- Windows
- MacOS
-
#7
I can’t replicate your error if the address is valid. If the sheet name is wrong, or the row number is missing, then you would get that error. Does the Else part of the If code work if you make iRow
equal 1?
bdautrich
New Member
- Joined
- Sep 22, 2022
- Messages
- 19
- Office Version
-
- 2013
- Platform
-
- Windows
-
#8
I set IRow to 1 and the else part of statement worked. I then used the same code as the else statement with iRow > 1 and it worked. I changed it to A2:I2 and it worked and then I change the I2 to I concatenated with iRow and it now works. The only thing I can think of it somehow a non visible character was in the code somewhere throwing the reference off. Thanks for your help.
Similar threads
-
Solved