Results 1 to 2 of 2
  1. #1
    Lorlai is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Posts
    107

    Question Only importing a specific spreadsheet-vba


    I have some code that imports an excel spreadsheet into access and creates a table. I recently received a new spreadsheet that has many sheets. I only need the data from one specific sheet, resources. How can I make adjustments to the code below so that only the data on the resources sheet is imported into a new table?
    Code:
    Option Explicit
    
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
    "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    
    Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type
    
    Public Function CreateProjects()
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    Dim WrksheetName As String
    Dim i As Integer
    Dim oApp As Object
    
    OpenFile.lStructSize = Len(OpenFile)
    'OpenFile.hwndOwner = Form.Hwnd
    'OpenFile.hInstance = App.hInstance
    'sFilter = "acSpreadsheetTypeExcel (*.xlxs)" & Chr(0) & "*.xlxs" & Chr(0)
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = "E:\"
    OpenFile.lpstrTitle = "Choose a File"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
    Set oApp = CreateObject("Excel.Application")
    oApp.Visible = True
    oApp.Workbooks.Open OpenFile.lpstrFile
    
    oApp.Range("A1").Select
         'Finds and replaces spaces with an underscore
        oApp.ActiveCell.Value = Replace(ActiveCell.Value, " ", "_")
        oApp.ActiveCell.Offset(0, 1).Select
    Loop Until IsEmpty(ActiveCell)
    
    
    With oApp
    .Visible = False
    DoCmd.TransferSpreadsheet (acImport), acSpreadsheetTypeExcel97, "Temp", OpenFile.lpstrFile, True
    
    End With
    oApp.DisplayAlerts = False
    oApp.Workbooks.Close
    oApp.DisplayAlerts = True
    oApp.Quit
    Set oApp = Nothing
    End Function
    Thanks for your help!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,900
    According to Help,for the Range argument:

    'If you are importing from or linking to an Excel version 5.0 or later spreadsheet, you can prefix the range with the name of the worksheet and an exclamation point; for example, Budget!A1:C7.'
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 13
    Last Post: 03-28-2011, 02:09 PM
  2. Importing Spreadsheet
    By derfalpha in forum Access
    Replies: 1
    Last Post: 01-04-2011, 10:47 AM
  3. Undefined error importing spreadsheet
    By kbremner in forum Import/Export Data
    Replies: 1
    Last Post: 10-23-2010, 05:57 PM
  4. importing excel spreadsheet issues
    By majortool in forum Import/Export Data
    Replies: 1
    Last Post: 01-24-2008, 06:29 PM
  5. Type error when importing Excel spreadsheet
    By jlmnjem in forum Database Design
    Replies: 2
    Last Post: 08-27-2006, 07:26 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