Deleting a file in VBA

1.) Check here. Basically do this: Function FileExists(ByVal FileToTest As String) As Boolean FileExists = (Dir(FileToTest) <> “”) End Function I’ll leave it to you to figure out the various error handling needed but these are among the error handling things I’d be considering: Check for an empty string being passed. Check for a string … Read more

How to create and write to a txt file using VBA

Use FSO to create the file and write to it. Dim fso as Object Set fso = CreateObject(“Scripting.FileSystemObject”) Dim oFile as Object Set oFile = FSO.CreateTextFile(strPath) oFile.WriteLine “test” oFile.Close Set fso = Nothing Set oFile = Nothing See the documentation here: http://technet.microsoft.com/en-us/library/ee198742.aspx http://technet.microsoft.com/en-us/library/ee198716.aspx

When editing Microsoft Office VBA, how can I disable the popup “Compile error” messages?

Do the following in your VBA editor window (entitled “Microsoft Visual Basic for Applications”): Click the menu “Tools” and then “Options“. In the Options’ “Editor” tab, uncheck the “Auto Syntax Check” box. (See screenshot, below.) This change does not make the editor stop compiling in the background and marking syntax errors in red (or whatever … Read more

Can I simultaneously declare and assign a variable in VBA?

There is no shorthand in VBA unfortunately, The closest you will get is a purely visual thing using the : continuation character if you want it on one line for readability; Dim clientToTest As String: clientToTest = clientsToTest(i) Dim clientString As Variant: clientString = Split(clientToTest) Hint (summary of other answers/comments): Works with objects too (Excel … Read more

Difference between Visual Basic 6.0 and VBA

For nearly all programming purposes, VBA and VB 6.0 are the same thing. VBA cannot compile your program into an executable binary. You’ll always need the host (a Word file and MS Word, for example) to contain and execute your project. You’ll also not be able to create COM DLLs with VBA. Apart from that, … Read more

Loop through files in a folder using VBA?

Dir takes wild cards so you could make a big difference adding the filter for test up front and avoiding testing each file Sub LoopThroughFiles() Dim StrFile As String StrFile = Dir(“c:\testfolder\*test*”) Do While Len(StrFile) > 0 Debug.Print StrFile StrFile = Dir Loop End Sub