ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel VBA basics (https://www.excelbanter.com/excel-programming/313133-excel-vba-basics.html)

Philipp

Excel VBA basics
 
Hello,
I wonder why the following Excel VBA code does not work.

Sub test()
Dim myBook As Workbook
myBook = ActiveWorkbook
Debug.Print "the book name is" & myBook.Name
End Sub

Is it not possible to assign the reference to the workbook to a variable?
Thanks for answers Phil

Haldun Alay[_4_]

Excel VBA basics
 
Hi,
Try


.......
Set myBook = ActiveWorkbook
........


Kind Regards.
--
Haldun Alay
"Philipp" , haber iletisinde sunlari ...
Hello,
I wonder why the following Excel VBA code does not work.

Sub test()
Dim myBook As Workbook
myBook = ActiveWorkbook
Debug.Print "the book name is" & myBook.Name
End Sub

Is it not possible to assign the reference to the workbook to a variable?
Thanks for answers Phil

Andy Pope

Excel VBA basics
 
Hi,

You need to use the Set command as it is an object variable.

Set myBook = ActiveWorkbook

Cheers
Andy

Philipp wrote:

Hello,
I wonder why the following Excel VBA code does not work.

Sub test()
Dim myBook As Workbook
myBook = ActiveWorkbook
Debug.Print "the book name is" & myBook.Name
End Sub

Is it not possible to assign the reference to the workbook to a variable?
Thanks for answers Phil


--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

Philipp

Excel VBA basics
 
Haldun Alay wrote:

Hi,
Try


......
Set myBook = ActiveWorkbook
.......


OK that worked. Thank you for your prompt answers. Phil


All times are GMT +1. The time now is 11:17 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com