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
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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