Stephenosn

Board Regular
Joined
Jun 2, 2015
Messages
52
Hello all,

I'm working on a userform and am setting up some insurance the users enter the required data. I have a three text boxes: tbMaterial, tbQty and tbCost. There is a index match function for tbMaterials that fills in the cost if its a material we already have the price for. If we do not have the price then the user needs to fill out the cost. My code doesn't appear to make a difference if tbMaterials value matches a value in the range or not. For Ref_Appliance I'm using the following.
Code:
=OFFSET(Inventory!$B$4,,,COUNTA(Inventory!$B:$B),1)

The code I'm trying to use for validation is as follows.

Code:
    Private Sub btAdd_Click()    
    Dim k As Integer, i As Integer
    Dim Ref_Appliance As Range
    Dim bMatch As Boolean
    
    For Each Ref_Appliance In Range("B2:B50")    
    If Ref_Appliance.Value = tbMaterial.Value And tbCost <> "" Then
        MsgBox "HEY! No need for price, I got your back pal."
        tbCost = ""
    End If
    If Appliance.Value = tbMaterial.Value And tbQty = "" Then
        MsgBox "Hey lazy hows about givin' me a quantity."
        tbQty.SetFocus
        Exit Sub
    End If
    If Appliance.Value <> tbMaterial.Value And tbCost <> "" And tbQty = "" Then
            MsgBox "Ben you dummy, enter a quantity for your materials."
        tbQty.SetFocus
        Exit Sub
    End If
    If Appliance.Value <> tbMaterial.Value And tbCost = "" And tbQty <> "" Then
            MsgBox "Seriously Ben? A quantity without a cost? As if."
        tbCost.SetFocus
        Exit Sub
    End If
  
    Next


Please let me know if you know of a way to tackle my problem.

Thank you for having a look,

John
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Surely everywhere you have
Code:
Appliance.Value
it should be
Code:
[COLOR="#FF0000"][B]Ref_[/B][/COLOR]Appliance.Value
 
Upvote 0
MARK858,

Thank you for catching that. I've made the change there, but am still running into the same problem. My code doesn't actual do any sort of lookup. So I need to be referencing Ref_Appliance differently?

Thanks,

John.
 
Upvote 0
Have you set breakpoints in your code and hovered your mouse over the variables or used debug.print to test exactly what your variables are returning?

Have you tried changing

Code:
tbMaterial.Value
to
Code:
tbMaterial.Text

and possibly test for an empty box with

Code:
If Len(tbCost.Text) = 0

rather than ""
 
Last edited:
Upvote 0
To refer to the named range Ref_Appliance, you might use syntax like

ThisWorkbook.Names("Ref_Appliance").RefersToRange
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,550
Members
449,735
Latest member
Gary_M

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