Debug IF Then within For Next

excelcraze

Board Regular
Joined
Sep 10, 2012
Messages
81
Hello,

I have been trying to debug following code since yesterday, but am failed. Can somebody help me please?
I actually don't know For... Next loop. I was just trying to create my own for the project I have.
I use F8 and see what'a happening but even when the value matches it doesn't execute comments what's after 'Then'

Code:
Private Sub CommandButton1_Click()

Me.Hide
Application.ScreenUpdating = False
Sheets("Add").Select
lr = Application.WorksheetFunction.Count(Range("A:A"))
For i = 1 To lr
If Range("A" & i).Value = frmPBill.cbSelectCustID.Value Then
        
frmViewPBills.lblPBillID.Caption = frmPBill.cbSelectCustID.Value
frmViewPBills.Show
Else


End If


MsgBox "Customer (" & frmPBill.cbSelectCustID.Value & ") does not exist"
Next i
End Sub
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi there,

I would most likely suggest running up from the bottom of the sheet to find the last cell with data, rather than COUNT(). Disregarding that, as we cannot see your workbook, I would look at this:

Range("A" & i).Value = frmPBill.cbSelectCustID.Value

If the range's value is numeric, and the values in your (presumably) combobox are as well, then try wrapping CDbl() around the combobox's .Value. If that works, it means that VBA was reading a string (it always does AFAIK) from the combobox, and failed to coerce this to a number to compare to the cell's value. See, TextBoxes, ComboBoxes and such, all store strings, so sometimes you need to coerce the control's return value. Does that help?

Mark
 
Upvote 0
try wrapping CDbl() around the combobox's .Value.

An expert only know that.

Thank you very much indeed.

I would most likely suggest running up from the bottom of the sheet to find the last cell with data, rather than COUNT().

Can you tell me how to do that? I did this way as I don't know how to do that.

Thank you once again.
 
Upvote 0
Find last row with

<Code>

LR = Range("A65536").End(xlUp).Row

</Code>
 
Upvote 0
excelcraze,


Try changing the next two code lines:

Rich (BB code):
lr = Application.WorksheetFunction.Count(Range("A:A"))

For i = 1 To lr

To this:

Rich (BB code):
lr = Cells(Rows.Count, "A").End(xlUp).Row

For i = lr To 1 Step -1
 
Last edited:
Upvote 0
dispelthemyth,

I would suggest that you use code tags.


When posting VBA code, please use Code Tags - like this:

[code]

Paste your code here.

[/code]
 
Upvote 0
Ive been using the below as code tags
<code>
<noparse><code><</code>> not []


Is that not a valid tag?

I'll use [] going forward, i just thought the difference was formatting


dispelthemyth,

I would suggest that you use code tags.


When posting VBA code, please use Code Tags - like this:

[code]

Paste your code here.

[/code]
</noparse></code>
 
Last edited:
Upvote 0
dispelthemyth,

You could go here:
Test Here

And, start a new thread.

Then use your present style for displaying some macro code.

Then use what I have suggested for the same macro code.

Save the thread.

Then open the thread again, and, you should see the difference.
 
Upvote 0
Please tell me how to code to deal with value of a textbox.
When I wrap textbox.value with CDbl() it did what exactly I want. But if I enter any letters into it I get error, how can I solve this issue. How to avoid this error and how to show a message box if letters are input?

Thanks.
 
Upvote 0

Forum statistics

Threads
1,203,435
Messages
6,055,353
Members
444,781
Latest member
rishivar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top