+ Reply to Thread
Results 1 to 5 of 5

Code for using a saved class module

  1. #1
    Registered User
    Join Date
    08-28-2014
    Location
    Chennai
    MS-Off Ver
    2013
    Posts
    3

    Code for using a saved class module

    Hi,

    I am a learner of excel vba. I have created a macro and saved as .cls file at a particular path. I am trying to create a new xlsm file, place a command button in it and use this saved class module on click of the command button created.

    I completed my work on creating a xlsm file, placing a command button over there and importing the saved class module. The module sits as "sheet11" under class module in the new xlsm file. How do I use that module's code to execute the macro by clicking the command button?

    Regards,
    Aravinth

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Code for using a saved class module

    Call it

    Call Sheet11.macroname

  3. #3
    Registered User
    Join Date
    08-28-2014
    Location
    Chennai
    MS-Off Ver
    2013
    Posts
    3

    Re: Code for using a saved class module

    This is the code I used

    Set wb1 = Workbooks.Add
    wb1.VBProject.VBComponents.Import "D:\vba\getohlc.cls"
    Sheets("sheet1").Select
    Set obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
    Link:=False, DisplayAsIcon:=False, Left:=200, Top:=100, Width:=100, Height:=35)
    obj.Name = "TestButton"
    'buttonn text
    ActiveSheet.OLEObjects(1).Object.Caption = "Test Button"

    code = "Private Sub CommandButton1_Click()" & vbCrLf
    code = code & "Call Sheet11.commandbutton1" & vbCrLf
    code = code & "End Sub"

    With ActiveWorkbook.VBProject.VBComponents("Sheet1").CodeModule
    .insertlines .countoflines + 1, code

    It returns the value inside the quotes (Call sheet11.commandbutton1) rather than pulling the code from sheet11

    Regards,
    Aravinth

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Code for using a saved class module

    What code do you have in the saved module?

    How are you using it before you save it?

    PS Can you add code tags when posting code?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Code for using a saved class module

    Why do you add the code on the fly, it seems totally pointless. Add it permanently.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating a class module with my VBA code
    By potterotter in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-01-2014, 11:44 AM
  2. Replies: 0
    Last Post: 03-08-2014, 08:02 AM
  3. [SOLVED] Convert code in a class module to a macro
    By Consty1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2013, 09:44 AM
  4. [SOLVED] ComboBox class .AddItem filled in class module
    By Jacques Grobler in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-03-2012, 05:48 AM
  5. Replies: 1
    Last Post: 08-30-2011, 02:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1