Copying Columns to each notepad

xsdip

New Member
Joined
May 21, 2019
Messages
32
I am very new to excel VBA. I have learned the basics of VBA programing language. Could you please anyone help me in this.

Assuming I have numbers in F9 to F208996 and G9 to G330571 etc.

How do I capture these numbers and convert it into separate nodepad files. i.e. I mean to say, F column datas in one textpad and G column datas in another textpad etc.

In figure below, I need to capture the bold numbers. The columns to be converted to each notepad are F,G,H,I always. The row to be converted to notepad starts always from 9th row till lot of numbers

1638904019048.png


I have made a rough code. But not sure how to write loop.

Sub Export()
Dim lastrow As Long
Dim lastcolumn As Long
Dim i As Integer, j As Integer

lastrow = Cells(1, "A").End(xlDown).Row
lastcolumn = activesheets.Cells(1, Column.Count).End(xlToLeft).Column

For i = 1 To lastrow
For j = 1 To lastcolumn



Next j
Next i


End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Share at least a source workbook and an expected result Notepad text file accordingly …​
 
Upvote 0
This is the source workbook

1638986653334.png



I would like to get output like this.

1638986890189.png


Inside Sheet1_Number1.txt
1638986972182.png


Inside Sheet1_Number2.txt
1638987038570.png


Similarly for Sheet1_Number3.txt and Sheet1_Number4.txt
 
Upvote 0
I can't guarantee this is faster but give it a whirl.
You will need to update where your data starts (cellStart currently set to F9)
and your folder_Name.

VBA Code:
Sub ExportToTextFile()

    Dim wbRead As Workbook
    Dim shRead As Worksheet
    Dim wbTemp As Workbook
    Dim shTemp As Worksheet
    Dim cellStart As Range
    Dim lastRow As Long, lastCol As Long, currCol As Long, firstRow As Long
    Dim i As Long
    Dim folder_Name As String
    Dim file_Name As String
   
    Application.ScreenUpdating = False
   
    Set wbRead = ThisWorkbook
    Set shRead = wbRead.ActiveSheet
   
    Set cellStart = shRead.Range("F9")                      '<----- Update starting position
    firstRow = cellStart.Row + 1
    currCol = cellStart.Column
   
    ' Get the last cell with data in row 1
    lastCol = shRead.Cells(cellStart.Row, shRead.Columns.Count).End(xlToLeft).Column
   
    folder_Name = "C:\Users\Documents\Excel\Test\"          '<----- Update Folder
    For i = cellStart.Column To lastCol
        Set wbTemp = Workbooks.Add
        Set shTemp = ActiveSheet
        With shRead
            .Range(.Cells(firstRow, i), .Cells(Rows.Count, i).End(xlUp)).Copy
        End With
        shTemp.Range("A1").PasteSpecial Paste:=xlPasteValues
        file_Name = shRead.Cells(cellStart.Row, i).Value
       
        Application.DisplayAlerts = False
        wbTemp.SaveAs fileName:=folder_Name & file_Name, _
                        FileFormat:=xlTextMSDOS, CreateBackup:=False
        wbTemp.Close SaveChanges:=False
        Application.DisplayAlerts = True
    Next i

    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you so much for the reply.

Could you please help me with below query

How would I browse a path and save the textfile in wherever we are interested instead of the C drive path
 
Upvote 0
Tha k you so much @Alex Blakenburg .. the code works pretty good and fast.

Regarding the folder picker option, I gave the code like this . But it does not work fine.

Dim folder_name as Filedialog
Set folder_name= application.filedialog(msofiledialogfolderpicker)
With folder_name
.Allow multiselect=True
.show

Could you please help me in this
 
Upvote 0
Regarding the folder picker option, I gave the code like this . But it does not work fine.
Just replace this:
VBA Code:
folder_Name = "C:\Users\Documents\Excel\Test\"          '<----- Update Folder

With this:
VBA Code:
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
        .Title = "Select Destination folder"
        If .Show = 0 Then Exit Sub
        folder_Name = .SelectedItems(1)
    End With
 
Upvote 0
Thank you for the quick reply. I have used the same.

Actually, the textfiles now saves outside of the folder and not inside.

That is if I select D:\Newfolder, the textfiles is getting generated in D:\
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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