VBA development contact for two months, since the thought of the novice, encountered a lot of problems, but also some experience. According to the problems encountered in the development began to summarize. During the development process, there is a lot of temporary data stored in the process of running the program, which is only used for the next calculation and does not need to be rendered in the final result. The table class module was created for ease of use and management in later steps. class module: CTable
1 Option Explicit2 3 '====================================4 'Name: CTable5 'function: Describe an Excel table area6 '====================================7 8 PublicStrName as String 'Table name9 PublicStraddress as String 'AddressTen PublicRngstart asRange'Start Cell One PublicRngend asRange'End Cell A PublicIcolumns as String 'Number of columns
class module Name: CTable, the following public is the property of the class, this class seems to be no different from the custom type, yes, this is already a class module.
Normal module: Data
Option Explicit'============================'Name: CreateTable'function: Create a table'parameter: strtablename: Table name'astrcolumnnames: Column an array group'return: Table'============================FunctionCreateTable (ByRefstrTableName as String, Astrcolumnnames asVariant) asCTableDimClsstutab as NewCTable'declaring and creating an objectClsstutab.straddress ="sheet2! $A $" SetClsstutab.rngstart =Range (clsstutab.straddress) clsstutab.strname=strTableNameDimI, J withClsstutab.rngstart. Offset (0,0). Value =Clsstutab.strname J=0 fori =LBound(Astrcolumnnames) to UBound(Astrcolumnnames). Offset (1, j). Value =Astrcolumnnames (i) J= j +1 NextClsstutab.icolumns=JSetClsstutab.rngend =. Offset (1, J-1) End withClsStuTab.rngStart.Offset (0,1). Value =clsStuTab.rngEnd.AddressSetCreateTable =ClsstutabEnd FunctionSubCreate student table ()DimClsstudenttable asCTable'declaring an object DimAstrcolumnnames asVariant astrcolumnnames= Array ("ID","Name","Gender","Stuid","Class") SetClsstudenttable = CreateTable ("Student Table", Astrcolumnnames)End Sub
Operation Result:
VBA class module--Create TABLE Class (1)