In this project I'm developing, I created many Subs that all use the same three workbooks. But is there a better way to use these workbooks' sheets without having to write them down everytime I create a new Sub? I tried returning it in a function but it does not work.
Function defineWorksheet() As Worksheet
Dim wk_Base_18 As Excel.Workbook
Dim ws_18 As Excel.Worksheet
Set wk_Base_18 = Excel.Workbooks("2019.01.03.xlsb")
Set ws_18 = wk_Base_18.Worksheets("Planilha1")
ws_18
End Function
error 91
Yes, you can declare them as a global variable.
Public ws1 As Worksheet
Then instantiate the global variable during the application load event of the excel application
Private Sub Workbook_Open()
Set ws1 = ThisWorkbook.Sheets("YourSheetName")
End Sub
And now, you can refer to it via the variable, eg.
Dim x as Integer: x = ws1.Range("B5")
The general idea is sound, you just need to create one function per object:
Function wk_Base_18() as workbook
Set wk_Base_18 = Excel.Workbooks("2019.01.03.xlsb")
End Function
Function ws_18() as worksheet
Set ws_18 = wk_Base_18.Worksheets("Planilha1")
End Function
Then whenever you go to use the variable ws_18 or wk_Base_18, you will be calling these same functions.
My issue involves
My is simply why can't I simplify my code using wb.ws.range("C6").value = to something** to change a value.
I keep getting error code 438.
Thank you in advance for any help you can provide.
private sub reset_input_sheet()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = Worksheets("CDR_Input")
'perf treatment type
wb.ws.Range("C6").value = "Multi-Stage Fracture"
end sub
I imagine you need this. You don't need to reference the workbook once the sheet variable includes a reference.
Private Sub reset_input_sheet()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("CDR_Input")
'perf treatment type
ws.Range("C6").Value = "Multi-Stage Fracture"
End Sub
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
I know this might come off as a trivial question, but I can't seem to declare a workbook or a worksheet as a variable in VBA. I have the following code, but I can't figure out what I am doing wrong, it should be straight forward. Normally I don't have any problems declaring variables such as Dim i As Integer etc.
sub kl()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheet("name")
wb.ws.Select
End Sub
When I run the above code, I receive a type missmatch error.
Use Sheets rather than Sheet and activate them sequentially:
Sub kl()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = Sheets("Sheet1")
wb.Activate
ws.Select
End Sub
If the worksheet you want to retrieve exists at compile-time in ThisWorkbook (i.e. the workbook that contains the VBA code you're looking at), then the simplest and most consistently reliable way to refer to that Worksheet object is to use its code name:
Debug.Print Sheet1.Range("A1").Value
You can set the code name to anything you need (as long as it's a valid VBA identifier), independently of its "tab name" (which the user can modify at any time), by changing the (Name) property in the Properties toolwindow (F4):
The Name property refers to the "tab name" that the user can change on a whim; the (Name) property refers to the code name of the worksheet, and the user can't change it without accessing the Visual Basic Editor.
VBA uses this code name to automatically declare a global-scope Worksheet object variable that your code gets to use anywhere to refer to that sheet, for free.
In other words, if the sheet exists in ThisWorkbook at compile-time, there's never a need to declare a variable for it - the variable is already there!
If the worksheet is created at run-time (inside ThisWorkbook or not), then you need to declare & assign a Worksheet variable for it.
Use the Worksheets property of a Workbook object to retrieve it:
Dim wb As Workbook
Set wb = Application.Workbooks.Open(path)
Dim ws As Worksheet
Set ws = wb.Worksheets(nameOrIndex)
Important notes...
Both the name and index of a worksheet can easily be modified by the user (accidentally or not), unless workbook structure is protected. If workbook isn't protected, you simply cannot assume that the name or index alone will give you the specific worksheet you're after - it's always a good idea to validate the format of the sheet (e.g. verify that cell A1 contains some specific text, or that there's a table with a specific name, that contains some specific column headings).
Using the Sheets collection contains Worksheet objects, but can also contain Chart instances, and a half-dozen more legacy sheet types that are not worksheets. Assigning a Worksheet reference from whatever Sheets(nameOrIndex) returns, risks throwing a type mismatch run-time error for that reason.
Not qualifying the Worksheets collection is an implicit ActiveWorkbook reference - meaning the Worksheets collection is pulling from whatever workbook is active at the moment the instruction is executing. Such implicit references make the code frail and bug-prone, especially if the user can navigate and interact with the Excel UI while code is running.
Unless you mean to activate a specific sheet, you never need to call ws.Activate in order to do 99% of what you want to do with a worksheet. Just use your ws variable instead.
Third solution:
I would set ws to a sheet of workbook wb as the use of Sheet("name") always refers to the active workbook, which might change as your code develops.
sub kl()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
'be aware as this might produce an error, if Shet "name" does not exist
Set ws = wb.Sheets("name")
' if wb is other than the active workbook
wb.activate
ws.Select
End Sub
Just coming across the same problem.
What you need to do is to declare ws as Object
Also it should be:
Set ws = wb.Sheets("Sheet1")
And should not be:
Set ws = Sheet("Sheet1")
The code below are working to me.
sub kl()
Dim wb As Workbook
Dim ws As Object
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
MsgBox ws.Name
End Sub
Try changing the name of the variable as sometimes it clashes with other modules/subs
Dim Workbk As Workbook
Dim Worksh As Worksheet
But also, try
Set ws = wb.Sheets("name")
I can't remember if it works with Sheet
to your surprise, you do need to declare variable for workbook and worksheet in excel 2007 or later version. Just add single line expression.
Sub kl()
Set ws = ThisWorkbook.Sheets("name")
ws.select
End Sub
Remove everything else and enjoy.
But why to select a sheet? selection of sheets is now old fashioned for calculation and manipulation.
Just add formula like this
Sub kl()
Set ws = ThisWorkbook.Sheets("name")
ws.range("cell reference").formula = "your formula"
'OR in case you are using copy paste formula, just use 'insert or formula method instead of ActiveSheet.paste e.g.:
ws.range("your cell").formula
'or
ws.colums("your col: one col e.g. "A:A").insert
'if you need to clear the previous value, just add the following above insert line
ws.columns("your column").delete
End Sub
I had the same issue. I used Worksheet instead of Worksheets and it was resolved. Not sure what the difference is between them.
Dim ws as Object
Set ws = Worksheets("name")
when declaring the worksheet as worksheet instead of an ojbect I had issues working with OptionButtons (Active X) in this worksheet (I guess the same will be with any Active-X element. When declared as object everything works fine.
Lots of answers above! here is my take:
Sub kl()
Dim wb As Workbook
Dim ws As Worksheet
Set ws = Sheets("name")
Set wb = ThisWorkbook
With ws
.Select
End With
End Sub
your first (perhaps accidental) mistake as we have all mentioned is "Sheet"... should be "Sheets"
The with block is useful because if you set wb to anything other than the current workbook, it will ececute properly
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