Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EXCEL NAMING

Status
Not open for further replies.

AdamWhitehouse

Programmer
Aug 7, 2000
34
GB
I am using the following code to open an Excel Workbook, but I want to either create a new "named" workbook or open one if the name already exists,
I am using the following code how can I adapt this for my needs?

Dim objXL As New Excel.Application
Dim wbXL As New Excel.Workbook
Dim wsXL As New Excel.Worksheet
Dim intRow As Integer ' counter
Dim intCol As Integer ' counter

If Not IsObject(objXL) Then
MsgBox "You need Microsoft Excel to use this function", _
vbExclamation, "Print to Excel"
Exit Sub
End If

On Error Resume Next

' open Excel
objXL.Visible = True
Set wbXL = objXL.Workbooks.Add
Set wsXL = objXL.ActiveSheet

wsxl.Name = "MYsheetName"

 
First, your declarations:
Code:
Dim objXL As Excel.Application
Dim wbXL As Excel.Workbook
Dim wsXL As Excel.Worksheet
To Start Excel:
Code:
Set objXL = New Excel.Application
objXL.WindowState = xlMaximized
objXL.Visible = True
Open or Start New Workbook:
Code:
Dim sFile As String
sFile = "C:\Temp\Test.xls"

If Dir(sFile)<>&quot;&quot; Then  'File Exists
  Set wbXL = xlApp.Workbooks.Open sFile
Else    'File Does Not Exist
  Set wbXL = xlApp.Workbooks.Add
End If
There are plenty of samples in this forum for manipulating Excel files from VB.

Hope this helps...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top