VBA – Save text from filtered table in Excel in separate file

I the current article I will simply present how to save the results from a filtered table in MS Excel in a separate text file. To do this you simply need some table as the following one, where you can have some values in Excel cells. To simplify the current article, I have created only two columns – Name and Town. I will use the second one to filter the values, equal to Sofia.

PeterTheKeeper

Once they are filtered, I have only the values on the lines of “Peter”, “Keeper”, “Nothing” and “To”, which I will try to export as a text file, located on the same folder, as the current file.

After running the macro below, this is the result:

Result

How do we achieve this result? This is what we do in the VBA code:

  1. We define the location of the current file (sPath).
  2. We copy the used range of the sheet.
  3. We define the last row of the used range.
  4. Then we open the sPath for input and with two embedded loops we go around the table and for each row we generate a line like the following “Peter”;”Sofia”.
  5. We close the file and we are ready.

The code is the one below:

Enjoy it! 🙂

Tagged with: , , , ,