Yes. It goes like this: i select a value from a list box and then click on the button near the list box. The button must open then a specific sheet in a workbook.
I did a code that allow you to select multiple values( 5 in my case) from a list box and
put them in a string with these values . I dont know if it helps you out.
and pass the name of the workbook and the worksheet to the function to tell it which one to open and which sheet to set the focus to:
Code:
Function LoadExcelSpreadsheet(xlfilename,xlSheetName)
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim iRow As Integer
Dim iCol As Integer
Dim iLastRow
Dim tDate As String
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xlfilename)
Set oSheet = oBook.Worksheets(xlSheetName)
...Rest of your code here
Where xlFileName = the name of the Workbook
Where xlSheetName = the name of the Worksheet
To employ this on the OnClick of your button apply
StrWorkbook and StrWorkSheet are obtained from your controls on the form
You may want to check if the actual workbook is open prior to attempting to open it as you will get errors appearing:
Copy this function into a module.
Code:
Function IsXLBookOpen(strName As String) As Boolean
'Function designed to test if a specific Excel
'workbook is open or not.
Dim i As Long, XLAppFx As Excel.Application, NotOpen As Boolean
'Find/create an Excel instance
On Error Resume Next
Set XLAppFx = GetObject(, "Excel.Application")
If Err.Number = 429 Then
NotOpen = True
Set XLAppFx = CreateObject("Excel.Application")
Err.Clear
End If
'Loop through all open workbooks in such instance
For i = XLAppFx.Workbooks.Count To 1 Step -1
If XLAppFx.Workbooks(i).Name = strName Then Exit For
Next i
'Set all to False
IsXLBookOpen = False
'Perform check to see if name was found
If i <> 0 Then IsXLBookOpen = True
'Close if was closed
If NotOpen Then XLAppFx.Quit
'Release the instance
Set XLAppFx = Nothing
End Function
well i did almost the same thing. the only difference is that mine opens a report instead of a sheet.
i thin k u can replace the sheet name by Me.List5.Selected where list5 is ur list box. i dont know if that works
and pass the name of the workbook and the worksheet to the function to tell it which one to open and which sheet to set the focus to:
Code:
Function LoadExcelSpreadsheet(xlfilename,xlSheetName)
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim iRow As Integer
Dim iCol As Integer
Dim iLastRow
Dim tDate As String
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(xlfilename)
Set oSheet = oBook.Worksheets(xlSheetName)
...Rest of your code here
Where xlFileName = the name of the Workbook
Where xlSheetName = the name of the Worksheet
To employ this on the OnClick of your button apply
StrWorkbook and StrWorkSheet are obtained from your controls on the form
You may want to check if the actual workbook is open prior to attempting to open it as you will get errors appearing:
Copy this function into a module.
Code:
Function IsXLBookOpen(strName As String) As Boolean
'Function designed to test if a specific Excel
'workbook is open or not.
Dim i As Long, XLAppFx As Excel.Application, NotOpen As Boolean
'Find/create an Excel instance
On Error Resume Next
Set XLAppFx = GetObject(, "Excel.Application")
If Err.Number = 429 Then
NotOpen = True
Set XLAppFx = CreateObject("Excel.Application")
Err.Clear
End If
'Loop through all open workbooks in such instance
For i = XLAppFx.Workbooks.Count To 1 Step -1
If XLAppFx.Workbooks(i).Name = strName Then Exit For
Next i
'Set all to False
IsXLBookOpen = False
'Perform check to see if name was found
If i <> 0 Then IsXLBookOpen = True
'Close if was closed
If NotOpen Then XLAppFx.Quit
'Release the instance
Set XLAppFx = Nothing
End Function
Thanks for your help CodeMaster but.. this wasn't where i was looking for. I camed to the point that i can open a specific worksheet in a specific workbook. But the issue is this:
I have 10 citynames in a list box and a workbook with 10 worksheets. Each worksheet has the same name of the cityname, listed in the list box. So i have on my form this list box and i want to make a button which can open workbook with the specific worksheet. And this is my problem It must be something dynamic thing.
I have a workbook name citynames.xls
When i select a city out of the list box and i hit a button, i want to open the worksheet that has the same name as the city.
Example:
I select Stockholm in the list box and i click on the button near it.
The button opens in the workbook the worksheet Stockholm.
The same for Tokio, for Madrid etc.
The worksheets i will normally add in the workbook and a new city i will normally add in the database of Access
So I have highlighted Stockholm from my listbox
I then click a button next to the list box
The OnClick event recognsis the name stockholm and opens the workbook citynames.xls
So I have highlighted Stockholm from my listbox
I then click a button next to the list box
The OnClick event recognsis the name stockholm and opens the workbook citynames.xls
The worksheet names Stockholm is selected
Exacto mundo. That is what i am trying to do. It opens the workbook citynames.xls and opens the worksheet Stockholm.
Thanks for your help CodeMaster but.. this wasn't where i was looking for. I camed to the point that i can open a specific worksheet in a specific workbook. But the issue is this:
I have 10 citynames in a list box and a workbook with 10 worksheets. Each worksheet has the same name of the cityname, listed in the list box. So i have on my form this list box and i want to make a button which can open workbook with the specific worksheet. And this is my problem It must be something dynamic thing.
I have supplied the code to do just that earlier. If you cannot explain the root problem how do you expect other people to give you the correct answer.
I have supplied the code to do just that earlier. If you cannot explain the root problem how do you expect other people to give you the correct answer.
Can some please help me with this problem?
I need a button that can open a specific sheet (based on the selection made in a list box on the same form) in a workbook (excel).