Create empty .txt files based on a range of cell values

lecex

New Member
Joined
Aug 20, 2022
Messages
6
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am looking for a way to create a list of empty .txt files based that are based on the values of a range of cells.
Here is what I have but no file is ever created.
Please note that I am a beginner in VBA and tried to adapt some code I found on the web.
Thanks in advance for any advice/suggestions.
Lex.

Sub MakeFolders()
Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Dim filename As String
Set Rng = Selection

maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
'MkDir (ActiveWorkbook.Path & "\" & Rng(r, c)) - THE CODE WORKS FOR CREATING FOLDER NAMES

'BUT WHEN I TRY TO ADAPT THE CODE AND USE THE SAME LOGIC FOR CREATNG FILES NO FILE GETS CREATED
filename = Rng(r, c) & ".txt"
'MsgBox filename I USED THIS TO CHECK IF THE FILENAME IS CORRECT

Open ActiveWorkbook.Path & filename For Output As r
Close r

On Error Resume Next
End If
r = r + 1
Loop
Next c
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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