I thought I was fairly proficient in VBA but this has me stumped and has cost many many hours without a solution, hence request for help.
my Userform with combobox entries for product checks against the rowsource and if not on the list, is added to the list. the list is a named range in a Table that is used in a data model.
No matter which way I try I am regularly getting a corruption in the table that is home to the named range which is the rowsource... after one or two entries from the userform.
The rowsource is not bound to the worksheet, it is populated at userform_initialize procedure.
(There are 4 combobox drawing from same rouwsource hence the loop through to check each)
debug points to the line at which conbobox value is being entered into the rowsource, though not always - it sometimes just crashes the whole file and everything needs restarting, upon which the table is fine and entry is evident.
I have tried resize, addrow to bottom of table variations and all result in the same core problem of a table corruption that crashes the file.
I am totally stumped and out of ideas. any help appreciated
Private Sub CommandButton1_Click()
Application.EnableEvents = False
Dim cCont As Control
Dim sourcedata As Range, NxtEmptyRow As Long
Dim found As Object
Dim ws As Worksheet
Dim Result As Variant
Dim Tble As ListObject
Set ws = Worksheets("ProductInfo")
Set sourcedata = ws.Range("SprayProductName")
For Each cCont In Me.FrameRecommendations.Controls
On Error Resume Next
If TypeName(cCont) = "ComboBox" Then
NxtEmptyRow = ws.Range("T3").End(xlDown).Row + 1
Set found = Nothing
'Set cCont = Product1Rec '1
Set found = sourcedata.Find(cCont.Value)
If found Is Nothing Then
Set Result = cCont
ws.Cells(NxtEmptyRow, 20) = Result
cCont.RowSource = Range("SprayProductName").Address(external:=True)
End If
End If
Next cCont
Application.EnableEvents = True
Exit Sub
errHandler:
MsgBox Err.Description
End Sub
the sub to initialize the rowsource (and the rest of the userform) is below
Private Sub SprayRecEntryForm_Initialize()
'empty all Control Boxes and list box with Clear command box
Product1rec.RowSource = Range("SprayProductName").Address(external:=True)
Product2rec.RowSource = Range("SprayProductName").Address(external:=True)
Product3rec.RowSource = Range("SprayProductName").Address(external:=True)
Product4rec.RowSource = Range("SprayProductName").Address(external:=True)
more code below but cut for brevity
thanks
Bookmarks