VBA: Global Variable (Image) not working

GH8D8

New Member
Joined
Oct 17, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi, I'm trying out a new module where I'm trying to code an image as a global variable. I'm new to VBA so please forgive me if I'm doing some things wrong:

VBA Code:
Public Sub Variables()

Global Astronaut As Shape
'Astronaut is the variable name (Image name)
Global ws As Worksheet

Set ws = ActiveSheet
Set Astronaut = ws.Shapes("New Image Name")
Astronaut.Name = "New Image Name"

End Sub

However, when I try to reference this image in other subs, Excel doesn't recognise it. E.g. this example:

Code:
    Astronaut.Select
    Selection.Cut
    Range("R18").Select
    ActiveSheet.Paste
    
    Selection.TopLeftCell.Select

Could you help to let me know why the global variable isn't working as intended? Did I define some things wrongly etc. Your help is much appreciated!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Alex Blakenburg

MrExcel MVP
Joined
Feb 23, 2021
Messages
5,125
Office Version
  1. 365
Platform
  1. Windows
Try putting the 2 Global statements outside the Sub at the module level.
(right at the top, if you are using Option Explicit just after that)
 

GH8D8

New Member
Joined
Oct 17, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Try putting the 2 Global statements outside the Sub at the module level.
(right at the top, if you are using Option Explicit just after that)
Hey thanks! this worked for me. When I made them part of their respective module which contained the variables it did work. Perhaps local variables are better?
 

Alex Blakenburg

MrExcel MVP
Joined
Feb 23, 2021
Messages
5,125
Office Version
  1. 365
Platform
  1. Windows
Just some background information.
Paul Kelly on Excel Macro Mastery and Russell Proctor from Better Solutions, both refer to the "Global" word as being only there for backward compatibility and that what most people refer to as Global should be declared as being Public.
Paul considers both Module Level (Private) and Project Level (Public) as being Global. Both are declared outside the Sub at the top of a module.

The microsoft documentation does not seem to use the word Global.
Declaring variables (VBA)

The consensus seems to be that you want to avoid declaring variables as Global because it is easy to get confused on where and what it was last set to and because it has a life time beyond just the currently running procedure.
Russell recommends using the prefix m_ for module level and g_ for project level variables so you at least have a heads up that the scope and life go beyond the current procedure.
VBA Variables - Lifetime and Scope

This article is well worth reading:-
VBA Dim - A Complete Guide - Excel Macro Mastery
(just keep in mind that Paul is using the term Global to cover both Module Level and Project Level variables)
 

Forum statistics

Threads
1,176,287
Messages
5,902,328
Members
434,962
Latest member
sgilmoreBBP

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