Declare a global variable to refer to workbooks

TheHack22

Board Regular
Joined
Feb 3, 2021
Messages
121
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi all,

I have these multiple VBA subs. See two examples below. I keep changing all my file names (Calculator for All Class 8.12.2021.xlsm) every day (by find and replace) when I run these subs.

Can I declare a global sub name once, and then refer to that name in all my subs and across all modules?
I know my VBA code below is not efficient. I don't know VBA. I would just copy and paste, play around with it to make it work.

VBA Code:
Sub CLASS1_INTL()
Dim MyRange As Range
Windows("1_CLASS_INTL_Daily.csv").Activate
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
    
    Windows("Calculator for All Class 8.12.2021.xlsm"). _
        Activate
    Sheets("CLASS 1 - INTL").Select
       
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("1_CLASS_INTL_Daily.csv").Close
End Sub


Sub CLASS2_INTL()
Dim MyRange As Range
 Windows("2_CLASS_INTL_Daily.csv").Activate
 
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
    
    Windows("Calculator for All Class 8.12.2021.xlsm"). _
        Activate
    Sheets("CLASS 2 - INTL").Select
       
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("2_CLASS_INTL_Daily.csv").Close
End Sub
 
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
It might be easier for you to use a UDF rather than a global variable that has to be set before being used.
 
Upvote 0
You could do that like
VBA Code:
Option Explicit
Const WBkName As String = "Calculator for All Class 8.12.2021.xlsm"
Sub CLASS1_INTL()
Dim MyRange As Range
Windows("1_CLASS_INTL_Daily.csv").Activate
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
    
    Windows(WBkName).Activate
    Sheets("CLASS 1 - INTL").Select
       
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("1_CLASS_INTL_Daily.csv").Close
End Sub


Sub CLASS2_INTL()
Dim MyRange As Range
 Windows("2_CLASS_INTL_Daily.csv").Activate
 
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
    
    Windows(WBkName).Activate
    Sheets("CLASS 2 - INTL").Select
       
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("2_CLASS_INTL_Daily.csv").Close
End Sub
 
Upvote 0
Solution
You could do that like
VBA Code:
Option Explicit
Const WBkName As String = "Calculator for All Class 8.12.2021.xlsm"
Sub CLASS1_INTL()
Dim MyRange As Range
Windows("1_CLASS_INTL_Daily.csv").Activate
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
   
    Windows(WBkName).Activate
    Sheets("CLASS 1 - INTL").Select
      
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("1_CLASS_INTL_Daily.csv").Close
End Sub


Sub CLASS2_INTL()
Dim MyRange As Range
 Windows("2_CLASS_INTL_Daily.csv").Activate
 
 With ActiveSheet.UsedRange
        Set MyRange = Range(.Cells(2, 1), .Cells(1, 1).Offset(.Rows.Count - 2, .Columns.Count - 1))
        MyRange.Select
        Selection.Copy
End With
   
    Windows(WBkName).Activate
    Sheets("CLASS 2 - INTL").Select
      
    Range("A2").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Windows("2_CLASS_INTL_Daily.csv").Close
End Sub
@Fluff

Thanks very much for your prompt help. This works perfectly. You're the best. :)

Imran
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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