Passing Public variable from Worksheet selection change event to Module macro

didijaba

Well-known Member
Joined
Nov 26, 2006
Messages
511
Hi,
I'm having trouble using public declared variable from Worksheet Selection Change event to macro in Module1.
THis is code I have. Thanks for any advice you can spare.
Code:
Option Explicit
Public XXX As String

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim CodeCall As String

If Application.Or(Target.Rows.Count > 1, Target.Columns.Count > 1) Then
Exit Sub
End If
Select Case Target.Column
Case 2

If IsEmpty(Target) = False Then
XXX = "Sheet24"
CodeCall = "'" & ActiveWorkbook.Name & "'!" & Target.Offset(0, 9).Text
Application.Run CodeCall
End If
Case 5

If IsEmpty(Target) = False Then
XXX = "Sheet25"
CodeCall = "'" & ActiveWorkbook.Name & "'!" & Target.Offset(0, 7).Text
Application.Run CodeCall
End If
Case 8

If IsEmpty(Target) = False Then
XXX = "Sheet31"
CodeCall = "'" & ActiveWorkbook.Name & "'!" & Target.Offset(0, 5).Text
MsgBox CodeCall
Application.Run CodeCall
End If
End Select
End Sub

Code:
Option explicit
Sub dannyrolnickevent(XXX)
Sheet2.Range("B6:H14").Value = XXX.Range("L2:R10").Value
Call SubRoutine_01
End Sub
 

Some videos you may like

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
Declare your public variable in a regular module, instead of a sheet module. Then replace...

Code:
Sheet2.Range("B6:H14").Value = XXX.Range("L2:R10").Value

with

Code:
Sheet2.Range("B6:H14").Value = [COLOR=#ff0000]Worksheets(XXX)[/COLOR].Range("L2:R10").Value

Hope this helps!
 

didijaba

Well-known Member
Joined
Nov 26, 2006
Messages
511
thanks, this is very nice. But what would code look if I would use codename of sheet? For example in VBA Sheets(Sheet25), not Sheets("Big sheet")?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
If the code name for the sheet is Sheet25...

Code:
Sheet25.Range("A2").Value
 

didijaba

Well-known Member
Joined
Nov 26, 2006
Messages
511

ADVERTISEMENT

I understand that, but what would be proper way to write it using XXX string?
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
Try...

Code:
Worksheets(CStr(ActiveWorkbook.VBProject.VBComponents(XXX).Properties("Name"))).Range("A2").Value
 

didijaba

Well-known Member
Joined
Nov 26, 2006
Messages
511
It says "Programmatic access to Visual basic Project is not trusted" . Run time error 1004
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
You'll need to allow access to the VBA project...

File > Options > Trust Center > Trust Center Settings > Macro Settings > check/select Trust access to VBA project object model
 

Watch MrExcel Video

Forum statistics

Threads
1,122,233
Messages
5,594,963
Members
413,954
Latest member
mrsandy

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