Set a type in VBA to nothing?

calendar_today Asked Mar 20, 2012
thumb_up 22 upvotes
history Updated April 16, 2026

Question posted 2012 · +17 upvotes

I have defined a variable with an own type, say

Dim point As DataPoint

Public Type DataPoint
   list as Collection
   name as String
   number as Integer
End Type

and I want to delete all values of the variable point at once. If it was a class, I would just use Set point = New DataPoint, or set Set point = Nothing, but how can I proceed if it’s a type?

Accepted answer +22 upvotes

You can benefit from the fact that functions in VB have an implicit variable that holds the result, and that contains the default type value by default.

public function GetBlankPoint() as DataPoint
end function

Usage:

point = GetBlankPoint()

2 code variants in this answer

  • Variant 1 — 2 lines, starts with public function GetBlankPoint() as DataPoint
  • Variant 2 — 1 lines, starts with point = GetBlankPoint()

Top vba Q&A (6)

+22 upvotes ranks this answer #16 out of 81 vba solutions on this site — top 20%.
vba