Ok, if you are visiting this article, then probably the reason is that you have received the following messagebox upon openning of Excel: And after looking all over your cells, with both VBA and “Find”, you have not found anything…
VBA is not a scripting language… Not at all!
Check if folder is empty:
1 2 3 |
Public Function FolderIsEmpty(myPath As String) As Boolean FolderIsEmpty = CBool(Dir(myPath & "*.*") = "") End Function |
Delete all files in a folder:
1 2 3 |
Public Sub DeleteAllFiles(path As String) Kill path & "*.*" End Sub |
Create text file in a given path with text:
1 2 3 4 5 6 7 8 9 10 11 12 |
Public Sub CreateTextFile(path As String, fileName As String, text As String) Dim fso As Object Dim file As Object Set fso = CreateObject("Scripting.FileSystemObject") Set file = fso.CreateTextFile(path & fileName, True) file.WriteLine text file.Close End Sub |
All together:
1 2 3 4 5 6 7 8 9 10 11 |
Sub Main() Dim path As String path = "C:\Users\Username\Desktop\New folder\" CreateTextFile path, "to_delete.txt", "some text inside the file" If Not FolderIsEmpty(path) Then DeleteAllFiles path End If End Sub |
Convert number to name – [3 -> March] Converting month number to name in VBA is actually coming out of the box with the MonthName() function, which is built-in the stanard VBA library:
1 2 3 4 5 6 7 8 9 |
Sub MonthNameExample() Dim i As Long For i = 1 To 12 Debug.Print MonthName(i) Debug.Print Format(MonthName(i, True)) Next End Sub |
The result is not flabbergasting, but…
Ok. This article is for all the people, who have written code like this one:
1 2 3 4 |
if direction == "left" then if column > 0 then: new_row = current_row new_col = current_col - 1 |
When they were solving a problem like this one: You know who you are. You are probably thinking what is wrong with it? After…
There are quite a few methods for writing data from a text file to Excel. The main are the following: Through a Query Table Through opening the text file and Reading and writing line by line Reading the text file,…
Although there are plenty of other tools for extracting data from a website (take a look at Beautiful Soup), VBA is somehow good, because … well, because it is somehow challenging to do it every time. Yesterday, I answered a…