Import the text file into same workbook but after active worksheet.

amittheexcel

Board Regular
Joined
Dec 17, 2013
Messages
50
Hi All,

As per below codes i'm able to import the excel, but this codes importing the excel into actives sheet .

Can you please arrange this codes in such a manner so that it will import sheet into next sheet of active sheet.

Sub ImportTextFile()


Dim DestBook As Workbook, SourceBook As Workbook
Dim DestCell As Range
Dim RetVal As Boolean


' Turn off screen updating.
Application.ScreenUpdating = False


' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = ActiveCell


' Show the Open dialog box.
RetVal = Application.Dialogs(xlDialogOpen).Show("*.txt")


' If Retval is false (Open dialog canceled), exit the procedure.
If RetVal = False Then Exit Sub


' Set an object variable for the workbook containing the text file.
Set SourceBook = ActiveWorkbook


' Copy the contents of the entire sheet containing the text file.
Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy


' Activate the destination workbook and paste special the values
' from the text file.
DestBook.Activate
DestCell.PasteSpecial Paste:=xlValues


' Close the book containing the text file.
SourceBook.Close False


End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You can make changes here:

Code:
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = ActiveCell

If you want to add a new sheet after the active sheet then:

Code:
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = DestBook.Sheets.Add(After:=ActiveSheet).Cells(ActiveCell.Row, ActiveCell.Column)

If you want to simply select the next sheet (i.e. don't add a new one) then:

Code:
' Set object variables for the active book and active cell.
Set DestBook = ActiveWorkbook
Set DestCell = DestBook.Sheets(ActiveSheet.Index + 1).Cells(ActiveCell.Row, ActiveCell.Column)

In both cases I'm assuming you want the same cell in the new sheet to be where the data is pasted.

WBD
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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