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
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
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!
 
Upvote 0
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")?
 
Upvote 0
If the code name for the sheet is Sheet25...

Code:
Sheet25.Range("A2").Value
 
Upvote 0
Try...

Code:
Worksheets(CStr(ActiveWorkbook.VBProject.VBComponents(XXX).Properties("Name"))).Range("A2").Value
 
Upvote 0
It says "Programmatic access to Visual basic Project is not trusted" . Run time error 1004
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
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