Calling sub from module in sheet

Suezz1994

New Member
Joined
Nov 8, 2022
Messages
1
Hello,

I'm trying to call a Sub which I created in a module into a worksheet. The idea is to create multiple modules which I can call in this worksheet where the module is applying on. This are the codes I use right now.

Module 1
VBA Code:
Private Sub Trap()

If Intersect(Range("C31"), Target) Is Nothing Then Exit Sub

If Target.Value = "Nee" Then

Sheets("AB-BV-BF").Rows(46).Interior.ColorIndex = 16
Sheets("AB-BV-BF").Rows(65).Interior.ColorIndex = 16
Sheets("AB-BV-BF").Rows(66).Interior.ColorIndex = 16
Sheets("AB-BV-BF").Rows(67).Interior.ColorIndex = 16

End If

If Target.Value = "Ja" Then

Sheets("AB-BV-BF").Rows(46).Interior.ColorIndex = xlNone
Sheets("AB-BV-BF").Rows(65).Interior.ColorIndex = xlNone
Sheets("AB-BV-BF").Rows(66).Interior.ColorIndex = xlNone
Sheets("AB-BV-BF").Rows(67).Interior.ColorIndex = xlNone

End If

End Sub

Worksheet
VBA Code:
Private Sub Worksheet_Activate()

Application.Run ("Module1.Trap")

End Sub


When I use the code which I placed in the module directly in the worksheet, the code is working. But I have to create multiple lines for other values like Lift and Vide, which was not working in the worksheet. So I want to create Modules for those values and run them on the worksheet.

Can someone help me with this problem?

Kind regards,
Sue Ellen
 
Last edited by a moderator:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Private Sub Trap()

This is not what we refer to as a Module Script;
A Module script is created in the vba editor window.
Which would look something like this:
Sub Hello()

A Worksheet script can run a Module script by entering a line of code like this in your worksheet Private script:
Call Hello
 
Upvote 0
You should not make the Trap routine private, then you wouldn't need Run - you can just call it directly. However, your code doesn't really make a lot of sense since Target has no value, and it's not an argument provided by a Worksheet_Activate event. What do you expect it to be?
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,139
Members
453,021
Latest member
Justyna P

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