ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Code crashes when ComboB = empty (https://www.excelbanter.com/excel-programming/302944-code-crashes-when-combob-%3D-empty.html)

Stift[_45_]

Code crashes when ComboB = empty
 
Hi everyone, I've got a userform where users can enter data into a
excelsheet.

ComboBox1 will be filled with descriptions from a range cells.
When the backgroundcolor of a cell red is, it will be skipped and als
when de data already is filled in.
But when all data is filled and/or backgrounds are redcolored I get a
ListIndex error.

Please help me fix this, I really need it.



Code
-------------------

Private Sub UserForm_Initialize()

Dim Cell As Range
Dim Counter As Long
Dim ListRange As Range
Dim ListRangeValue() As Variant

Set ListRange = _
ActiveSheet.Range("C9:C106,C113:C162,C169:C183")
ListRange.Style.IncludeNumber = True

ReDim ListRangeValue(0 To ListRange.Cells.Count)

For Each Cell In ListRange.Cells
If Cell.Interior.ColorIndex < 3 Then
Set Celv = Cells(Cell.Row, 16 + Weekscherm.ComboBox1.Value)
If Celv = "" Then
ListRangeValue(Counter) = Cell.Value
Counter = Counter + 1
Me.ComboBox2.AddItem (Cell.Row)
Me.ComboBox3.AddItem Cells(Cell.Row, 2)
'MsgBox Cells(Cell.Row, 2)
End If
End If
Next Cell

TextBox1.SetFocus
Me.ComboBox1.List = ListRangeValue
Me.ComboBox1.ListIndex = 0
Me.ComboBox3.ListIndex = 0
TextBox6.Value = Weekscherm.ComboBox1.Value
End If

End Sub

Private Sub CommandButton1_Click()
X = Me.ComboBox1.ListIndex
If Me.TextBox1.Text = "" Or Me.TextBox1.Text = "" Then
MsgBox "TextBox = empty"
X = X - 1
Else
Set Cel = Cells(ComboBox2.Value, 16 + Weekscherm.ComboBox1.Value)
Cel.Value = _
Me.TextBox1.Text
Me.ComboBox1.ListIndex = IIf(X = 160, 0, X + 1)
Me.ComboBox2.ListIndex = Me.ComboBox1.ListIndex
Me.ComboBox3.ListIndex = Me.ComboBox1.ListIndex
Me.TextBox1.Text = ""
End If

End Su
-------------------


I'm a very bad programmer and created this code with examples,
so please help meeeeeeeeeee !!!!
10000000000000x THANKS in Advanc

--
Message posted from http://www.ExcelForum.com


Stift[_46_]

Code crashes when ComboB = empty
 
:

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 12:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com