Create a file with the help of VBA in the folder

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
80
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi,
dear professors and friends, I have a question, please help me🙏🙏
I create a file in the folder using the following code

VBA Code:
Sub CreateTxtFile()
directory = "D:\Data\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(directory & "F100.txt ")
End Sub
I want to check this code. If the F100.txt file is available in the Data folder, write the name of F100 in cell A1 of the sheet and write the word "Available" in A2.
If the F100.txt file does not exist in the Data folder, create a file and write the word "file created" in cell A2.
 
Last edited by a moderator:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try the following macro...

VBA Code:
Sub CreateTxtFile()

    Dim objFSO As Object 'filesystemobject
    Dim objTS As Object 'textstream
    Dim directory As String
    Dim filename As String

    directory = "D:\Data\"
    
    If Right(directory, 1) <> "\" Then
        directory = directory & "\"
    End If
    
    filename = "F100.txt"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    With ThisWorkbook.Worksheets("Sheet1") 'change the workbook and sheet references as desired
        If objFSO.FileExists(directory & filename) Then
            .Range("a1").Value = objFSO.GetBaseName(directory & filename)
            .Range("a2").Value = "Available"
        Else
            Set objTS = objFSO.CreateTextFile(directory & filename)
            .Range("a2").Value = "Created"
        End If
    End With
    
    
End Sub

If you want to write to the active sheet instead, replace...

VBA Code:
With ThisWorkbook.Worksheets("Sheet1")

with

Code:
With ActiveSheet

Hope this helps!
 
Upvote 0
Solution
Try the following macro...

VBA Code:
Sub CreateTxtFile()

    Dim objFSO As Object 'filesystemobject
    Dim objTS As Object 'textstream
    Dim directory As String
    Dim filename As String

    directory = "D:\Data\"
  
    If Right(directory, 1) <> "\" Then
        directory = directory & "\"
    End If
  
    filename = "F100.txt"
  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  
    With ThisWorkbook.Worksheets("Sheet1") 'change the workbook and sheet references as desired
        If objFSO.FileExists(directory & filename) Then
            .Range("a1").Value = objFSO.GetBaseName(directory & filename)
            .Range("a2").Value = "Available"
        Else
            Set objTS = objFSO.CreateTextFile(directory & filename)
            .Range("a2").Value = "Created"
        End If
    End With
  
  
End Sub

If you want to write to the active sheet instead, replace...

VBA Code:
With ThisWorkbook.Worksheets("Sheet1")

with

Code:
With ActiveSheet

Hope this helps!
Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,224,905
Messages
6,181,662
Members
453,059
Latest member
jkevin

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