This VBA function will remove the last character from a string:
Function CutOffLastChar(str As String)
CutOffLastChar = Left(str, Len(str) - 1)
End Function
A Dictionary object is an associative array. Items can be any form of data, and are stored in the array. Each item is associated with a unique key.
It is part of VBScript. To use a Dictionary from VBA, first you need to set a reference to Microsoft Scripting Runtime.
This is how you create a dictionary object:
Dim dic As New Dictionary
dic.Add "a", "aaa"
dic.Add "b", "bbb"
dic.Add "c", "ccc"
This is how you access an item through a key:
Debug.Print dic.Item("b")
The result is "bbb"
This function will create a file on the file system. It accepts 2 arguments: a file name, and file contents as string.
Sub SaveFile(sName As String, sContents As String)
Dim iFile As Integer
iFile = FreeFile()
Open sName For Binary Access Write As #iFile
Put #iFile, , sContents
Close #iFile
End Sub
Let's say you have a range "A1:H2".
The first row represents a column name, and the second row represents a row value,e.g:
ColA ColB
100 200
To enumerate the range values, we could use the following code:
Dim rng As Range, col As Range
Set rng = Range("A1:H2")
For Each col In rng.Columns
Debug.Print col.Cells(1).Value & " -" & col.Cells(2).Value
Next
This VBA function will remove characters from the end of a string:
Function removeCharactersFromEnd(str As String, char As String)
Do While Right(str, 1) = char
str = Left(str, Len(str) - 1)
Loop
removeCharactersFromEnd = str
End Function
So, for example, if you have a string similar to the following:
str = "abc____"
you call this function as follows:
str = removeCharactersFromEnd(str, "_")
The resulting string will be : "abc"
This function walks a DOM tree and counts the number of nodes:
Function treeWalk(node As IXMLDOMNode, ByRef c As Integer) As Integer
Dim nodeName
Dim child As IXMLDOMNode
For Each child In node.childNodes
If child.nodeType = NODE_ELEMENT Then
c = c + 1
End If
If child.hasChildNodes Then
treeWalk child, c
End If
Next
treeWalk = c
End Function
I found the following piece of code effective:
If str = vbNullString Then ...
IsError function:
If Not IsError(ActiveWorkSheet.Range("A1")) Then ...
A related VB issue is to check for a null value coming from a database query. In this case, you need to use the IsDBNull function
This article will show you how to read data from an Excel spreadsheet and insert it into a SQL Server database.
Function LastDateOfTheWeek()
LastDateOfTheWeek = 8 - Weekday(Date) + Date
End Function
This function gets the last date of the current week.
To get the last day of the week from a specific date, we need to make a slight change and pass a Date as an argument to the function.
Function LastDateOfTheWeek2(ByVal d As Date)
LastDateOfTheWeek2 = 8 - Weekday(d) + d
End Function
The following function deletes entire rows if there are no values in the corresponding cells of the specified column.
Function DeleteEmptyRows(ColNum As Integer)
Columns(ColNum).SpecialCells(xlCellTypeBlanks).EntireRow.delete
End Sub
It accepts a column number as an argument.