How to copy specific columns from a specific sheet of a closed workbook

rahul92

New Member
Joined
Mar 18, 2014
Messages
3
Hi,

I am very new at VBA. However, I'm working on this tool, where I need to develop the following in sequence:
1. User has to browse for a particular workbook
2. User then selects the sheet required for copying
3. User then selects a rage of columns required to be copied
4. This tool copies column D, along with the range, and pastes it in Sheet 2 in the Active Worbook

The code is as follows, but it doesn't compile successfully.

Code:
Sub ImportData()
    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
     
    Dim rngSourceRange, rngSourceRange2 As Range
    Dim rngDestination, rngDestination2 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
        .AllowMultiSelect = False
        .Show
         
        If .SelectedItems.Count > 0 Then
            Workbooks.Open .SelectedItems(1)
            Set wkbSourceBook = ActiveWorkbook
            [COLOR=#ff0000]'code to select sheet from the workbook[/COLOR]
            Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1", Type:=8)
            Set rngSourceRange2 = wkbSourceBook.ActiveSheet.Cells(2, 4) ' required to select column D completely
            wkbCrntWorkBook.Activate
            Set rngDestination = ActiveSheet.Cells(2, 2)               [COLOR=#ff0000] ' paste destination must be column B onwards, i.e, whatever the user selects as range[/COLOR]
            Set rngDestination2 = ActiveSheet.Cells(1, 1)              [COLOR=#ff0000] ' paste destination must be column A, i.e, from D of other sheet to A of this sheet[/COLOR]
            rngSourceRange.copy rngDestination
            rngSourceRange2.copy rngDestination2
            
            
            rngDestination.CurrentRegion.EntireColumn.AutoFit
            wkbSourceBook.Close False
        End If
    End With
End Sub

Private Sub Import_Click()
    ImportData
End Sub

Any help will be appreciated!
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,216,526
Messages
6,131,187
Members
449,631
Latest member
mehboobahmad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top