VBa automatically run macro if cell changes

ljkennedy

New Member
Joined
Sep 12, 2019
Messages
4
Hi all,
I want to make the spread sheet automatically run this macro code when a number is inputted into column B
what code do i need to add to it
Sub Ratio_BC()


Dim Lastrow As Long


Application.ScreenUpdating = False


Lastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("D2:D" & Lastrow).Formula = "=IF(ISNUMBER(--MID($B:B,SEARCH(""241"",$B:B),3)),MID($B:B,18,10))"


Application.ScreenUpdating = True


End Sub

thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this event macro (i.e. right click on the tab you want the code to run on and from the short cut menu select View Code and paste the following code) on the sheet in question:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lngLastRow As Long

    If Target.Column = 2 Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
                lngLastRow = Range("B" & Rows.Count).End(xlUp).Row
                Range("D2:D" & lngLastRow).Formula = "=IF(ISNUMBER(--MID($B:B,SEARCH(""241"",$B:B),3)),MID($B:B,18,10))"
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End If

End Sub

Have you tested the formula as I'm I don't think SEARCH or MID don't work for entire columns?

Robert
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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