+ Reply to Thread
Results 1 to 7 of 7

Import data from an excel workbook to another workbook without manually selecting

Hybrid View

rmachbitz Import data from an excel... 07-10-2013, 12:32 PM
Norie Re: Import data from an excel... 07-15-2013, 09:43 AM
rmachbitz Re: Import data from an excel... 07-15-2013, 10:09 AM
Norie Re: Import data from an excel... 07-15-2013, 10:15 AM
rmachbitz Re: Import data from an excel... 07-15-2013, 11:27 AM
Norie Re: Import data from an excel... 07-15-2013, 01:35 PM
rmachbitz Re: Import data from an excel... 07-15-2013, 02:27 PM
  1. #1
    Forum Contributor
    Join Date
    06-20-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    106

    Import data from an excel workbook to another workbook without manually selecting

    Hi all,

    I'm trying to write a VBA code in my worksheet ( blank excel sheet) to pull data from another excel sheet without opening that file.

    This code will open a new window to open my file and range. But, I'm trying to avoid the process of going through different windows to select my data.

    I need this file to pull data from my C:\ directory with the range that I want. I use active X button for my convenience.

    Private Sub CommandButton2_Click()
     Dim wkbCrntWorkBook As Workbook
        Dim wkbSourceBook As Workbook
         
        Dim rngSourceRange As Range
        Dim rngDestination As Range
         
        Set wkbCrntWorkBook = ActiveWorkbook
         
        With Application.FileDialog(msoFileDialogOpen)
            .Filters.Clear
            .Filters.Add "Excel 2002-03", "*.xls", 1
            .Filters.Add "Excel 2007", "*.xlsx; *.xlsm; *.xlsa", 2
            .Filters.Add "Excel 2010", "*.xlsx; *.xlsm; *.xlsa", 2
            .AllowMultiSelect = False
            .Show
             
            If .SelectedItems.Count > 0 Then
                Workbooks.Open .SelectedItems(1)
                Set wkbSourceBook = ActiveWorkbook
                Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1", Type:=8)
                wkbCrntWorkBook.Activate
                Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
                rngSourceRange.Copy rngDestination
                rngDestination.CurrentRegion.EntireColumn.AutoFit
                wkbSourceBook.Close False
            End If
        End With
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Import data from an excel workbook to another workbook without manually selecting

    Is it a particular workbook you want to pull the data from?
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    06-20-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Import data from an excel workbook to another workbook without manually selecting

    Yes, I want to pull it from Sheet1 in the Excel.xlsx Workbook

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Import data from an excel workbook to another workbook without manually selecting

    Perhaps.
    Private Sub CommandButton2_Click()
    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    
    Dim rngSourceRange As Range
    Dim rngDestination As Range
    
        Set wkbCrntWorkBook = ActiveWorkbook
    
        Set wkbSourceBook = Workbooks.Open(Filename:="C:\Excel.xlsx")
        Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1", Type:=8)
        
        wkbCrntWorkBook.Activate
        Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
        
        rngSourceRange.Copy rngDestination
        rngDestination.CurrentRegion.EntireColumn.AutoFit
        
        wkbSourceBook.Close False
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    06-20-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Import data from an excel workbook to another workbook without manually selecting

    Thank you for this post. But, this still opens a message box for me to select the range and my destination. I want it to select certain range ex: A1:E10 and paste it automatically in my worksheet. So, basically all the message boxes should be automated.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Import data from an excel workbook to another workbook without manually selecting

    Replace Application.Inputbox with appropriate references for the source and destination ranges.

    For example to copy A1:E10 from the active sheet of the source workbook to A1 on the active sheet of the destination sheet.
    Set rngSourceRange = wkbSourceBook.ActiveSheet.Range("A1:E10")
        
    Set rngDestingation = wkbCrntWorkBook.ActiveSheet.Range("A1")
        
    rngSourceRange.Copy rngDestination
    
    rngDestination.CurrentRegion.EntireColumn.AutoFit

  7. #7
    Forum Contributor
    Join Date
    06-20-2013
    Location
    Dallas, Texas
    MS-Off Ver
    Excel 2007
    Posts
    106

    Re: Import data from an excel workbook to another workbook without manually selecting

    Thank you so much It works!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1