User login

vba

Remove Last Character From A String

This VBA function will remove the last character from a string:

Function CutOffLastChar(str As String)
CutOffLastChar = Left(str, Len(str) - 1)
End Function

Use Dictionary From VBA

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"

Write to File in VBA

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

Enumerate Range Values

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

Remove Characters From End of String

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"

DOM Tree Walk

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

Check For Null or Empty String

I found the following piece of code effective:

If str = vbNullString Then ...


If there is an error in a cell formula, like from a VLOOKUP the way around it is to use the IsError function:

If Not IsError(ActiveWorkSheet.Range("A1")) Then ...


Database Null

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

Insert Data from Excel to SQL Server

This article will show you how to read data from an Excel spreadsheet and insert it into a SQL Server database.

A VBA Function to Get Last Date of the Week


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

A VBA function That Deletes Empty Rows

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.

Syndicate content