How to use the SET statement
The SET statement allows you to save an object reference to a variable, the image above demonstrates a macro that assigns a range reference to a range object.
For example, the DIM statement (among others) only declare a variable but it will be empty, nothing is referred until you assign an object using the SET statement.
What is an Excel object? It can be a chart, worksheet, a cell range or a shape among other things basically. Each object has usually a number of properties that you may change, for example, a worksheet may be hidden by changing the visible property to False.
The following macro is used in the above example.
'Name macro Sub Macro1() 'Declare variable Dim rng As Range 'Assign object Set rng = Worksheets("Sheet1").Range("B2:C9") 'Show object address MsgBox rng.Address End Sub
Excel Statement Syntax
Set objectvar = {[ New ] objectexpression | Nothing }
Syntax Explained
Excel Name | Description |
objectvar | The name of the object variable. |
New | Optional. Is used to create an object. |
objectexpression | A name of an object or another object variable with the same object type. |
Nothing | Deletes object. Recommended if an object holds a lot of memory that you don't need anymore. |
Comments
How can I create a new worksheet and assign it to an object variable?
The following macro creates a new worksheet and then shows the worksheet name using the assigned object.
Sub Macro2() Set obj = Sheets.Add MsgBox obj.Name End Sub
SET statement examples
This article demonstrates how to add or remove a value in a regular drop down list based on a list […]
This article demonstrates how to place values automatically to a table based on two conditions using a short macro. Cell […]
In this article I will demonstrate how to quickly change chart data range utilizing a combobox (drop-down list). The above […]
The image above shows a chart populated with data from an Excel defined Table. The worksheet contains event code that […]
The image above demonstrates a user defined function that merges up to 255 cell ranges and removes blanks. I will also […]
Today I would like to share a macro that compares the content in two different folders and their subfolders. It […]
Question: I have multiple worksheets in a workbook. Each worksheets is project specific. Each worksheet contains almost identical format. The […]
I will in this article demonstrate a macro that automatically opens all workbooks in a folder and subfolders, one by […]
This article demonstrates several VBA macros, they will save you time if you have lots of worksheets. The first macro […]
This article demonstrates a User Defined Function (UDF) that counts how many times a string exists in a cell value […]
I will in this article demonstrate a macro that counts how many times a specific text string is found in […]
It can sometimes be helpful having a large cell value in a comment. You can then easily hover over cell […]
The macro demonstrated above creates hyperlinks to all worksheets in the current worksheet. You will then be able to quickly […]
This article demonstrates a macro that inserts new worksheets based on names in a cell range. The cell range may […]
In a previos post:Excel vba: Save invoice data we added/copied data between sheets. This post describes how to overwrite existing […]
In this article, I am going to demonstrate a simple workbook where you can create or delete projects and add […]
This article demonstrates a User Defined Function that allows you to extract cell references from a formula in a given […]
This article demonstrates a macro that returns cell references for cell ranges populated with values on a worksheet. Jinesh asks: […]
Picture above shows cheap chinese IoT modules: a temp sensor and an esp8266-01 (25 mm x 15 mm) Thingspeak […]
In this post I am going to demonstrate how to quickly apply a filter to a table. I am using […]
This is a follow up to my last post Compare file names in two different folder locations and their subfolders, the obvious […]
This article describes different ways to locate literal or hardcoded values in formulas. The image above shows the result from […]
This article describes a macro that hides specific columns automatically based on values in two given cells. I am also […]
This post describes how to add a new custom-built item to the shortcut menu in Excel, when you press with right […]
This article demonstrates a formula and a VBA macro that returns every n-th row from a given cell range. The […]
The image above demonstrates a macro linked to a button. Press with left mouse button on the button and the […]
If you try to copy multiple cell ranges on a worksheet that don't have the same number of rows or […]
Did you know that you can select all cells containing comments in the current sheet? Press F5, press with left […]
A dialog box is an excellent alternative to a userform, they are built-in to VBA and can save you time […]
The FIX function removes the decimals from the argument. Excel Function VBA Syntax Fix(number) Arguments number Required. Is a Double or […]
The INT function removes the decimals if the numeric value is above 0 (zero) and returns a negative integer less than […]
The Range.Find method returns a range object for the first cell that matches the lookup value. Macro used in workbook above The […]
This article demonstrates how to insert and use a scroll bar (Form Control) in Excel. It allows the user to […]
The SET statement allows you to save an object reference to a variable, the image above demonstrates a macro that […]
The SGN function returns an integer that shows the sign of the number. Argument SGN returns Number greater than 0 (zero). […]
The image above shows an array formula in cell D6 that extracts missing numbers i cell range B3:B7, the lower […]
The macro in this blog post lists all hyperlinks in a worksheet. It also lists all words beginning with "http" […]
In this post, I am going to demonstrate how to automatically create a new sheet in the current workbook and […]
The following macro inserts a new sheet to your workbook and lists all Excel defined Tables and corresponding Table headers […]
Table of Contents List files in a folder and create hyperlinks (VBA) How to navigate quickly in a complex workbook […]
This article demonstrates how to locate a shape in Excel programmatically based on the value stored in the shape. The […]
This article demonstrates how to move a shape, a black arrow in this case, however, you can use whatever shape […]
Today I will share a To-do list excel template with you. You can add text to the sheet and an […]
To be able to use a Pivot Table the source data you have must be arranged in way that a […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
In this tutorial I am going to explain how to: Create a combo box (form control) Filter unique values and […]
In this post I am going to demonstrate two things: How to populate a combobox based on column headers from […]
Excel defined Tables, introduced in Excel 2007, sort, filter and organize data any way you like. You can also format […]
This post demonstrates how to: Insert a button to your worksheet Assign a macro to the button Create a basic […]
This article demonstrates a macro that allows you to rearrange and distribute concatenated values across multiple rows in order to […]
This article demonstartes different techniques on how to take a screenshot of your worksheet. You can do that by simply […]
In this post I am going to rearrange values from a list into unique columns. Before: After: The code Get […]
Sean asks: Sheet1A B C D 8 Country Europe 9 Lights 100 10 Type A 200 11 12 Country USA […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
Today I'll show you how to search all Excel workbooks with file extensions xls, xlsx and xlsm in a given folder for a […]
This article demonstrates a macro that allows you to search for a text string in multiple worksheets and workbooks located […]
The image above demonstrates a user-defined function in cell range B6:D7 that allows you to search a folder and subfolders […]
This article demonstrates a macro that automatically applies a filter to an Excel defined Table based on the result from […]
This post demonstrates a macro that automatically selects cell A1 on each sheet right before you close a workbook. The […]
This article demonstrates a macro and a formula that allows you to sort delimited data in a cell or cell […]
This article demonstrates how to sort a specific column in an Excel defined Table based on event code. The event […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
This article describes a User Defined Function that lookups values in multiple cross reference tables based on two conditions. A […]
The VBA macro demonstrated in this article lets you unzip all zip files in a folder to a destination folder […]
This article explains how to check if Excel files in a given folder are password protected. The image above shows […]
What's on this page Copy a file Copy and rename a file Rename a file List files in a folder […]
This blog post shows you how to manipulate List Boxes (form controls) manually and with VBA code. The list box […]
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form