How do I import desired columns into another workbook?

Frankximus

New Member
Joined
Feb 13, 2016
Messages
32
Hey guys, I've got some codes below that allows me to import a few columns into anther excel document, however I need to repeat it so it allows import of non adjacent columns. Currently it imports from A to J, but I need import column A to I and J to R and drop them in different columns. How do I repeat it so it allows me to do that?

Inputs from the source workbook: columns A to I, J to R
Outputs dropped in destination workbook: columns A to I and R to X,


Sub importcontact()
Dim actwb As Workbook

Dim LastRow As Long
Dim DestRow As Long
Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object

Dim filename As Variant
Dim sFilename As String
Dim nbrMarkerRecs As Integer
Dim nbrProjectRecs As Integer
Dim nbrActivityRecs As Integer
Dim nbrWhatRecs As Integer

' Open File to load

filename = Application.GetOpenFilename(FileFilter:="Contact Import Files (*.txt; *.csv; *.xls; *.xlsx), *.txt;*.csv*.xls;*.xlsx", Title:="Import Contacts", MultiSelect:=False)
Set actwb = Workbooks.Open(filename)
Range("A12:I101").Copy 'copy values
If TypeName(filename) = "String" Then
sFilename = CStr(filename)
Application.ScreenUpdating = False

ThisWorkbook.Worksheets("BG Load tab").Activate 'insert values
Range("A15:I104").PasteSpecial xlPasteValues
rval = MsgBox("Import Completed")

Application.CutCopyMode = False

actwb.Close

Else
' No file selected
rval = MsgBox("Import Cancelled: You did not selected a file to import reference data from.", vbExclamation, cAppTitle)
End If


ImportExit:
Application.ScreenUpdating = True
On Error Resume Next
Exit Sub


End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi and welcome to the forum.

Code:
[color=darkblue]Sub[/color] importcontact()
    
    [color=darkblue]Dim[/color] filename  [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    
    [color=green]' Open File to load[/color]
    
    filename = Application.GetOpenFilename(FileFilter:="Contact Import Files (*.txt; *.csv; *.xls; *.xlsx), *.txt;*.csv*.xls;*.xlsx", Title:="Import Contacts", MultiSelect:=False)
    [color=darkblue]If[/color] TypeName(filename) = "String" [color=darkblue]Then[/color]
    
        Application.ScreenUpdating = [color=darkblue]False[/color]
        Application.DisplayAlerts = [color=darkblue]False[/color]
    
        [color=darkblue]With[/color] Workbooks.Open(filename)
    
            Range("A12:I101").Copy    [color=green]'copy values[/color]
            ThisWorkbook.Worksheets("BG Load tab").Range("A15").PasteSpecial xlPasteValues    [color=green]'insert values[/color]
            
            Range("J12:R101").Copy    [color=green]'copy values[/color]
            ThisWorkbook.Worksheets("BG Load tab").Range("R15").PasteSpecial xlPasteValues    [color=green]'insert values[/color]
        
            .Close SaveChanges:=[color=darkblue]False[/color]
            
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
        Application.DisplayAlerts = [color=darkblue]True[/color]
        Application.ScreenUpdating = [color=darkblue]True[/color]
        MsgBox "Import Completed"
    
    [color=darkblue]Else[/color]
        [color=green]' No file selected[/color]
        MsgBox "You did not selected a file to import reference data.", vbExclamation, "Import Cancelled"
    [color=darkblue]End[/color] [color=darkblue]If[/color]
        
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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