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 71719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:55:55+00:00 2026-05-10T19:55:55+00:00

Is there a generic way to clone objects in VBA? So that i could

  • 0

Is there a generic way to clone objects in VBA? So that i could copy x to y instead of copying just the pointer?

  Dim x As New Class1   Dim y As Class1    x.Color = 1   x.Height = 1    Set y = x   y.Color = 2    Debug.Print 'x.Color=' & x.Color & ', x.Height=' & x.Height 

By generic i mean something like Set y = CloneObject(x) rather than having to create my own method for the class copying its properties one by one.

  • 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. 2026-05-10T19:55:55+00:00Added an answer on May 10, 2026 at 7:55 pm

    OK, here’s the beginning of something that illustrates it:

    Create a class, call it, oh, ‘Class1’:

    Option Explicit  Public prop1 As Long Private DontCloneThis As Variant  Public Property Get PrivateThing()     PrivateThing = DontCloneThis End Property  Public Property Let PrivateThing(value)     DontCloneThis = value End Property 

    Now we need to give it a Clone function. In another module, try this:

    Option Explicit

    Public Sub makeCloneable()  Dim idx As Long Dim line As String Dim words As Variant Dim cloneproc As String  ' start building the text of our new function     cloneproc = 'Public Function Clone() As Class1' & vbCrLf     cloneproc = cloneproc & 'Set Clone = New Class1' & vbCrLf      ' get the code for the class and start examining it         With ThisWorkbook.VBProject.VBComponents('Class1').CodeModule          For idx = 1 To .CountOfLines              line = Trim(.lines(idx, 1)) ' get the next line             If Len(line) > 0 Then                 line = Replace(line, '(', ' ') ' to make words clearly delimited by spaces                 words = Split(line, ' ') ' so we get split on a space                 If words(0) = 'Public' Then ' can't set things declared Private                     ' several combinations of words possible                     If words(1) = 'Property' And words(2) = 'Get' Then                         cloneproc = cloneproc & 'Clone.' & words(3) & '=' & words(3) & vbCrLf                     ElseIf words(1) = 'Property' And words(2) = 'Set' Then                         cloneproc = cloneproc & 'Set Clone.' & words(3) & '=' & words(3) & vbCrLf                     ElseIf words(1) <> 'Sub' And words(1) <> 'Function' And words(1) <> 'Property' Then                         cloneproc = cloneproc & 'Clone.' & words(1) & '=' & words(1) & vbCrLf                     End If                 End If             End If         Next          cloneproc = cloneproc & 'End Function'          ' put the code into the class         .AddFromString cloneproc      End With  End Sub 

    Run that, and the following gets added into Class1

    Public Function Clone() As Class1 Set Clone = New Class1 Clone.prop1 = prop1 Clone.PrivateThing = PrivateThing End Function 

    …which looks like a start. Lots of things I’d clean up (and probably will – this turned out to be fun). A nice Regular Expression to find gettable/lettable/settable attributes, refactoring into several small functions, code to remove old ‘Clone’ functions (and put the new one at the end), something a bit more Stringbuilder-ish to DRY (Don’t Repeat Yourself) up the concatenations, stuff like that.

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

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 58k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer In this case jQuery issues a POST request: POST /controller/view… May 11, 2026 at 8:34 am
  • added an answer C does not support any form of introspection or doing… May 11, 2026 at 8:34 am
  • added an answer Quote from W3C In a block formatting context, boxes are… May 11, 2026 at 8:34 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.