
Functions in Python are objects. If this is the first time you read this sentence, then you will not get a lot from this article, but it will show you, that there is quite a lot to learn about Python.…
Have you seen the error below?
1 2 3 4 5 6 |
Traceback (most recent call last): File "C:/Users/foo/PycharmProjects/pythonProject3/main.py", line 29, in <module> main() File "C:/Users/foo/PycharmProjects/pythonProject3/main.py", line 19, in main a['foo'] += 5 KeyError: 'foo' |
Of course you have. And you know, that it is because you have written something extremely unwise like this:
1 2 |
a = dict() a['foo'] += 5 |
Yeah. You cannot increment the value of a non-existent key. Unless… Unless you…
Transforming Range to Array in VBA is actually a one liner – we take the range and we assign it to a variant:
1 2 3 4 5 |
Dim myRange As Range Set myRange = Range("a1").CurrentRegion Dim myArray As Variant myArray = myRange |
The “strange” part is that the array is converted to 2-dimensional array after this: and working…
Intersection of dictionary (or of any two lists) is a list with elements, that are present in both other lists. Thus, if we have the following 3 dictionaries in Python:
1 2 3 |
dict_a = dict([('aa', 4139), ('bb', 4127), ('cc', 4098)]) dict_b = dict(aa=4139, bb=4127, ee=4098) dict_c = {'aa':12, 'bb':13, 'dd':23} |
Then, an intersection of their keys will contain only…
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…
VBA – Make Excel Comments Beautiful
Everyone, who has worked with Excel more than a year knows that the comments are actually quite useful – they allow us to put info data into a cell, without actually putting data in the cell. However, the standard way…