I suspect that the .end(xlDown)
is acting a bit weird.
Dim rfound As Range
Set rfound = Columns("B:B").Find(What:=Me.ComboBox1.Value, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
If ComboBox1.Value <> "" And WorksheetFunction.CountIf(Range("B:B"), _
ComboBox1.Value) > 0 And rfound.Offset(0, 1).Value <> "" Then
rfound.Offset(0, 1).End(xlDown).Offset(1, 0).Value = TextBox1.Value
Upon CommandButton1_click
, the code will search in the column B for any matched criteria and then OffSet
to the right cell provided my conditions are all met. However, it prompt me a message Run-time error '1004': Application defined or object-defined error
.
I have no clue where is the problem. For illustration:
My three cents...
The beauty is not in writing complex codes but breaking it up in easy to understand lines so that it is easier to understand what the code does. Also it helps in debugging it if an error occurs...
Offset
rfound
exists as brettdj suggestedxlUp
to find the last row.See this example. (UNTESTED)
Your current code
B2
asrfound
(Note: It would be better to test ifrfound
exists after theFind
with `If Not rfound Is Nothing Then)C2
isrfound.Offset(0, 1)
rfound.Offset(0, 1).End(xlDown)
find the last cell in column C as all other cells are blankrfound.Offset(0, 1).End(xlDown).Offset(1, 0)
tries to enter a value in the cell one row below the very last row - no can do.Look up from the bottom instead, ie rather than
Then rfound.Offset(0, 1).End(xlDown).Offset(1, 0).Value = TextBox1.Value
use
Then Cells(Rows.Count, rfound.Offset(0, 1).Column).End(xlUp).Offset(1, 0) = TextBox1.Value