Results 1 to 7 of 7
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136

    Finding Range in Excel SpreadSheet via Access VBA

    Hi.

    I am trying to import data from Excel Spreadsheet into an access table.
    I don't want to import the whole spreadsheet, only a specific range which is not always static.
    The key to finding the start position of the range is to look for the word "Group Name".
    The end position would be something like start_position.end(xldown)



    The problem I am having is because we are accessing all this through Access VBA, it is not recognising some of the stuff in my code.
    Here is my code so far:

    Code:
    Option Compare Database
    Sub FindRange()
    
    Dim xlApp As Object
    Set xlApp = CreateObject("Excel.Application")
    
    
    xlApp.Visible = True
    
    
    Dim wb As Object
    Set wb = xlApp.Workbooks.Open("C:\Desktop\Test.xlsx")
    
    
    Dim rng As Object
    Set rng = wb.sheets(1).cells.Find(What:="Group Name")
    
    Dim rng2 As Object
    Set rng2 = wb.sheets(1).range(rng.Address).end(xldown)  'IT IS NOT LIKING THIS - HOW DO I OVERCOME THIS PART

    Basically, all I am trying to do is open up an excel spreadsheet, determine the range which starts from the keyword "Group Name", goes all the way up to column G and then all the way down to the last used row, and then pass this range as a parameter in my DoCmd.TransferSpreadsheet.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Link the XL file as an external table,
    make a query on the table and filter the data to append.

    no code needed.

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136
    not sure what you mean.

  4. #4
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    have you considered using the excel currentregion property?

    https://docs.microsoft.com/en-us/off....currentregion

  5. #5
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    You have to resize the cell that you found vertically and horizontally as follows:
    Code:
    Set rng2 = rng.Resize(rng.End(xlDown).Row - rng.Row + 1, 8 - rng.Column)
    Cheers,
    John

  6. #6
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    136
    ok, so my issue was not setting a reference to the Microsoft Excel Library.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    now you have set up the reference you can either

    hit F2, then find xlDown and see what the numeric value is
    or in the immediate window type ?xlDown

    when you know the numeric value, either replace xlDown with the number in your code or create a constant for xlDown

    then you can go back to late binding

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-20-2019, 11:38 AM
  2. Replies: 4
    Last Post: 03-19-2019, 08:35 AM
  3. Creating excel spreadsheet from access vba
    By nyneave in forum Programming
    Replies: 1
    Last Post: 10-12-2012, 09:59 AM
  4. Replies: 6
    Last Post: 07-24-2012, 05:09 AM
  5. Email Excel Spreadsheet from Access
    By Nancy in forum Access
    Replies: 2
    Last Post: 11-09-2010, 02:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums