Macros to export from single worksheet based on parameters to multiple txt files

anuradhagrewal

Board Regular
Joined
Dec 3, 2020
Messages
85
Office Version
  1. 2010
Platform
  1. Windows
Hi
I have this excel file with serial numbers of employees. I need to make individual text files of 500 serial numbers.
The master list is enclosed.
What I am looking for is that I am able to run a VB script that automatically that
1)Opens the worksheet-->DATA( main sheet) of the workbook file.
2)Pick up the first 500(
A501) serial numbers in DATA---> copies it and then creates a----> text file name "list1.txt".
3)Then the code picks up(in a loop) from the 501
(A502) serial number-----> copies it and then creates a----> text file name "list2.txt" and so on...
There are 3350 serial numbers in (3351 rows) and hence it should make around 7 text files.


I am completely lost and have no clue how to do it.
Can anyone help me out with this ... it will be a lifesaver.
 

Attachments

  • raw data.png
    raw data.png
    94.7 KB · Views: 11

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
This seemed to work OK.

VBA Code:
Sub t()
Dim sh As Worksheet, x As Long, fPath
fPath = ThisWorkbook.Path
Set sh = ActiveSheet
x = 1
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row Step 500
    Workbooks.Add
    sh.Cells(i, 1).Resize(500).Copy ActiveWorkbook.Sheets(1).Range("A2")
    ActiveWorkbook.SaveAs fPath & "\List" & x & ".txt", 42
    ActiveWorkbook.Close False
    x = x + 1
Next
End Sub

Couldn't include opening the file because no info provided on path and format of workbook.
 
Last edited:
Upvote 0
Thanks a ton ... it worked for me.:love:
However I have been unable to figure out how to add another column
For eg the Serial number of an employee is in A1 and his/her name is in A2.
Now the list only enlists his/her serial number. Is it it possible in this code that for an employee serial number his/her name also appears in the txt file.
Please help me out with this trivial query. You have already been of great help.
 
Upvote 0
Should I just make up the names or would you like to share with us where the names would be found?
 
Upvote 0
Should I just make up the names or would you like to share with us where the names would be found?
Hi
Just make up names.. or even better if the code allows name that are in the field to be picked up as I have close to 1500 or so employees.
Much appreciate your gesture and thanks a gazillion
 
Upvote 0
I was being facetious about making up the names. To modify the code so that it shows the names in the new workbooks, I need to know where on the source sheet the names are located. For example, if the names are in column B then change this line of code
sh.Cells(i, 1).Resize(500).Copy ActiveWorkbook.Sheets(1).Range("A2")
To this
Rich (BB code):
sh.Cells(i, 1).Resize(500, 2).Copy ActiveWorkbook.Sheets(1).Range("A2")
and it will then copy both columns A and B to the new workbook and save it as a text file.
But I cannot write code if you do not tell me where to find the data.
 
Upvote 0
The code worked.... thanks again for such a fast response...
On the lighter side buddy I would have shared the entire list of 1500 employees if my manager was cool with it. But hey WTH.... you have made my day.
Thanks Thanks Thanks:geek::love:
 
Upvote 0
The code worked.... thanks again for such a fast response...
On the lighter side buddy I would have shared the entire list of 1500 employees if my manager was cool with it. But hey WTH.... you have made my day.
Thanks Thanks Thanks:geek::love:
You're welcome,
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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