Two Macros In One Sheet

vdespard

New Member
Joined
Jan 3, 2018
Messages
7
I have two separate macros in one worksheet, but I can't get them to both work. Below are the two macros. The red font is not part of the coding, just my notation to separate the two macros for this post. How can I code these to both work?

MACRO 1:
Private Sub Worksheet_Change(ByVal Target As Range)

If Range("B3").Value = "Assumption" Then
Columns("H:J").Hidden = False
Else
Columns("H:J").Hidden = True
End If
If Range("B3").Value = "New Loan" Then
Columns("E:G").Hidden = False
Else
Columns("E:G").Hidden = True
End If
If Range("I14").Value = "Yes" And Range("B3").Value = "Assumption" Then
Columns("K:M").Hidden = False
Else
Columns("K:M").Hidden = True
End If
If Range("B4").Value = "Yes" Then
Columns("N:P").Hidden = False
Else
Columns("N:P").Hidden = True
End If


End Sub

MACRO 2:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 And Target.Row = 7 Then
If Target.Value = "Loan $" Then Target.Offset(2, 0).Formula = "=F14/F8": Target.Offset(2, 0).Interior.Color = RGB(217, 217, 217)
If Target.Value = "Loan to Cost" Then
Target.Offset(7, 0).Formula = "=F8*F9": Target.Offset(2, 0).Interior.Color = vbWhite
End If
End If
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Where do you have the code? Change event code should go in the worksheet module of the sheet you want the code to run on. You can only have one change code on the sheet you would need to combine them.
 
Upvote 0
Thanks. Yes, the code is in the worksheet module of the sheet that the code runs on. How do I combine the two macros?
 
Upvote 0
Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Range("B3").Value = "Assumption" Then
 Columns("H:J").Hidden = False
 Else
 Columns("H:J").Hidden = True
 End If
 If Range("B3").Value = "New Loan" Then
 Columns("E:G").Hidden = False
 Else
 Columns("E:G").Hidden = True
 End If
 If Range("I14").Value = "Yes" And Range("B3").Value = "Assumption" Then
 Columns("K:M").Hidden = False
 Else
 Columns("K:M").Hidden = True
 End If
 If Range("B4").Value = "Yes" Then
 Columns("N:P").Hidden = False
 Else
 Columns("N:P").Hidden = True
 End If
If Target.Column = 6 And Target.Row = 7 Then
 If Target.Value = "Loan $" Then Target.Offset(2, 0).Formula = "=F14/F8": Target.Offset(2, 0).Interior.Color = RGB(217, 217, 217)
 If Target.Value = "Loan to Cost" Then
 Target.Offset(7, 0).Formula = "=F8*F9": Target.Offset(2, 0).Interior.Color = vbWhite
 End If
 End If

 End Sub
 
Upvote 0
That worked! Thanks!!

Try
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 If Range("B3").Value = "Assumption" Then
 Columns("H:J").Hidden = False
 Else
 Columns("H:J").Hidden = True
 End If
 If Range("B3").Value = "New Loan" Then
 Columns("E:G").Hidden = False
 Else
 Columns("E:G").Hidden = True
 End If
 If Range("I14").Value = "Yes" And Range("B3").Value = "Assumption" Then
 Columns("K:M").Hidden = False
 Else
 Columns("K:M").Hidden = True
 End If
 If Range("B4").Value = "Yes" Then
 Columns("N:P").Hidden = False
 Else
 Columns("N:P").Hidden = True
 End If
If Target.Column = 6 And Target.Row = 7 Then
 If Target.Value = "Loan $" Then Target.Offset(2, 0).Formula = "=F14/F8": Target.Offset(2, 0).Interior.Color = RGB(217, 217, 217)
 If Target.Value = "Loan to Cost" Then
 Target.Offset(7, 0).Formula = "=F8*F9": Target.Offset(2, 0).Interior.Color = vbWhite
 End If
 End If

 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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