简体   繁体   中英

How to import specific data from Excel file to text file and import the data from generated text file into new Excel file by one macro

I am new to VBA so not have good idea. I have written a code but I am not able to perform specific tasks as I want to perform. If somebody helps me out then I shall be grateful to you. Tasks:

I want to develop just one macro that automatically

1- Import Data from specific cells from Excel sheet to .txt file.

2- Import that data from created .txt file into new excel file. ( Note Not want to get data in same excel file I want a new excel file to get open with data in it).

在此处输入图片说明

The code I have written for importing data from specific cells in excel files is producing text file where I am not being able to get separate columns (as shown in figure 2 separate columns). This code gives me data but just in 1 column but I want to get data in 2 separate columns.

2nd part of my code works to copy data from text file to excel sheet but in same excel sheet data is being copied means in same excel workbook but in different sheet in that workbook but I want that data should be copied in some other / new excel file/workbook that automatically being generated.

Important every time I update data in excel sheet so data in generated .txt file and new excel file should also be keep on updating.Please have a look at the image/ picture attached.

My Code

Private Sub CommandButton1_Click()

Dim rng As Range
Dim Sourceworksheet As Worksheet
Dim DestFile As String
Dim cel As Range

Application.DefaultFilePath = "C:\Libraries\Documents\"

DestFile = Application.DefaultFilePath & "\Test.txt"
Open DestFile For Output As #1

Set Sourceworksheet = ActiveWorkbook.ActiveSheet
Set rng = Range("A6:B40")

For Each cel In rng.Cells
    Write #1, cel.Address & "|" & cel.Value2
Next cel
Close #1

MsgBox "txt file exported"

Dim X As Double
Dim TXT As String

Open "C:\Users\Documents\Test.txt" For Input As #1

X = 0
Do While Not EOF(1)
    Line Input #1, TXT
    Cells(1, 1).Offset(X, 0) = TXT
    X = X + 1
Loop
Close #1

End Sub

Quick questions (will make assumptions in code):

.1) Do you have one file that you work with and update (fixed name)?

.2) Does the file you are creating actually need to auto-update?

.3) Why do you need a separate spreadsheet? It might work to even have a new Sheet in the existing Workbook.

If you have a fixed file that you log information into, you can make a second excel file with the labels you want, then you can make the values link directly to the fixed file, eg:

A1 = "# of new TTNR:", then B1 = "FILEPATH/[FIXED NAME.xlsx]Sheet1!B2"

If you did this, you would get a prompt on loading the file containing that info to update the cell values, where Excel will pull the named data explicitly.

If you didn't have a fixed file name/location, you can make a macro to pull that value and name, dynamically.

I'd like to think that the above will help you, as a non-VBA solution. The assumptions made are that you have a fixed file name which your original data is in, and that your "truncated" file can also have a fixed name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM