Excel VBA To Set Variable To Worksheet - vba

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

Related

VBA: Set Workbook, and Activate Workbook Error

There is a simple task that I cannot figure out. I would like to Copy Value of wb1.ws1 and paste into wb2.ws2
This is what I have tried.
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = Sheet("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = Sheet("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
wb1.ws1.Range("A1").Copy wb2.ws2.Range("A1")
Why does this not work? It seems like I cannot select a range by specifying wb1.ws1.Range(XYZ)
It forces me to separate the lines into
wb1.Activate
ws1.Select
Range("A1").Copy
wb2.Activate
ws2.Select
Range("A1").Paste
Seems terribly inefficient but I don't know how to eliminate the Activate-Select nightmare
Once you set a worksheet the workbook parent is already defined. so declare the parent at the Set
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = wb1.WorkSheets("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = wb2.Worksheets("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
ws1.Range("A1").Copy ws2.Range("A1")

Looping through all files in a folder (encountering run time error '-2147221080 (800401a8)')

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!

Working with specific Worksheet and selecting range; runtime error 91

I am trying to grab a certain column in one worksheet and (eventually..no code written yet) copy it over to another worksheet in another workbook. I am getting a runtime error 91, and when I try to find a way around it, I will get a runtime error 1004.
Sub User_Rolee()
Dim UserRoleWkb As Workbook, ConfigWkb As Workbook, UserRoleWkst As Worksheet, ConfigWkst As Worksheet
Set UserRoleWkb = Workbooks.Open("C:\Users\clara\Desktop\S_User_Role_Map_TEMPLATE_V2_BLANK.xlsx")
Set ConfigWkb = ActiveWorkbook
'Set ConfigWkb = Workbooks.Open("C:\Users\clara\Desktop\Configuration Workbook - Paramaribo.xlsm")
Set UserRoleWkst = UserRoleWkb.Sheets("Users")
Set ConfigWkst = ConfigWkb.ActiveSheet
Dim rng, rnga As Range
With ConfigWkst
Set rng = .Columns(2).Find(What:="Procurement Agent")
Set rnga = .rng.Offset(1) 'runtime error 91
Set rngar = .Range(rnga, rnga.End(xlDown)).Select
End With
End Sub
Following my comments above, try the code below:
(see my comment in the line before last, you also never defined rngar anywhere).
Option Explicit
Sub User_Rolee()
Dim UserRoleWkb As Workbook, ConfigWkb As Workbook, UserRoleWkst As Worksheet, ConfigWkst As Worksheet
Dim rng As Range, rnga As Range, rngar As Range
Set UserRoleWkb = Workbooks.Open("C:\Users\clara\Desktop\S_User_Role_Map_TEMPLATE_V2_BLANK.xlsx")
Set ConfigWkb = ThisWorkbook
'Set ConfigWkb = Workbooks.Open("C:\Users\clara\Desktop\Configuration Workbook - Paramaribo.xlsm")
Set UserRoleWkst = UserRoleWkb.Sheets("Users")
Set ConfigWkst = ConfigWkb.Worksheets("Sheet1") '<-- modify "Sheet1" to your sheet's name
With ConfigWkst
Set rng = .Columns(2).Find(What:="Procurement Agent")
If Not rng Is Nothing Then '<-- find was successful
Set rnga = rng.Offset(1)
Set rngar = Range(rnga, rnga.End(xlDown))
rngar.Copy '<-- copy the Range
End If
End With
End Sub

Copy cell range between workbooks from same folder

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.

VBA dim ws as worksheets (not worksheet)

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