VBA copy multiple columns in one file and pasting in another

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello all! Many many thanks for all of your past help. Reaching out again as I continue to find a roadblock when going between files.

The text file generates from a weekly report and the name will change each time. I run the code from the imported text file and the code works for the first copy, but when switching back to the original, it crashes when going to the original window. I receive a Run-time error '9'" Subscript out of range. This name (textfile.txt) comes from the text file that is imported. I have tried to rename the window, but it ends up renaming all.

VBA Code:
Sub Copy_Original()
'
' Copy_Original Macro
'

'
    Range("A1:L1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("Blank whole report.xlsm").Activate
    Range("A2").Select
    ActiveSheet.Paste
    Windows("textfile.txt").Activate
    Range("M1322").Select
    Selection.End(xlUp).Select
    Range("M1:T1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Blank whole report.xlsm").Activate
    Range("P2").Select
    ActiveSheet.Paste
    Application.Left = 238.75
    Application.Top = 98.5
End Sub

Someone may have a better suggestion than how I have it. But what I need is for all the data in columns A-L in spreadsheet 1 to be pasted into cell A2 in spreadsheet 2. And all the data on Spreadsheet 1 in columns M-T to be pasted into Spreadsheet 2 in P2.


Any help would be appreciated!!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
At the moment you are pretty dependant on the Text file being the activeworkbook and that the sheet you want to copy it to is the active sheet in the other workbook.
I have assumed the code is in the workbook that you are copying it to.

VBA Code:
Sub Copy_Original_Mod()
'
    Dim wbTxt As Workbook, wbXL As Workbook
    Dim shTxt As Worksheet, shXL As Worksheet
    Dim lastRowTxt As Long
    
    Set wbTxt = ActiveWorkbook
    Set shTxt = wbTxt.ActiveSheet
    
    Set wbXL = ThisWorkbook             ' Assuming Blank whole report.xlsm contains the code
    Set shXL = wbXL.ActiveSheet         ' A named sheet would be better
    
    lastRowTxt = shTxt.Range("A" & Rows.Count).End(xlUp).Row
    
    shTxt.Range("A1:L" & lastRowTxt).Copy Destination:=shXL.Range("A2")
    shTxt.Range("M1:T" & lastRowTxt).Copy Destination:=shXL.Range("P2")
    
    Application.CutCopyMode = False

End Sub
 
Upvote 0
Solution
At the moment you are pretty dependant on the Text file being the activeworkbook and that the sheet you want to copy it to is the active sheet in the other workbook.
I have assumed the code is in the workbook that you are copying it to.

VBA Code:
Sub Copy_Original_Mod()
'
    Dim wbTxt As Workbook, wbXL As Workbook
    Dim shTxt As Worksheet, shXL As Worksheet
    Dim lastRowTxt As Long
   
    Set wbTxt = ActiveWorkbook
    Set shTxt = wbTxt.ActiveSheet
   
    Set wbXL = ThisWorkbook             ' Assuming Blank whole report.xlsm contains the code
    Set shXL = wbXL.ActiveSheet         ' A named sheet would be better
   
    lastRowTxt = shTxt.Range("A" & Rows.Count).End(xlUp).Row
   
    shTxt.Range("A1:L" & lastRowTxt).Copy Destination:=shXL.Range("A2")
    shTxt.Range("M1:T" & lastRowTxt).Copy Destination:=shXL.Range("P2")
   
    Application.CutCopyMode = False

End Sub
You're correct about the assumptions! Thank you so very much for this!!
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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