VBA In a Table Range Cell A4 <> "" then Cell B4 = 140. Same for next A5 and B5 and so on

JulianvO

New Member
Joined
Sep 9, 2022
Messages
25
Office Version
  1. 2021
Platform
  1. Windows
Greetings

I have a table("BloodPressure") containing the following fields:
Date - Systolic Baseline - Systolic Reading - Diastolic Baseline - Diastolic Reading - Pulse Rate

When a date is entered, the Systolic Baseline must automatically have the value 140 inserted as well as
the Diastolic Baseline must automatically have the value 90 inserted.

If date column is empty, then no values are entered in columns Systolic Baseline and Diastolic Baseline.

My code:

Option Explicit

Dim BPrng As Range
Dim iDate As Variant
Dim BL1 As Variant
Dim BL2 As Variant

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Set BPrng = Sheet1.Range("BloodPressure")

If iDate <> "" Then
BL1 = 140 And BL2 = 90
Else: BL1 = "" And BL2 = ""
End If

End Sub

I keep getting a Type Mismatch error.

I can get the code to work if I specify the Range cells individually, but unable to create a dynamic function.

Can anyone kindly assist me?

Thanking you kindly
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi @JulianvO
Could you please share your workbook? I am unable to test the code without proper data.
 
Upvote 0
Like This ?
This code will automatically fill the "Systolic Baseline" column with 140 and "Diastolic Baseline" with 90, with a reference if you enter any value in each column A

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next


If Not Intersect(Range("A:A"), Target) Is Nothing Then
Application.EnableEvents = False
    If Target <> "" Then
        Cells(Target.Row, 2).Value = 140
        Cells(Target.Row, 4).Value = 90
            Else
        Cells(Target.Row, 2).Clear
        Cells(Target.Row, 4).Clear
    End If
End If
Application.EnableEvents = True
End Sub

1706867618573.png
 
Last edited:
Upvote 0
Solution
Like This ?
This code will automatically fill the "Systolic Baseline" column with 140 and "Diastolic Baseline" with 90, with a reference if you enter any value in each column A

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next


If Not Intersect(Range("A:A"), Target) Is Nothing Then
Application.EnableEvents = False
    If Target <> "" Then
        Cells(Target.Row, 2).Value = 140
        Cells(Target.Row, 4).Value = 90
            Else
        Cells(Target.Row, 2).Clear
        Cells(Target.Row, 4).Clear
    End If
End If
Application.EnableEvents = True
End Sub

View attachment 106192

Greetings

SunnyAlv

Thank you very much. I have learnt something new.
Greetings

PeteWright

Please refer to above entry.

Thank you
 
Upvote 0

Forum statistics

Threads
1,215,097
Messages
6,123,077
Members
449,094
Latest member
mystic19

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