VBA Form Textbox Color Format Determined by Parameter Worksheet

Jonny117

New Member
Joined
Dec 21, 2017
Messages
4
Hello Everyone, first post for me on here though I've visited this site from time to time so I'll start by saying thanks to all those helping to answer questions, give tips, etc.

Now to the issue I am having with my VBA Userform.

I have a mostly completed form for inspectors to fill in values and submit data once complete, I am trying to get the form text boxes to turn yellow based on the values selected in a "Size" and "SDR" combobox and then if the value they enter in a textbox is outside the acceptable tolerance on a parameters chart said textbox turn yellow (see parameter chart example below). I am unable to figure out how to color a userform textbox based on a value in my parameters sheet when the parameter value is determined by two columns. In the chart below you can see that multiple rows have the "size" column value of "2" "3" etc. and also the "SDR" column also comes into play when I am trying to format the color of a text box if the value entered in said textbox falls outside those values.

Example: If the inspector selects Size: 2 in one combobox and then SDR: 7 in another and then if the operator enters a Wall Thickness value less than 0.339 or greater than 0.380 then the textbox should turn yellow.
If the inspector selects Size:2 and SDR: 9 in the comboboxes then this time if the operator enters a Wall Thickness value less than 0.264 or greater than 0.296 then the textbox will turn yellow.

SIZESDRWALL THICKNESSHUB THICKNESSPIPE O.D.FLANGE O.D.OAL
MINMAXMINMAXMINMAXMINMAX
270.3390.3800.4500.5042.3692.3813.9804.0006.0006.250
290.2640.2960.4500.5042.3692.3813.9804.0006.0006.250
2110.2160.2420.4020.4502.3692.3813.9804.0006.0006.250
370.5000.5600.6250.7003.4923.5084.9885.0126.0006.250
390.3890.4360.6250.7003.4923.5084.9885.0126.0006.250
3110.3180.3560.5980.6703.4923.5084.9885.0126.0006.250

<tbody>
</tbody>


Below is working code I have for the parameters that are just determined by Size, because for instance the Pipe O.D. parameter is only determined by size and not also the SDR as seen in the above chart.
Code:
Private Sub PipeODText_AfterUpdate()

 Dim Found As Range
    On Error Resume Next
   
    If PipeODText.Value <> "" Then
        PipeODText.BackColor = rgbWhite
        PipeODLabel.ForeColor = Me.ForeColor
    End If
    
    If SizeCombo.Value <= 63 Then
      ' Find size in table
      Set Found = wsParameters.Range("A:A").Find(what:=SizeCombo.Value, lookat:=xlWhole, LookIn:=xlValues)
      If Found Is Nothing Then
         MsgBox "Could not find size value " & SizeCombo.Value & " in Parameters."
      Else
         If CDbl(PipeODText.Value) < wsParameters.Cells(Found.Row, "H") Or _
            CDbl(PipeODText.Value) > wsParameters.Cells(Found.Row, "I") Then
            PipeODText.BackColor = rgbYellow
         Else
            PipeODText.BackColor = rgbWhite
         End If
      End If
   End If


End Sub


This is the troubling code, I tried to get similar code below to work for the values determined by Size and SDR but haven't had any success.
Code:
Private Sub WT1Text_AfterUpdate()

Dim Found As Range
    On Error Resume Next
   
    If WT1Text.Value <> "" Then
        WT1Text.BackColor = rgbWhite
        WT1Label.ForeColor = Me.ForeColor
    End If
    
    If SizeCombo.Value <= 63 Then
     If SDRCombo.Value <= 32.5 Then
      ' Find size in table
      Set Found = wsParameters.Range("A:A").Find(what:=SizeCombo.Value, lookat:=xlWhole, LookIn:=xlValues)
      ' Find sdr in table
      Set Found = wsParameters.Range("C:C").Find(what:=SDRCombo.Value, lookat:=xlWhole, LookIn:=xlValues)
      If Found Is Nothing Then
         MsgBox "Could not find SDR value " & SDRCombo.Value & " in Parameters."
        
      Else
         If CDbl(WT1Text.Value) < wsParameters.Cells(Found.Row, "D") Or _
            CDbl(WT1Text.Value) > wsParameters.Cells(Found.Row, "E") Then
            WT1Text.BackColor = rgbYellow
         Else
            WT1Text.BackColor = rgbWhite
         End If
        End If
       End If
      End If
    
End Sub

Is there a way to provide my project for better review?

Hopefully someone can help with this.

Regards,
Jon
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Anyone have any ideas on this?

I'm new to VBA coding and am learning as I go.

I've done some searching around but having a hard time finding something that will work for my case here.

Regards,
Jon
 
Upvote 0
I may have figured it out with the below:

Code:
Private Sub WT1Text_AfterUpdate()

    Dim Found As Range
    On Error Resume Next
    
    ' Find size in parameters sheet
    With SizeCombo.Value <= 63
        Set Found = wsParameters.Range("A:A").Find(what:=SizeCombo.Value, lookat:=xlWhole, LookIn:=xlValues)
    End With
    
    ' Find sdr in parameters sheet
    With SDRCombo.Value <= 32.5
        Set Found = wsParameters.Range("C:C").Find(what:=SDRCombo.Value, lookat:=xlWhole, LookIn:=xlValues)
    End With
    
    ' Compare value entered in textbox to parameters sheet
    If CDbl(WT1Text.Value) < wsParameters.Cells(Found.Row, "D") Or _
        CDbl(WT1Text.Value) > wsParameters.Cells(Found.Row, "E") Then
            WT1Text.BackColor = rgbYellow
        Else
            WT1Text.BackColor = rgbWhite
    End If
        
    ' if textbox left blank, turn white
    If WT1Text.Value = "" Then
        WT1Text.BackColor = rgbWhite
    End If
    
End Sub
 
Upvote 0
Have you tried conditional formatting? No code would be needed in this instance

Hi Codeliftsleep

What I'm trying to do with this userform is have inspectors use the form to submit the data and not have to deal with the spreadsheet at all, so when they open the excel file it automatically opens the userform in fullscreen and when they close the userform it saves and closes the entire file but I still want to give them an easy visual clue on the userform itself if something is out of spec with the values they entered.

It looks like the code I posted in my previous post kinda works for some sizes but not all, truly confused on why this would be.
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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