Dear colleagues,
Since my last EXCEL VBA application passed two years and now I started the whole new project. I did already a lot of works and now I wanted to set some variables to be set on excel opening. So this is what I did: the userform is called "CACalc" and at the beginning I'm declaring variables:
Option Explicit
Public wb As Workbook
Public taryfy_sheet As Worksheet
(... more ...)
I would like to have those two variables already set when the excel file is opened. I created then this method in the "ThisWorkbook" object:
Private Sub Workbook_Open()
CACalc.wb = (ActiveWorkbook.Path & "\" & ActiveWorkbook.Name) ' <-- error 91
CACalc.taryfy_sheet = wb.Worksheets("Tariffs")
CACalc.Show
End Sub
When I run the project I receive the "Error 91, Object Variable or with block not set " in the shown line of above code. This can be solved if I change the variable "wb" type to "String", but I'd like to have it as "Workbook". What I'm missing? The goal is to use those two variables in the userForm code to get the data from the excel sheet:
Dim Number as Integer
(...)
' Number = Workbooks(wb).Worksheets(taryfy_sheet).Range("H28") ' <--- this caused the ERROR 9, when 'wb' and 'taryfy_sheet' were string with correct values
Number = taryfy_sheet.Range("H28") '<--- this is the goal of the subject
And by the way: I get the error 9 in above code although the variables were correct . When I enter the object "Workbook(wb)." after pressing CTRL + SPACE I get the list of possible methods and object to select. However whatever I select "Worksheets" or "Sheets" followed by the index (e.g. Sheets(2) ) after the next '.' I don't have a list with other methods and objects. I was expecting "Range" or "Cell" available but I get nothing. I will appreciate you help.
Best regards,
Michal
Bookmarks