add two different vba codes to same sheet

joelmidiowa

New Member
Joined
Dec 16, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
i am trying to add the two different lines of code to the same excel sheet. one to automatically add rows then the next i want it to auto sort column G. i when i add both to the same sheet it won't work

Private Sub Worksheet_Change(ByVal Target As Range)

'Test if change is in column B and only one cell changed
If Not Intersect(Target, Me.Columns("B")) Is Nothing And _
Target.Cells.Count = 1 Then

Target.Offset(1, 0).EntireRow.Insert Shift:=xlDown 'Insert row below data entry

'Target.EntireRow.Insert Shift:=xlDown 'Insert row above data entry

End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("G:G")) Is Nothing Then
Range("G1").Sort Key1:=Range("G2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Test this on a copy first

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Test if change is in column B and only one cell changed
Appication.EnableEvents = False
    If Not Intersect(Target, Me.Columns("B")) Is Nothing And _
        Target.Cells.Count = 1 Then
        Target.Offset(1, 0).EntireRow.Insert Shift:=xlDown 'Insert row below data entry
        'Target.EntireRow.Insert Shift:=xlDown 'Insert row above data entry
    End If
On Error Resume Next
    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        Range("G1").Sort Key1:=Range("G2"), _
        Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
On Error GoTo 0
Err.Clear
Application.EnableEvents = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,107
Messages
5,622,782
Members
415,927
Latest member
vedasinternational

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
Top