Home » excel » Properties of a cell in Excel (used in VBScript)?

Properties of a cell in Excel (used in VBScript)?

Posted by: admin May 14, 2020 Leave a comment

Questions:

What properties/attributes does a cell have in Excel? I searched online but the best thing I could find was this. However, this doesn’t include a list attributes of a cell like .Font, .Value, etc.

I’m trying to find a way to get the hyperlink located in a cell in VBScript. The value of the cell is different from the link. I tried Cells(1, 1).Range("A1").Hyperlinks(1).Address and just Cells(1,1).Hyperlink, but neither are working.

Also, is there any detailed documentation for Excel use in VBScript? I’ve used the MSDN and W3Schools documentation, but neither of them have any Excel-specific information. I’ve found that in many places, Excel use in VBA or VB is different from in VBScript.

Thank you so much!

How to&Answers:

As per the documentation, Cells is a Range object. You can either refer to a specific cell (item) or a range of cells, but the individual cell is just a specific instance of a Range object.

Therefore, you should refer to the list of Range properties.

With regard to getting the hyperlink in a cell. If the hyperlink is in A1, you can use

Worksheets(1).Cells(1, 1).Hyperlinks(1).Name

Here is additional documentation about Hyperlinks. The documentation looks sparse, but if you click the arrows for “Hyperlink Object” in the left-hand menu, you’ll see all the properties and methods.

Answer:

I think part of what you are missing is how to gain access to Excel objects within VBScript. The way you gain access to Excel objects in VbScript is to call the following in to your script

Set myxlObject = CreateObject("Excel.Application")
Set myxlApplication = myxlObject.Application

With that you will be able to do practically anything that you have done in VBA but now in vbScript and then you will need to call that application object to gain access to the other objects. For example:

Set myWorkBook = myxlApplication.Workbooks.Open( "C:\Wherever\Whatever.xlsx" )

For more information about the properties and methods that you can access via this “Excel.Application” object see this page: Application Object (Excel)

The following script will output the names and addresses of hyperlinks on a worksheet in a particular range to the console.

Set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")

Set myxlObject = CreateObject("Excel.Application")
Set myxlApplication = myxlObject.Application
Set myWorkBook = myxlApplication.Workbooks.Open( curDir & "048353.xlsx" ) 'Change to the actual workbook that has the Hyperlinks
Set myWorkSheet = myWorkBook.Worksheets(1)

myxlApplication.Visible = False

Set rng = myWorkSheet.Range("A2:A3") 'Change to the actual range that has the Hyperlinks

For Each row In rng.Rows

    myLinkName = row.Cells(1, 1).Hyperlinks(1).Name
    myLinkURL = row.Cells(1, 1).Hyperlinks(1).Address

    wscript.echo myLinkName & " " & myLinkURL

Next

Run this using cscript

Possibly a more practical use of this script is to output the results to a file as such:

Set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")

Set myLinksFile = fso.OpenTextFile( curDir & "\mylinks.txt", 8, True)

Set myxlObject = CreateObject("Excel.Application")
Set myxlApplication = myxlObject.Application
Set myWorkBook = myxlApplication.Workbooks.Open( curDir & "048353.xlsx" ) 'Change to the actual workbook that has the Hyperlinks
Set myWorkSheet = myWorkBook.Worksheets(1)

myxlApplication.Visible = False

Set rng = myWorkSheet.Range("A2:A3") 'Change to the actual range that has the Hyperlinks

For Each row In rng.Rows

    myLinkName = row.Cells(1, 1).Hyperlinks(1).Name
    myLinkURL = row.Cells(1, 1).Hyperlinks(1).Address

    mylinksFile.WriteLine myLinkName & ", " & myLinkURL

Next

mylinksFile.Close