OK so, I know I can do this:
Dim ws as worksheet
Set ws = thisworkbook.worksheets("Sheet1")
and then do my fancy stuff with the ws worksheet object
I also know I can Dim wss as worksheets and that using worksheets("Sheet1") returns the worksheet object. So why doesn't the following work?
Dim wss as worksheets
Dim ws as worksheet
Set wss = thisworkbook.worksheets
Set ws = wss("Sheet1")
I've also tried:
Dim wss as worksheets
Dim ws as worksheet
Set ws = thisworkbook.wss("Sheet1")
but the latter just looks like I'm trying to rename/shorten "worksheets" which seems totally wrong. I'm trying to get the worksheets of a workbook in to one worksheets object called wss. This is more about trying to understand the heirachy than anything but for functional purposes I'm trying to get wss to encompass all worksheets from workbook x so I could just do ws = wss(1) instead of saying set ws = wb.worksheets(1)
Is that even possible or am I misunderstanding the worksheets/ worksheet relationship?
you must declare wss as a Sheets object
Dim wss As Sheets
Dim ws As Worksheet
Set wss = ThisWorkbook.Worksheets
Set ws = wss("Sheet1")
this is because Worksheets property of Workbook object returns a Sheets collection, i.e. a collection that contains both Worksheets and Charts object of the workbook
Should you need a collection of your Workbook Worksheets only (not Charts) to be called like ws = wss(1) or the likes then you could adopt the following workaround with Collection object
Option Explicit
Sub main()
Dim wss As Collection
Dim ws As Worksheet
Set wss = GetWorkSheets
Set ws = wss("Sheet1")
Set ws = wss(1)
End Sub
Function GetWorkSheets() As Collection
Dim wss As New Collection
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
wss.add ws, ws.Name
Next ws
Set GetWorkSheets = wss
End Function
Related
I am running a code that aims to pull in data from all workbooks within a specific folder. I have written the code for the loop separate from the code for the pulling in of data.
Sub FixedIncomeLoop()
Dim file As Variant
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim folderpath As String
Dim ws As Worksheet
Dim ws2 As Worksheet
Set wb1 = ActiveWorkbook
Set ws = ActiveSheet
folderpath = "C:\Users\Wei Hong\Documents\Trade Tickets\"
file = Dir(folderpath)
While (file <> "")
Set wb2 = Workbooks.Open(file)
Set ws2 = wb2.Worksheets("Trade Ticket")
Call FixedIncome(wb1, ws, ws2)
wb2.Close SaveChanges:=False
Set wb2 = Nothing
Wend
End Sub
Private Sub FixedIncome(wb1 As Workbook, ws As Worksheet, ws2 As Worksheet)
Dim ws3 As Worksheet
Set ws3 = wb1.Worksheets("Constants") <-- facing error
ws.Activate
...
End Sub
I am facing the runtime error above. Not sure why!
I am trying to set a Worksheet variable to the Activesheet but I am getting an error of
Object variable or with block variable not set
This is my syntax - what is the appropriate way of doing this?
Sub TestIt()
Dim ws2 As Worksheet, ws1 As Worksheet
ws2 = ActiveWorkbook.ActiveSheet
ws1 = "Belgium"
Debug.Print ws2
Debug.Print ws1
End Sub
1- You need to use the Set keyword to assign object variables.
2- You cannot assign a sheet variable to a string directly, but you need to index through the Worksheets collection
3- You cannot Debug.Print a worksheet, but only its name or some cell inside
Sub TestIt()
Dim ws2 As Worksheet, ws1 As Worksheet
Set ws2 = ActiveWorkbook.ActiveSheet
Set ws1 = ThisWorkbook.Worksheets("Belgium")
Debug.Print ws2.Name
Debug.Print ws1.Cells(1,1).Value
End Sub
I'm trying to assign the code name of a worksheet to a vriable.
Some times it gets the code name correctly, and sometimes it doesn't, the variable stays null.
Dim sCodeName As String
sCodeName = Worksheets(atar).CodeName
atar is a variable that contains the worksheet name.
When i stop the code running, and coninue in debug mode, it works fine.
What can be the reason?
Worksheets without qualification refers to same as ActiveWorkbook, perhaps you're assuming too much about which workbook is active in some instances.
It is recommended to fully qualify Worksheets. So get a reference to a Workbook object , say wbFoo and then use sCodeName = wbFoo.Worksheets(atar).CodeName
You could try something like the code below to verify that atar does exist in one of the worksheet names in ThisWorkbook.
Option Explicit
Sub GetWorksheetCodeName()
Dim sCodeName As String
Dim atar As String
Dim Sht As Worksheet
'atar = "Sheet3" '<-- for tests only
' loop through all worksheets in ThisWorkbook
For Each Sht In ThisWorkbook.Worksheets
If Sht.Name Like atar Then
sCodeName = Worksheets(atar).CodeName
Exit For
End If
Next Sht
End Sub
CREATE MULTIPLE WORKSHEET BUT SOMETIMES NOT WORK WITH MACRO FROM WORKSHEET ONLY WORK WITH VBA CODE RUN
Sub WSCreate()
Dim WSA As String
Dim WSB As String
Dim WS As Worksheet
WSA = "SheetA"
WSB = "SheetB"
Application.DisplayAlerts = False
On Error Resume Next
'DELETE MULTIPLE WORKSHEETS (IF Already Exist)
Set WS = Nothing
Set WS = Sheets(WSA)
WS.Delete
Set WS = Nothing
Set WS = Sheets(WSB)
WS.Delete
Set WS = Nothing
'CREATE MULTIPLE WORKSHEETS
With Application.ThisWorkbook
.Worksheets.Add.Name = WSA 'WSA = SheetA
.VBProject.VBComponents(Worksheets(WSA).CodeName).Name = WSA
.Worksheets.Add.Name = WSB 'WSB = SheetB
.VBProject.VBComponents(Worksheets(WSB).CodeName).Name = WSB
End With
If Err <> 0 Then Exit Sub
End Sub
I currently have:
Sub Ranger()
Dim rng As Range
Dim WB1 As Workbook, WB2 As Workbook, ActiveWB As Workbook
Dim WS1 As Worksheet, WS2 As Worksheet
Dim FName As String
FName = "General Text"
Set WB1 = ThisWorkbook
Set WS1 = WB1.Sheets("Sheet1")
Set WB2 = Workbooks.Open(FileName:=FName)
Set WS2 = WB2.Sheets(1)
With WS2
Set rng = .Range(Range("A1"), Range("A5"))
End With
With WS1
rng.Copy .Cells(1, 1)
End With
WB2.Close
End Sub
Which aims to copy the range A1:A5 in the newly opened workbook into the original workbook (ThisWorkbook). It currently opens the second workbook but does not copy anything into the first workbook. There are also no errors and I would like to avoid using specific names in setting WB1/WB2 as WB2 could be either .xls or .xlsx
Try this, it works for me:
Sub Ranger()
Dim rng As Range
Dim WB2 As Workbook
Dim FName As String
FName = "D:\Tuchanka\Temp\pelda.xlsx"
Set WB2 = Workbooks.Open(Filename:=FName)
ThisWorkbook.Worksheets(1).Range("A1:A5").Value = WB2.Worksheets(1).Range("A1:A5").Value
WB2.Close
End Sub
Using variables and with statements is pointless in your situation, as instead of making your code simpler and easier to read, they just add a lot of gibberish and make your code seem way too complex. Only use these if you need them.
I am trying to work with multiple workbooks, one of them being a shared workbook.
As far as my testing has gone, it looks like I cannot set a workbook/worksheet variable to that of the shared workboook.
Is there a way that I haven't figured out, or is this impossible without first unsharing?
Below are the methods I have tested (while having the shared workbook as the active workbook):
Sub TestAssign()
Dim wb As Workbook
Dim ws As Worksheet
Dim r As Range
With Workbooks("testSHARE.xlsb")
Set ws = .Sheets(1)
End With
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")
Set ws = wb.Sheets(1)
Set ws = ActiveSheet
Set r = Range("A1:P5")
End Sub
You can modify shared workbook if the shared workbook is set for corraboration.
Setup procedure is explained at http://office.microsoft.com/en-us/excel-help/use-a-shared-workbook-to-collaborate-HP010096833.aspx .