Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:08:34+00:00 2026-06-12T13:08:34+00:00

Experts out there – quick question… In Excel VBA, I have created a class

  • 0

Experts out there – quick question…

In Excel VBA, I have created a class which holds 4 string variables – No constructor and nothing else.

Now, in my code, I am looping through an ENORMOUS array (100k + iterations)
Constantly creating a new instance of the class, assigning values to its variables, using it then assigning it toMyClass = nothing. Then looping again:

Dim x As New MyClass
Set x = Nothing

For i = 0 to 1000000
   x.Var1 = "XYZ"
   x.Var2 = "abc"
   x.Var3 = "123"
   x.Var4 = 456

... Use x ...

Set x = Nothing

Next i

Given this usage, would it be more memory efficient to define x as a class or as a public type (or doesn’t it really make a difference)??

Thanks!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:08:35+00:00Added an answer on June 12, 2026 at 1:08 pm

    They way you have it written, using classes will definitely take longer than types (5x longer on my machine). This is because you’re constantly deallocating and reallocating memory when you destroy the class. You may not need to destroy the class though, just keep it in memory and change its properties. This

    Sub test3()
    
        Dim x As CMyClass
        Dim i As Long
        Dim lStart As Long
    
        lStart = Timer
    
        Set x = New CMyClass
    
        For i = 0 To 1000000
           x.Var1 = "XYZ"
           x.Var2 = "abc"
           x.Var3 = "123"
           x.Var4 = 456
        Next i
    
        Debug.Print Timer - lStart
    
    End Sub
    

    Took the same amount time as a UDT because the class is only created once and only destroyed when it goes out of scope. Unless there is some need to set it to Nothing – and you probably don’t if you can use a Type in its place – then you shouldn’t. And in that case, it looks like you can use either a Type or a Class and get the same performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey just a quick question for any experts out there. I have a site
Hey guys, one more quick question for any experts out there. I have a
I have a question for the front-end web development experts out there which is
Hey guys, quick question for any experts out there. I am allowing users to
Quick question for an ASP experts out there! I am reading a SQL database
I have a question to all you MySQL experts out there: Is it possible
I have a question for you linq experts out there! In a nested list
I have a basic question for all of the math experts out there. If
I have a very simple question for all of you Drupal experts out there:
Hope there are some GLKViewController experts out there because I have some problems :)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.