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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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