简体   繁体   中英

excel vba: creating a range without referencing a sheet

I would like to know if I can save a generic range, without it having a sheet name attached to it?

Lets say that my program creates a sheet for every day of the week, and then makes headings for every sheet. I want to give it a few ranges, and it must merge and add different headings to those ranges in EVERY sheet. So the headings of every sheet looks the same.

I have the following range for example:

...
Set rowTwoHeadingKiloRange = Range(Cells(2, 4), Cells(2, 8)) 
Set rowTwoHeadingUnitRange = Range(Cells(2, 10), Cells(2, 14)) 
...

Now when I try to pass this range in a dictionary to every sheet that gets created, I find that I have undesired results as some of the headings gets created on sheets where they do not belong (And I think that is because when the range is created, it attaches to the active sheet at that moment - which may be different from time to time).

So now I basically have a function that looks something like this...

Public Function colmHeadingsAndSpacing(sheetName)
    With Worksheets(sheetName)
        ...
        Set rowTwoHeadingKiloRange = Range(Cells(2, 4), Cells(2, 8)) 
        Set rowTwoHeadingUnitRange = Range(Cells(2, 10), Cells(2, 14)) 
        ...
    End with
End Function

... and I run the function every time I create a sheet, with the name of the sheet that was just created given through. But this runs the function 7 times, when I use the same data (range) every time. Also I feel that it is not working properly as well (I still get strange reactions - ranges ending up in wrong sheets).

Second question, is there a way to find out on a range, what sheet is "attached" to the range. Something like: msgbox rowTwoHeadingKiloRange.worksheets.name which will give the result of Sunday

You were close. To attach your Range and Cell to the With statement you need to use a full-stop . before the keyword. Like this:

Public Function colmHeadingsAndSpacing(sheetName)
    With ThisWorkbook.Worksheets(sheetName)
        ...
        Set rowTwoHeadingKiloRange = .Range(.Cells(2, 4), .Cells(2, 8)) 
        Set rowTwoHeadingUnitRange = .Range(.Cells(2, 10), .Cells(2, 14)) 
        ...
    End with
End Function

This is good practice to qualify every range reference with it's attached worksheet. I went one step further included a workbook reference ( ThisWorkbook ). Now it's fully qualified.

For the second question -- try MsgBox rowTwoHeadingKiloRange.Parent.Name to get the name of the worksheet. It's usually better to start with the worksheet name rather than work back to it though.

A range is a reference of a (mostly rectangular) area on a sheet. So referring to a range without defining a sheet like Set r = Range ("B2:F10") is exactly the same as Set r = Activeworkbook.Activesheet.Range ("B2:F10") . So for more professional purposes VBA offers you the flexibility in the following way:

Dim wb As Workbook
Dim ws As Worksheet

Set wb = Workbooks.Add   ' open new xlsx file
Set ws = Activesheet OR 
Set ws = wb.Activesheet OR 
Set ws = wb.Sheets(1)
' do something else here and later when neither wb nor ws is active, you can
Set r = wb.ws.Range("B5:G22") OR 
Set r = ws.Range("B5:G22")

An addition: for producing a number of sheets of the same format, you may consider using templates either. Then you need to fill in only the differences programatically. Less programming, easier maintenance :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM