How to Public Set - Public Declared variables? - excel

I have a separate module to declare public variables
Public ws1, ws2 As Worksheet
On each module, on each procedure I must repeat the following :
Set ws1 = Sheets("Sun")
Set ws2 = Sheets("Moon")
...and then - write the code.
So, WHERE and HOW can I Set this variables so they are allready set on each module, on each Sub or Function - as Sheets Sun and Moon ?

Once you have it set it will remain available to the other modules. So I suggest you set it on Workbook_Open. Also your current code does not dimension ws1 as a worksheet, so I suggest you use
Standard Module
Public ws1 As Worksheet, ws2 As Worksheet
ThisWorkbook Module
Private Sub Workbook_Open()
Set ws1 = ThisWorkbook.Sheets("Sun")
End Sub

Related

Setting Public variables in VBA Excel

I am creating public variables in my program to be used across multiple subs in the module.
Public c As Range, wsSrc As Worksheet, wsDest As Worksheet, cDest As Range
However I want these variables to have the following value across the entire function as well
Set wsSrc = Workbooks("SourceBook").Worksheets("SourceSheet")
Set wsDest = Workbooks("DestBook").Worksheets("DestSheet")
Is there a way for me to set these and their values carry across the entire function?
Also for c and cDest I am changing those in the functions so I Do Not want those defined globally. Only wsSRC and wsDest. Are variables in VBA immutable?
This is a question of scope. For using variables across a module, you don't need to set them public - Public is used for making variables available to other modules.
In this case just need to declare variables outside function
'Module begining
Dim wsSrc As Worksheet, wsDest As Worksheet
Function f1
Dim c As Range, cDest As Range
'... wsSrc and wsDest are acessible, eg:
Set c = wsSrc.Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, _ LookAt:=xlPart, MatchCase:=False)
End Function
Function f2
'... wsSrc and wsDest are also acessible here
End Function
However, to avoid initialization problems, you may use them as properties, so each time they are called, value is properly returned:
'Module begining
Property Get wsSrc As Worksheet
Set wsSrc = Workbooks("SourceBook").Worksheets("SourceSheet")
End Property
Property Get wsSrc As Worksheet
Set wsDest = Workbooks("DestBook").Worksheets("DestSheet")
End Property
Function f1
Dim c As Range, cDest As Range
'... wsSrc and wsDest are acessible
End Function
Function f2
'... wsSrc and wsDest are also acessible here
End Function

Do you need to pass both the workbook and worksheet to sub/function

If you are going to be just working with a single worksheet in your function or sub and you pass the worksheet, do you also need to pass the workbook, or does that come along with worksheet that was passed?
Option 1
Sub option1 (byref wb as workbook, byref ws as worksheet)
wb.ws.range("A1") = 8
end sub
Option 2
sub option2(byref ws as worksheet)
ws.range("A1") = 8
end sub
in option 2 is there any danger of of opening a worksheet with the same name in a different workbook then then the one you passed the worksheet from?
The worksheetobject always has a workbook as a parent. So as long as you make sure you're calling the right worksheet when passing it on, you should be fine. So something like this should always work:
dim ws as Worksheet
set ws = ThisWorkbook.Sheets("Sheet1")
call option2(ws)

VBA Global Object Variables

I need some help with global variables. The code I have below needs to be accessed by a multiple Modules and a User form. My attempts to make them a global failed.
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open("Some.xlsm")
Set ws = wb.Worksheets("Some Sheet")
I have tried making a module to hold them but ran into a runtime error. My program works fine if I copy this code to each function that uses it but that becomes messy. I would like to set it up to just have to change the two lines in order to affect the entire file. When I made a new module it did not like Dim.
Edit:
Public wb As Workbook
Public ws As Worksheet
Public Sub modVariables()
Set wb = Workbooks.Open("V:\My Stuff\Templates\Work Order Batch Creation Template\Sandbox\WorkOrderDatabase.xlsm")
Set ws = wb.Worksheets("Description - Processes")
End Sub
Just declare them as public variables like the following:
Public wb As Workbook
Public ws As Worksheet
Thats how it should look like:

Global Variables for Worksheet Names

I need to declare global variables for worksheet names so that if a worksheet name needs to be changed, it can be changed in one spot. This will help to prevent a user from having to dig through each macro to find where each worksheet name was used.
Below is what I have for my global variables. When used this way you get the "Invalid Outside Procedure" for the each of the "Set" instances. I know that the "Set" needs to be inside a procedure but how can I avoid that so that I only have to change the name in one spot for all macros.
Option Explicit
'Global Variables
Dim WS As Worksheet
Dim VB As Worksheet
Dim DB As Worksheet
Dim ED As Worksheet
Dim OC As Worksheet
Dim SH As Worksheet
Dim SL As Worksheet
Set WS = Sheets("WorkSheet")
Set VB = Sheets("VBA Codes")
Set DB = Sheets("Dashboard")
Set ED = Sheets("Extra Details")
Set OC = Sheets("Occupancy")
Set SH = Sheets("Shrinkage")
Set SL = Sheets("SL Impact")
Just in case what I described above does not make sense. The following is an example:
Example: I changed the name of a worksheet from "Dashboard" to "Summary". To avoid changing all instances of "Dashboard" in the macros, I only need to change the global variable to "Summary".
Public workbook as workbooks
Public worksheet as worksheets
Have function init.
Function init()
Set workbook = thisworkbook
set worksheet = workbook.worksheets("sheet1")
End function
Call the init method once in the workbook open sub. The public object should be sustained for other use.
If you want to show the progress in dashboard follow this tutorial: https://youtu.be/zH8eleVVpm8

Can't assign value to field in custom class where field is object

I have my custom class for VBA. I create field where will keep reference of object Worksheet:
Private ws As Worksheet
write properties:
Property Get TargetWorksheet() As Worksheet
TargetWorksheet = ws
End Property
Property Set TargetWorksheet(ByRef newws As Worksheet)
ws = newws
End Property
but when I try assign value
Dim mylist As CustomPropertWS
mylist.TargetWorksheet = Sheet1
or
Set mylist.TargetWorksheet = Sheet1
macro always did throws an exception
"object variable or with block variable not set". How can I set fields with objects like it?
Thanks.
Make the following modifications to your class module:
CustomPropertWS Class Code
Private ws As Worksheet
Property Get TargetWorksheet() As Worksheet
Set TargetWorksheet = ws
End Property
Property Set TargetWorksheet(ByRef newws As Worksheet)
Set ws = newws
End Property
Your Sub, which calls the Class code:
TestClass Module Code
Option Explicit
Sub TestClass()
Dim mylist As CustomPropertWS
Set mylist = New CustomPropertWS ' initiate the new class
'Set mylist.TargetWorksheet = Worksheets("Sheet1")
Set mylist.TargetWorksheet = Sheet1 ' <-- this method passes the worksheet's codename
Debug.Print mylist.TargetWorksheet.Name ' just for DEBUG
End Sub

Resources