LOUISBUHAGIAR54
Registered User.
- Local time
- Today, 01:01
- Joined
- Mar 14, 2010
- Messages
- 157
I am trying to write code in an excel worksheet to try to export excel data to a table in access.
The range in which the data is situated in excel varies. So the range address varies with the data. After a lot of searching I wrote the following code which works for a range which is fixed. The code is the following:
Dim acc As New Access.Application
Range(Range("b2
2"), Range("b2
2").End(xlDown)).Select
ActiveWorkbook.Names.Add Name:="ghazla", RefersTo:=Selection
acc.OpenCurrentDatabase "D:\PayRollFactory\JasminePayroll.accdb"
acc.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:="WorkItems", _
Filename:=Application.ActiveWorkbook.FullName, _
HasFieldNames:=True, _
Range:="Sheet2$B1
40"
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
End Sub
---------------------
As one can see from the first two lines of code I am trying to first delineate the range and then to name it. The intent is to name the range "ghazla" and so to cater for the fact that the extent of data varies.
The code works with the fixed range ="Sheet2$B1
40". However what I need to do is to adjust the code so that I can insert "ghazla" as the range name so that this can vary.
Can someone please give a hand. I know that this is not access code but I am sure someone has had this problem before. Sorry for the grin but it does not seem to go away. The range is from B1 to D40 although it does not really matter much.
Many thanks.
Louis Buhagiar
The range in which the data is situated in excel varies. So the range address varies with the data. After a lot of searching I wrote the following code which works for a range which is fixed. The code is the following:
Dim acc As New Access.Application
Range(Range("b2


ActiveWorkbook.Names.Add Name:="ghazla", RefersTo:=Selection
acc.OpenCurrentDatabase "D:\PayRollFactory\JasminePayroll.accdb"
acc.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:="WorkItems", _
Filename:=Application.ActiveWorkbook.FullName, _
HasFieldNames:=True, _
Range:="Sheet2$B1

acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
End Sub
---------------------
As one can see from the first two lines of code I am trying to first delineate the range and then to name it. The intent is to name the range "ghazla" and so to cater for the fact that the extent of data varies.
The code works with the fixed range ="Sheet2$B1

Can someone please give a hand. I know that this is not access code but I am sure someone has had this problem before. Sorry for the grin but it does not seem to go away. The range is from B1 to D40 although it does not really matter much.
Many thanks.
Louis Buhagiar
Last edited: