Macro is ignoring copy command!

hobboy

New Member
Joined
Jul 13, 2011
Messages
18
Hi,

First of all I would like to point out that I'm a complete beginner at vba. I've only been learning for a few days and am still finding my way around.

What my code does is create text files named as a particular song, and then writes the lyrics into those text files.

Code:
    Else
        LyricText = Range("F" & iRow)
        MyFile.WriteLine (LyricText)
        MyFile.Close
        End If

For some reason, at F16 the lyrics do not wish to write into the text file. The name of the file is "05 Blank Infinity.txt". However, when I change any part of the text file name (eg. Blasdfnk Infinity or Blank Infasdfnity) the lyrics write in perfectly.

This is the ONLY case in which lyrics do not get written in. I added a watch to the "LyricText" and saw that the lyrics string does get written into that. But for some reason it doesn't save.

Note: I am copying the text files to a MP3 player connected via usb.

Code:
Option Explicit
Public PathCount As Long


Sub CreateTextFile()
Sheets("FileList").Activate
PathCount = 0
Dim fso
Dim Filepathname As String
Dim iRow As Long
Dim iCol As String
Dim MyFile
Dim LyricText As String

Set fso = CreateObject("Scripting.FileSystemObject")

iRow = "12"
iCol = "C"

While PathCount < CycleCount
    Filepathname = Range("C9") & Range(iCol & iRow) & ".txt"
    Set MyFile = fso.CreateTextFile(Filepathname, True)

    If IsEmpty(Worksheets("Filelist").Range("F" & iRow)) = True Then
    
    Else
        LyricText = Range("F" & iRow)
        MyFile.WriteLine (LyricText)
        MyFile.Close
        End If

        iRow = iRow + 1
    PathCount = PathCount + 1
Wend
    Cells("10", "D") = PathCount
End Sub
 
Last edited:

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Update:

After some more troubleshooting, I seem to be a bit closer to solving the problem.

It turns out that when I debug the macro and step through it manually the mystery file (05 Blank Infinity.txt) successfully creates and saves the lyrics. It works when I both tap the F8 key or hold the key down.

I was thinking that it could be that the file closes too quickly for the text to write, but have not been able to overcome the issue.

Any suggestions?
 
Upvote 0
Put a breakpoint on the line
Set MyFile = fso.CreateTextFile(Filepathname, True)

Then see what the Filepathname variable contains when you are at row 16.
Probably it contains something that cannot be interpreted as a valid path/filename...
 
Upvote 0
Thanks for the response.
I tried the breakpoint at that line you suggested and played it until it go to row 16, and it had the correct pathname and lyrics oddly.

So from that I decided to make a loop to check if any text was written into the file, and if it hasn't, repeat the option to create text. This does it indefinitely until the file is created, so the loop is a bit dodgy. However this method works and the text file creates successfully.

A day after this I deleted the loop to see if it worked again without the file size checker, and it did! So I'm puzzled as to why there was this issue. I'm going to keep the loop in there just is case it decides to happen again in the future. Do note that I rewrote a lot of my code just before I implemented the loop, and although it didn't immediately fix the problem, maybe it helped after a few computer restarts. :/

Here's my new code if anyone is curious

Code:
Option Explicit
Public PathCount As Long
Dim fso
Dim GenTextFile As Object
Dim iRow As Long
Dim iCol As Long
Dim LyricsDir As String
Dim LyricFile As Object
Dim LyricsSource As Object
Dim LyricsText As String
Dim Mp3Dir As String
Dim Mp3Source As Object
Dim TxtPathName As String
Dim x


Sub TextFileGen()

Sheets("FileList").Activate

PathCount = 0
iRow = 12
iCol = 3
Mp3Dir = Cells(7, 3)
LyricsDir = Cells(9, 3)
screwupcnt = 0

    While PathCount < CycleCount
        
    If IsEmpty(Worksheets("Filelist").Cells(iRow, 6)) = True Then

        Else
            Call AddLyrics(iRow)
    
        End If

    iRow = iRow + 1
    PathCount = PathCount + 1
    
        
Wend
    Cells("10", "D") = PathCount
    Cells(10, 8) = screwupcnt
End Sub

Sub AddLyrics(iRow)

    Sheets("FileList").Activate
    
    
    TxtPathName = Cells(9, iCol) & Cells(iRow, iCol) & ".txt"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set GenTextFile = fso.CreateTextFile(TxtPathName, True)
    Set Mp3Source = fso.GetFolder(Mp3Dir)
    Set LyricFile = fso.GetFile(TxtPathName)
    Set LyricsSource = fso.GetFolder(LyricsDir)
    
    LyricsText = Cells(iRow, 6)
    GenTextFile.WriteLine (LyricsText)
    GenTextFile.Close
    
    If LyricFile.Size = 0 Then
        Call AddLyrics(iRow)
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,776
Members
452,942
Latest member
VijayNewtoExcel

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