Syntax problem for excel object commands

pwhilgert

New Member
Joined
Mar 31, 2003
Messages
4
I'm new to VBA for Excel and I'm trying to use the Excel object model to determine if a Workbook contains a specific worksheet. I'm trying to use the Names property to get the list of worksheets so I can search the list.

However, I cannot get the syntax correct for this command to work.

I can get the Counts property to work:

Set OApp = CreateObject("Excel.Application")
OApp.Workbooks.Open "c:\myexcel.xls"
RS = OApp.ActiveWorkbook.Worksheets.Count
OApp.Quit

And have working code for inserting a column into a worksheet (the true goal of this whole task).

But..I can't get Names to work.

Thanks,

- Phillip
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
If you're working within Excel, you don't strictly have to create a new instance of Excel as you're doing in your code. However, in case that's what you want, I've included two example procedures, both of which should work OK. I think you're mistaking the Names collection, which is comprised of the defined names within a workbook with the Worksheet Name property.

Code:
Public Sub WorksheetNames()
Dim ws As Worksheet

Workbooks.Open "C:\My Documents\Book1.xls"

For Each ws In ActiveWorkbook.Worksheets
    MsgBox ws.Name
Next ws

End Sub

Public Sub WorksheetNames2()
Dim OApp As Application
Dim ws As Worksheet

Set OApp = CreateObject("Excel.Application")
OApp.Workbooks.Open "c:\My Documents\Book1.xls"
OApp.Visible = True

For Each ws In OApp.ActiveWorkbook.Worksheets
    MsgBox ws.Name
Next ws

End Sub
 
Upvote 0
Mudface,

thanks this gets me closer. I think I do want to use the Names collection. I need to run a script (outside of Excel) that looks a several different workbooks to see if they contain a specific worksheet.

The code you provided does work, but I can't seem to assign the ws.Name value into an array.

Thanks so much for your help.
 
Upvote 0
I'm not sure if you need to assign the worksheet names into an array (although you can do quite easily). Would something like this do the trick?

Code:
Public Sub WorksheetNames2()
Dim OApp As Application
Dim ws As Worksheet

Set OApp = CreateObject("Excel.Application")
OApp.Workbooks.Open "c:\My Documents\Book1.xls"
OApp.Visible = True

For Each ws In OApp.ActiveWorkbook.Worksheets
    If ws.Name = "Sheet2" Then 
' Replace "Sheet2" with the name of the sheet you're looking for
        MsgBox "Sheet Found" ' Do your stuff here
    End If
Next ws

End Sub
 
Upvote 0
The last mile

Mudface,

that'll definitely work, but can you help me with the last mile? :D

Now that I can find the sheet and determine if the column name exists in Row 1, I now need to be able to add the column name in row 1 if it does not exist. I'm guessing I have to figure out the last column populated in Row 1, and then insert the new column name in the first open column on row 1, but am struggling (again) with syntax.

Thanks for all the help.

- Phillip
 
Upvote 0
OK, you can reference the last filled column in a row by using

Range("IV1").End(xlToLeft).Offset(, 1)

this looks from cell IV1 until it finds a cell with an entry and then offsets it by one column to reference the first empty cell in that row. Take a look at the End property in the VBA help files for more, as there are various directions in which you could look (from IV1 to the left, A1 to the right etc).

The slightly amended code would look something like this

Code:
Public Sub WorksheetNames2()
Dim OApp As Application
Dim ws As Worksheet

Set OApp = CreateObject("Excel.Application")
OApp.Workbooks.Open "c:\My Documents\Book1.xls"

For Each ws In OApp.ActiveWorkbook.Worksheets
    If ws.Name = "Sheet2" Then
        ws.Range("IV1").End(xlToLeft).Offset(, 1).Value = "YourColumnName"
    End If
Next ws

OApp.Visible = True

End Sub
 
Upvote 0
Getting Closer

Mudface,

again, many thanks. However, I have uncovered part of my problem. I'm actually having to code this in VBScript not VBA. So..I have everything working, except the line that inserts the value. It doesn't error, but it doesn't seem to do anything.

It also doesn't seem to be saving the workbook when I tell it to save.

Here's the current version of my code:

Function WS_Search (Worksheet_Name, Column_Name, Column_Value)
Set OApp = CreateObject("Excel.Application")
'Set ws = OApp.ActiveWorkbook.Worksheets
OApp.Workbooks.Open "c:\myexcel.xls"
'OApp.visible = True
On Error Resume Next

For Each ws In OApp.ActiveWorkbook.Worksheets
if ws.Name = Worksheet_Name then
MsgBox ("Worksheet: " & ws.Name & " was found")
Search_Result = OApp.ActiveWorkbook.Worksheets(Worksheet_Name).Rows(1).Find(Column_Name).Column
If Search_Result = "" Then
msgbox ("column is missing and will be added: " & Column_Name)
'ws.Range("IV1").End(xlToLeft).Offset(, 1).Value = Column_Name
' OApp.ActiveWorkbook.Worksheets(Worksheet_Name).Range("IV1").End(xlToLeft).Offset(, 1).Value = Column_Name
Cell_Val = OApp.ActiveWorkbook.Worksheets(Worksheet_Name).Range("IV1").End(xlToLeft).Value
msgbox (Cell_Val)
else
msgbox ("Column already exists")
end if
end if
Next
Workbook.Save
OApp.Quit
End Function

Column_Value = "No"

Worksheet_Name = "Login"
Column_Name = "New_Col"
WS_Search Worksheet_Name, Column_Name, Column_Value

- Phillip
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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