workbooks.open - with or without ( )

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I understand that workbooks.open returns a workbook object. So it returns something and because of that, I thought it should be call using ( ) for example workbooks.open("abc.xlsx"), but the author of this link https://analysistabs.com/excel-vba/open-close-existing-workbook/
called workbooks.open function without using ( )
Any idea why? and why it works? Thank you
Code:
Sub Open_ExistingWorkbook()
    Workbooks.Open "C:\WorkbookName.xls"
    'OR
    'Workbooks.Open Filename:="C:\WorkbookName1.xls"
End Sub
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
If you use an = symbol
Code:
Set wb = Workbooks.Open(fileName)
You need the parentheses ()
but if you just
Code:
Workbooks.Open fileName
then you do not use the parentheses.

The reason is that the underlying application of VBA is written that way. Apparently, two different software engineers at two different times had different opinions of how it should be.

If you look further down in the ariticle you will see that the author did use the parentheses when using the = symbol.
 
Last edited:
Upvote 0
Just different ways of writing the code.....maybe you could have posted on their webpage and asked the very same question ??
I guess the same as adding a sheet can also be written differently

Code:
Sheets.Add(After:=Sheets(Sheets.Count))
activesheet.name="bla bla"

could also be

Code:
Sheets.Add.name="bla bla"
 
Upvote 0
Thanks once again for all your help. I tried the code below and I noticed excel accept "x=workbook.count()" and "x=workbook.count"
I thought since I am assigning the fuction to x then I must use ( ) but that is not the case. Thank you so much.

Code:
Sub xyz()
    Dim z As Integer
    Dim x As Integer
    x = Workbooks.Count()
    z = Workbooks.Count
    MsgBox z
    MsgBox x
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,545
Messages
6,120,128
Members
448,947
Latest member
test111

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top