Modify VBA to import Text File into new rows in a workbook sheet

danielalvz

New Member
Joined
Jan 29, 2022
Messages
9
Office Version
  1. 2011
Platform
  1. Windows
hello Im trying to modify a VBA code, right now I have a code working that open a text file as workbook, then it copies the ranges from the active workbook into thisworkbook, the only issue is that I will continue to import into this workbook and I need it to import into the next row, and this bit of coding to copy the desire range into a new row is not working out for me, see current code below. Thanks in advance

Sub Get_Summary()

Dim FileToOpen As Variant
Dim wsMaster As Worksheet
Dim wbTextImport As Workbook
Dim destLastRow As Long

Application.ScreenUpdating = False

Set wsMaster = ThisWorkbook.Sheets(15)

destLastRow = wsMaster.Range("A" & Rows.Count).End(xlUp).Row
If destLastRow < 1 Then destLastRow = 1

FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Text Files (*.txt*),*txt*")

If FileToOpen = False Then
MsgBox "No File Selected"

Else

Workbooks.OpenText _
Filename:=FileToOpen, _
StartRow:=2, _
DataType:=xlDelimited, _
Tab:=True

Set wbTextImport = ActiveWorkbook


wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A1" & destLastRow + 1)

wbTextImport.Close False

End If

Application.ScreenUpdating = True

Worksheets("Main Sheet").Activate

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
Try changing this line:
Rich (BB code):
wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A1" & destLastRow + 1)
to this:
Rich (BB code):
wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A" & destLastRow + 1)

Note that you were doing.l
Let's say that "destLastRow" was 20 (so the last row with data in column A on that sheet was row 20.
You then want to paste to line 21. So adding one to that variable accomplishes that.
However, you were NOT attaching that to column "A", you were attaching it to "A1", meaning you would be pasting it to row A121, not A21.
 
Upvote 0
Solution
Try changing this line:
Rich (BB code):
wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A1" & destLastRow + 1)
to this:
Rich (BB code):
wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A" & destLastRow + 1)

Note that you were doing.l
Let's say that "destLastRow" was 20 (so the last row with data in column A on that sheet was row 20.
You then want to paste to line 21. So adding one to that variable accomplishes that.
However, you were NOT attaching that to column "A", you were attaching it to "A1", meaning you would be pasting it to row A121, not A21.
thanks
 
Upvote 0

Forum statistics

Threads
1,214,553
Messages
6,120,179
Members
448,948
Latest member
spamiki

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