VBA Private Sub Column Criteria

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day All,

I'm working with Column H upon my sheet, where if I enter a value into the cell's of this column; I'd like the corresponding cells of Column I to produce an interior color red with the words "Help!".

For example, image:

Capture.PNG



Please let me know, if you can help me with a code for this criteria.

Many thanks!
pinaceous
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

When you enter any value in column H the script will run:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/4/2022  9:35:12 PM  EDT
If Target.Column = 8 Then
    Target.Offset(, 1).Value = "Help!"
    Target.Offset(, 1).Interior.Color = vbRed
End If
End Sub
 
Upvote 0
Try this:
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

When you enter any value in column H the script will run:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  10/4/2022  9:35:12 PM  EDT
If Target.Column = 8 Then
    Target.Offset(, 1).Value = "Help!"
    Target.Offset(, 1).Interior.Color = vbRed
End If
End Sub
Wow, thank My Answer Is This! Please allow me to test it out and get back to you! Many thanks again! Pinaceous
 
Upvote 0
Hi MyAnswerIsThis,

Do you know in your code above, if you can specify its condition?

For example, if Column H has a number and Column I is blank then

VBA Code:
If Target.Column = 8 Then
    Target.Offset(, 1).Value = "Help!"
    Target.Offset(, 1).Interior.Color = vbRed
End If

Please let me know.

Thank you!
pinaceous
 
Upvote 0
You said:
For example, if Column H has a number and Column I is blank then

If column "H" has what number?
and Column I is blank do what
 
Upvote 0
Another option to try. This allows for the possibility of multiple values in column H being changed at the same time. It also prevents itself from being needlessly re-called when the text is entered into column I.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Changed As Range, c As Range
  
  Set Changed = Intersect(Target, Columns("H"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If IsNumeric(c.Value) And IsEmpty(c.Offset(, 1).Value) Then
        With c.Offset(, 1)
          .Value = "Help!"
          .Interior.Color = vbRed
        End With
      End If
    Next c
    Application.EnableEvents = True
  End If
End Sub

Is it possible that after numbers are entered in column H, they could be removed or changed to some text value? If so, what do you want to happen to the red & "Help!"?
What about if column I has something other than "Help!" in it?
 
Upvote 0
Since your now getting help from others I will move on to help out on some other posting.
 
Upvote 0
Hey Peter_SSs!

Thank you for your intuitive thinking!

I'm going to test it out right now!

Respectfully,
pinaceous
 
Upvote 0
Hi Peter_SSs,

Actually, thank you for your questions! Can you add an additional criteria onto your code; if the Column H entry gets deleted that the Column I interior color & text will get removed?

For example; if the first picture represents after the code is executed, the second picture represents what I propose will happen when the Column H entry gets deleted.



Capture.JPG


Capture.JPG


Is this possible? Many thanks!
pinaceous
 
Upvote 0
Is this possible?
Yes, but there seems to be other circumstances not accounted for.

1.
You earlier said ..
if Column H has a number and Column I is blank then
.. and I presumed after "then" came enter "Help!" and colour red.
I also assumed that if column H has a number and column I is not blank then do nothing. So, going the other way, suppose that column H has a number and column I contains something other than "Help!" and then column H is cleared, what should happen?

2.
What should happen if column H contained something other than a number and is then cleared. Is that possible and, if so, what should happen?
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,691
Members
449,117
Latest member
Aaagu

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