Copy Specific Columns using VBA

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
Good Afternoon,

In the following code I'm importing data from another worksheet. As you can see it's currently importing range A1:T40. I want to copy a few specific columns from the report instead. It's about 8 columns, data going down to row 40. How would I do that?

Code:
Sub ImportDatafromotherworksheet()   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Sheets("Orders").Range("A1:T40").Copy wkbCrntWorkBook.Sheets("Sheet1").Range("A1")
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").CurrentRegion.EntireColumn.AutoFit
         wkbSourceBook.Close False
      End If
   End With
End Sub
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,203
Office Version
  1. 365
Platform
  1. Windows
Something like
Code:
With Sheets("Orders")
   Intersect(.Range("A1:T40"), .Range("A:A, C:C, H:H")).Copy SwkbCrntWorkBook.Sheets("Sheet1").Range("A1")
End With
 

Glasgowsmile

Board Regular
Joined
Apr 14, 2018
Messages
152
Where should I put that code? I put it here, but I get an error.

Code:
Sub ImportDatafromotherworksheet()   Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
   With Sheets("Orders")
   Intersect(.Range("A1:T40"), .Range("A:A, C:C, H:H")).Copy SwkbCrntWorkBook.Sheets("Sheet1").Range("A1")
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Sheets("Orders").Range("A1:T40").Copy wkbCrntWorkBook.Sheets("Sheet1").Range("A1")
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").CurrentRegion.EntireColumn.AutoFit
         wkbSourceBook.Close False
      End If
   End With
   End With
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,203
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
Sub ImportDatafromotherworksheet()
Dim wkbCrntWorkBook As Workbook
   Dim wkbSourceBook As Workbook
   Set wkbCrntWorkBook = ActiveWorkbook
   With Application.FileDialog(msoFileDialogOpen)
      .Filters.Clear
      .Filters.Add "Excel 2007-13", "*.xlsx; *.xls; *.xlsm; *.xlsa"
      .AllowMultiSelect = False
      .Show
      If .SelectedItems.Count > 0 Then
         Workbooks.Open .SelectedItems(1)
         Set wkbSourceBook = ActiveWorkbook
         Intersect(Sheets("Orders").Range("A1:T40"), Sheets("Orders").Range("A:A, C:C, H:H")).Copy _
            wkbCrntWorkBook.Sheets("Sheet1").Range("A1")
         wkbCrntWorkBook.Sheets("Sheet1").Range("A1").CurrentRegion.EntireColumn.AutoFit
         wkbSourceBook.Close False
      End If
   End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,134
Messages
5,599,914
Members
414,348
Latest member
KloppyM

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
Top