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 Worksheet Range 3

Status
Not open for further replies.

vza

Programmer
Aug 1, 2003
179
US
Quick question...Say I have a line of code like the following:

Code:
rsXL.Open "Select * From [Sheet1$A1:G100]", CnXL, adOpenStatic

If I don't know the exact range of the worksheet i needed during runtime is there any way I could represent it in code??

Any responses would be greatly appreciated.

Thanks
-vza
 
The easiest way is to use the .UsedRange property of the worksheet object. You'll have to set a reference to the workbook and worksheet to use the property, but it should do what you need.

Hope this helps.

Glen Appleton

VB.Net student.
 
I have decided to use the transferspreadsheet function from the Access library which is why I need to find the range dynamically....I do not think the function will accept the .UsedRange method of an Excel.Application object.

Thanks
-vza
 
This MAY work - I emphasize the may as I have not actually tried it:

rsXL.Open "Select * From [Sheet1$A1:G" & rsXL.sheets(1).cells(65536,1).end(xlup).row & "]", CnXL, adOpenStatic

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
xlbo

I get an error stating that '[Sheet1$A1:G1]' is not a valid name....

-vza
 
Would excel be open at this time ??

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Here is what I have:
Code:
' Open Excel Workbook
   xlApp.Workbooks.Open FileName:=ConvertForm.ExcelText.Text
    
' Transfer Excel Spreadsheet to Access Database
    axApp.OpenCurrentDatabase ConvertForm.AccessText.Text
        
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "TABLE1", ConvertForm.ExcelText.Text, True, "[Sheet1$A1:G" & xlApp.Sheets(1).Cells(65536, 1).End(xlUp).Row & "]"

    Set axApp = Nothing
    Set xlApp = Nothing

I don't think I am missing anything....

Thanks
-vza
 
Try this:
"[Sheet1$A1:G" & xlApp.Activeworkbook.Sheets(1).Cells(65536, 1).End(xlUp).Row & "]"

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
same error....
Is there any way to store the worksheet's used range within a variable and then apply to the TransferSpreadsheet function??
Thanks for all the help..
Much appreciative!

-vza
 
Code:
Set xlWB = xlApp.Workbooks.Open FileName:=ConvertForm.ExcelText.Text
with xlWB
lRow = .sheets("Sheet1").cells(65536,1).end(xlup).row
      .Names.Add Name:="myRange", RefersTo1:= _
        "=Sheet1!A1:G" & lRow
end with

' Transfer Excel Spreadsheet to Access Database
    axApp.OpenCurrentDatabase ConvertForm.AccessText.Text
        
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "TABLE1", ConvertForm.ExcelText.Text, True, myRange

    Set axApp = Nothing
    Set xlApp = Nothing

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Try to use in TransferSpreadsheet:
"Sheet1!A1:G" & xlApp.Sheets(1).Cells(65536, 1).End(xlUp).Row

combo
 
oops - RefersTo1:= should read RefersTo:=

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
combo,

The command now runs...but nothing is transfrred...

xlbo,

I will try your alternative and report the results...Thanks!

-vza
 
vza,
I've just noticed that you work with Application object instead of Workbook - this may produce wrong range.
So follow Geoff's line of code (which as a whole should work perfectly):
[tt]Set xlWB = xlApp.Workbooks.Open FileName:=ConvertForm.ExcelText.Text[/tt]
and use in TransferSpreadsheet:
"Sheet1!A1:G" & xlWb.Sheets(1).Cells(65536, 1).End(xlUp).Row

combo
 
combo,
still no go...
The line executes but nothing is transferred...

this is what I have:
Code:
Private Sub ImportButton_Click()
 Dim axApp As New Access.Application
 Dim xlApp As New Excel.Application
 Dim xlWb As New Excel.Workbook

' Open Excel Workbook
   Set xlWb = xlApp.Workbooks.Open(FileName:=ConvertForm.ExcelText.Text)
    
' Transfer Excel Spreadsheet to Access Database
   axApp.OpenCurrentDatabase ConvertForm.AccessText.Text
        
   DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "TABLE1", ConvertForm.ExcelText.Text, True, "Sheet1!A1:G" & xlWb.Sheets(1).Cells(65536, 1).End(xlUp).Row

   Set axApp = Nothing
   Set xlApp = Nothing

End Sub

This is very frustrating...thanks for helping as much as you guys have.

-vza
 
Hi,
Could you try to dim workbook without "New":
[tt]Dim xlWb As Excel.Workbook[/tt]

With:
[tt]Dim axApp As New Access.Application[/tt]
the first reference to axApp creates new, hidden instance of access, so to work in it, use:
[tt]axApp.DoCmd.TransferSpreadsheet ...[/tt]
and:
[tt]axApp.Visible=True[/tt]

In case of problems, test the range address:
[tt]Msgbox "Sheet1!A1:G" & xlWb.Sheets(1).Cells(65536, 1).End(xlUp).Row[/tt]

So the code (without message):
[tt]Private Sub ImportButton_Click()
Dim axApp As New Access.Application
Dim xlApp As New Excel.Application
Dim xlWb As Excel.Workbook

' Open Excel Workbook
Set xlWb = xlApp.Workbooks.Open(FileName:=ConvertForm.ExcelText.Text)

' Transfer Excel Spreadsheet to Access Database
axApp.OpenCurrentDatabase ConvertForm.AccessText.Text
axApp.Visible=True
axApp.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "TABLE1", ConvertForm.ExcelText.Text, True, "Sheet1!A1:G" & xlWb.Sheets(1).Cells(65536, 1).End(xlUp).Row

Set axApp = Nothing
Set xlApp = Nothing
Set xlWb=Nothing

End Sub[/tt]

Hope this will work.

combo
 
The range is not working...
The MessageBox returns

[Sheet1!A1:G1]

even though the spreadsheet tested has over
50 entries....

Thanks
-vza
 
This function will return the used range of a worksheet using the .UsedRange object of the worksheet. It uses generic objects so it is not tied to any particular version of Excel library.

To use the function, use the following sample syntax:
Code:
Dim strRange As String

strRange = GetUsedRange("C:\MyPath\MyWorkbook.xls", "MySheetName")

-----
Code:
Function GetUsedRange(WorkbookPath As String, SheetName As String) As String

    Dim xlApp As Object
    Dim xlBook As Object
    Dim xlSheet As Object

    Set xlApp = CreateObject("Excel.Application")
    
    Set xlBook = xlApp.Workbooks.Open(WorkbookPath)
    Set xlSheet = xlBook.Sheets(SheetName)
    
    GetUsedRange = xlSheet.UsedRange.Address
    
    Set xlSheet = Nothing
    
    xlBook.Close False
    Set xlBook = Nothing
    
    xlApp.Quit
    Set xlApp = Nothing

End Function

Hope this helps.

Glen Appleton

VB.Net student.
 
Jumping in late here, but what about putting a dynamic named range into your worksheet that refers to the used range? Then you just have to refer to that named range in your code, which should be easy.

General pointers on this can be found here:


although it will need a bit of tweaking.

Just wanted to provide an alternate suggestion. . .

VBAjedi [swords]
 
Doh! That assumes that the file you are opening is an existing Excel file, which a second read of your posts indicates may not be the case. . .

VBAjedi [swords]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top