Saturday, February 28, 2015

How to quickly convert an Excel file into several SRT files!



If you want to create multiple ".srt" files or if you need to upload many subtitles on Youtube from an Excel file, here's a little, ugly yet functional code in VBA that can save you a lot of time.


1. Your Excel file needs to look like this:



2. Add an Command button (ActiveX control)

3. Copy/Paste the following code between « Private Sub […] » and « End sub »

Set Rng = Selection
Dim strFile As String, impression As String
Dim fso As FileSystemObject
Dim txtStrm As TextStream

chemin = "C:\FilePath"
Name = Range("C1") + Mid(Selection.Cells(1, 1), 1, 23) + ".srt"
strFile = chemin + Name

 
Dim x As Long, y As Long, j As Long
j = 1

If Selection.Rows.Count = 1 Then
    MsgBox "Please select more than one row"
    Exit Sub
End If

x = Selection.Rows(1).Row
y = Selection.Rows.Count + x - 1

Set fso = New FileSystemObject
Set txtStrm = fso.CreateTextFile(strFile, Overwrite:=True, Unicode:=True)
For maligne = x + 1 To y


txtStrm.WriteLine Trim(j)
txtStrm.WriteLine Format(CStr(ActiveSheet.Range("A" & maligne)), "hh:mm:ss") + ",000" + " --> " + Format(CStr(ActiveSheet.Range("B" & maligne)), "hh:mm:ss") + ",000"
txtStrm.WriteLine Range("C" & maligne).Value
txtStrm.WriteLine
j = j + 1
 
Next maligne
txtStrm.Close

 

MsgBox ("Done !")

4. Modify "FilePath" to indicate where you want your .srt file to be saved

5. Change the indicated column if needed: 

The current configuration will take the subtitle of the C column (see the second highlighted part above).

6. In "Tools" --> "References" make sure that Microsoft Scripting Runtime is enabled

7. You're good to go!

You just have to select the subtitles as shown below and click on the button to generate your ".srt" file. It will appear in the folder you've entered as "FilePath".



No comments:

Post a Comment

Saturday, February 28, 2015

How to quickly convert an Excel file into several SRT files!



If you want to create multiple ".srt" files or if you need to upload many subtitles on Youtube from an Excel file, here's a little, ugly yet functional code in VBA that can save you a lot of time.


1. Your Excel file needs to look like this:



2. Add an Command button (ActiveX control)

3. Copy/Paste the following code between « Private Sub […] » and « End sub »

Set Rng = Selection
Dim strFile As String, impression As String
Dim fso As FileSystemObject
Dim txtStrm As TextStream

chemin = "C:\FilePath"
Name = Range("C1") + Mid(Selection.Cells(1, 1), 1, 23) + ".srt"
strFile = chemin + Name

 
Dim x As Long, y As Long, j As Long
j = 1

If Selection.Rows.Count = 1 Then
    MsgBox "Please select more than one row"
    Exit Sub
End If

x = Selection.Rows(1).Row
y = Selection.Rows.Count + x - 1

Set fso = New FileSystemObject
Set txtStrm = fso.CreateTextFile(strFile, Overwrite:=True, Unicode:=True)
For maligne = x + 1 To y


txtStrm.WriteLine Trim(j)
txtStrm.WriteLine Format(CStr(ActiveSheet.Range("A" & maligne)), "hh:mm:ss") + ",000" + " --> " + Format(CStr(ActiveSheet.Range("B" & maligne)), "hh:mm:ss") + ",000"
txtStrm.WriteLine Range("C" & maligne).Value
txtStrm.WriteLine
j = j + 1
 
Next maligne
txtStrm.Close

 

MsgBox ("Done !")

4. Modify "FilePath" to indicate where you want your .srt file to be saved

5. Change the indicated column if needed: 

The current configuration will take the subtitle of the C column (see the second highlighted part above).

6. In "Tools" --> "References" make sure that Microsoft Scripting Runtime is enabled

7. You're good to go!

You just have to select the subtitles as shown below and click on the button to generate your ".srt" file. It will appear in the folder you've entered as "FilePath".



No comments:

Post a Comment