VBA to generate .md (format) field based on two columns in excel

alirezaEsfand

New Member
Joined
Aug 7, 2024
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
Hi everyone - I'm importing custom metadata into Salesforce using Excel and I need the below if possible via VBA:

VBA to allow me:
- Generate up to 500 individual files in a folder on my computer
- Name of the files to be based on what is in column A (starting with column A2)
- The content of each file to based on what is in column B (starting with column B2)
- The format of the files to be .md

i.e. A2= Name, B2= Germany ------> A file to be created called Name.md which contains Germany
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi all - please let me know if the above makes sense - I need the files exported to maintain the accents and special characters hence the below line (highlighted) -


VBA Code:
Sub GenerateMDFiles()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim filePath As String
    Dim fileName As String
    Dim fileContent As String
    Dim i As Long
    Dim fileSystemObject As Object
    Dim fileStream As Object
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change Sheet1 to the name of your sheet
    
    ' Find the last row with data in column A
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    ' Create a FileSystemObject
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
    
    ' Loop through each row
    For i = 2 To Application.WorksheetFunction.Min(501, lastRow)
        ' Get the file name and content from columns A and B
        fileName = ws.Cells(i, 1).Value
        fileContent = ws.Cells(i, 2).Value
        
        ' Define the file path
        filePath = "C:\Desktop\CustomMetadataImport\" & fileName & ".md" ' Change to your target folder path
        
        ' Create and write to the file
[B]        Set fileStream = fileSystemObject.CreateTextFile(filePath, True, True) ' True for Unicode (special characters)[/B]
        fileStream.Write fileContent
        fileStream.Close
    Next i
    
    ' Clean up
    Set fileSystemObject = Nothing
    Set fileStream = Nothing
    
    MsgBox "Files generated successfully!", vbInformation
End Sub
 
Upvote 0
I'm not sure what you need help with as you seem to have solved it yourself. What part of your code isn't working? If it helps, I've made a small amendment to how you're creating your file path, and I've changed the For Loop to reference lastRow, seen as you've gone to the trouble of declaring it further up. I've tested it and it seems to work fine for me, though I'm obviously just using made-up data for the test so it might not actually reflect your use case.

Code:
Sub GenerateMDFiles()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim filePath As String
    Dim fileName As String
    Dim fileContent As String
    Dim i As Long
    Dim fileSystemObject As Object
    Dim fileStream As Object
   
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change Sheet1 to the name of your sheet
   
    ' Find the last row with data in column A
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
   
    ' Create a FileSystemObject
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
   
    ' Loop through each row
    For i = 2 To lastRow
        ' Get the file name and content from columns A and B
        fileName = ws.Cells(i, 1).Value
        fileContent = ws.Cells(i, 2).Value
       
        ' Define the file path
        filePath = CreateObject("WScript.Shell").specialFolders("Desktop") & "\CustomMetadataImport\" & fileName & ".md" ' Change to your target folder path
       
        ' Create and write to the file
        Set fileStream = fileSystemObject.CreateTextFile(filePath, True, True)
        fileStream.Write fileContent
        fileStream.Close
    Next i
   
    ' Clean up
    Set fileSystemObject = Nothing
    Set fileStream = Nothing
   
    MsgBox "Files generated successfully!", vbInformation
End Sub
 
Upvote 0
Thanks @Sunjinsak - I'm getting an error:

Run-time error '76' page not found and when debugging it go to this line: Set fileStream = fileSystemObject.CreateTextFile(filePath, True, True)

1723122024229.png

I'm not sure what you need help with as you seem to have solved it yourself. What part of your code isn't working? If it helps, I've made a small amendment to how you're creating your file path, and I've changed the For Loop to reference lastRow, seen as you've gone to the trouble of declaring it further up. I've tested it and it seems to work fine for me, though I'm obviously just using made-up data for the test so it might not actually reflect your use case.

Code:
Sub GenerateMDFiles()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim filePath As String
    Dim fileName As String
    Dim fileContent As String
    Dim i As Long
    Dim fileSystemObject As Object
    Dim fileStream As Object
  
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change Sheet1 to the name of your sheet
  
    ' Find the last row with data in column A
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
  
    ' Create a FileSystemObject
    Set fileSystemObject = CreateObject("Scripting.FileSystemObject")
  
    ' Loop through each row
    For i = 2 To lastRow
        ' Get the file name and content from columns A and B
        fileName = ws.Cells(i, 1).Value
        fileContent = ws.Cells(i, 2).Value
      
        ' Define the file path
        filePath = CreateObject("WScript.Shell").specialFolders("Desktop") & "\CustomMetadataImport\" & fileName & ".md" ' Change to your target folder path
      
        ' Create and write to the file
        Set fileStream = fileSystemObject.CreateTextFile(filePath, True, True)
        fileStream.Write fileContent
        fileStream.Close
    Next i
  
    ' Clean up
    Set fileSystemObject = Nothing
    Set fileStream = Nothing
  
    MsgBox "Files generated successfully!", vbInformation
End Sub
 
Upvote 0
That's most likely because the folder you're trying to refer to with filePath doesn't exist. Does that error occur when using your version of the code, or mine (or both)?
 
Upvote 0
That's most likely because the folder you're trying to refer to with filePath doesn't exist. Does that error occur when using your version of the code, or mine (or both)?

On both I'm getting the same error - This is the path: filePath = CreateObject("WScript.Shell").specialFolders("Desktop") & "C:\Users\alex.jones\OneDrive\Desktop\CustomMetadataImport\" & fileName & ".md" ' Change to your target folder path

Could you help me fix that please?
 
Upvote 0
Does the folder CustomMetadataImport exist on the desktop?

Try doing:

Code:
MsgBox CreateObject("WScript.Shell").specialFolders("Desktop")

Does that return the same error?
 
Upvote 0
Does the folder CustomMetadataImport exist on the desktop?

Try doing:

Code:
MsgBox CreateObject("WScript.Shell").specialFolders("Desktop")

Does that return the same error?
I get the runtime error 5 with the below: :(
1723123595499.png
 
Upvote 0
I know, but that will happen if the folder you're referring to with filePath doesn't already exist, hence why I asked if the folder called CustomMetadataImport exists on the desktop? If it doesn't you need to create it before the code will work. If it does exist, check if you have a typo in the folder name - it needs to match the hard-coded string in your code exactly (though it's not case sensitive).
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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