ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   please give an example of a class module. (https://www.excelbanter.com/excel-programming/272512-please-give-example-class-module.html)

Mike[_33_]

please give an example of a class module.
 
someone please explain how to create a class in VBA
eg class named swap
Public swapdb As clsswap
Private m_number As Byte
Private m_units, m_notional, m_libor, cvalue As Double
Private m_name, m_ticker As String
Private m_td, m_exdate, m_r1, m_r2, m_r3, m_r4 As Date





Public Property Get addnumber() As Double
addnumber = m_number 'new swap #
End Property

Public Property Let addnumber(ByVal newnumber As Double)
m_number = newnumber
End Property
Public Property Get addunits() As Double
addunits = m_units 'new swap units
End Property

Public Property Let addunits(ByVal newunits As Double)
m_units = newunits
End Property
Public Property Get addname() As String
addname = m_name 'new swap name
End Property

Public Property Let addname(ByVal newname As String)
m_name = newname
End Property
Public Property Get addticker() As String
addticker = m_ticker 'new swap ticker
End Property

Public Property Let addticker(ByVal newticker As String)
m_ticker = newticker
End Property
Public Property Get addnotional() As Double
addnotional = m_notional 'new notional amount
End Property

Public Property Let addnotional(ByVal newnotional As Double)
m_ticker = newnotional
End Property
Public Property Get addlibor() As Double
addlibor = m_libor 'new libor rate
End Property

Public Property Let addlibor(ByVal newlibor As Double)
m_libor = newlibor
End Property
Public Property Get addtd() As Date
addtd = m_td 'trade date
End Property

Public Property Let addtd(ByVal newtd As Date)
m_td = newtd
End Property
Public Property Get addexdate() As Date
addexdate = m_exdate 'expiration date
End Property

Public Property Let addexdate(ByVal newexdate As Date)
m_exdate = newexdate
End Property
Public Property Get addr1() As Date
addr1 = m_r1 'reset date1
End Property

Public Property Let addr1(ByVal newr1 As Date)
m_r1 = newr1
End Property
Public Property Get addr2() As Date
addr2 = m_r2 'reset date2
End Property

Public Property Let addr2(ByVal newr2 As Date)
m_r2 = newr2
End Property
Public Property Get addr3() As Date
addr3 = m_r3 'reset date3
End Property

Public Property Let addr3(ByVal newr3 As Date)
m_r3 = newr3
End Property
Public Property Get addr4() As Date
addr4 = m_r4 'reset date4
End Property

Public Property Let addr4(ByVal newr4 As Date)
m_r4 = newr4
End Property

Dim i As Byte
'i = 1
Public swapi As New swapdb

Public Sub addswap()
swapi.addnumber = Range("b2")
swapi.addname = Range("b4")
swapi.addtd = Range("b6")
swapi.addexdate = Range("b8")
swapi.addunits = Range("b10")
swapi.addnotional = Range("b12")
swapi.addlibor = Range("b14")
swapi.addticker = Range("b16")
swapi.addr1 = Range("b18")
swapi.addr2 = Range("b20")
swapi.addr3 = Range("b22")
swapi.addr4 = Range("b24")
end sub

what am i missing?

Thanks

patrick molloy

please give an example of a class module.
 
be careful with your DIM statements!
You have

Private m_units, m_notional, m_libor, cvalue As Double
Private m_name, m_ticker As String
Private m_td, m_exdate, m_r1, m_r2, m_r3, m_r4 As Date

in this you assume m_units is double and mlibor is
Double. They are not, you have defaulted them to Variant.
M-name is variantand only the last date is Date.

You should have

Private m_units As Double, m_notional As Double, m_libor
As Double, cvalue As Double
Private m_name As String, m_ticker As String
Private m_td As Date, m_exdate As Date, m_r1 As Date,
m_r2 As Date, m_r3 As Date, m_r4 As Date


Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
The following seems to work, if there are no wrong

inputtypes

Normal Module:

Public clsSwapi As New swapDB

Public Sub addswap()
Dim strMsg As String
Set clsSwapi = New swapDB
With clsSwapi
.addnumber = ActiveSheet.Range("B2").Value2
.addname = ActiveSheet.Range("B4").Value2
.addtd = ActiveSheet.Range("B6").Value2
.addexdate = ActiveSheet.Range("B8").Value2
.addunits = ActiveSheet.Range("B10").Value2
.addnotional = ActiveSheet.Range("B12").Value2
.addlibor = ActiveSheet.Range("B14").Value2
.addticker = ActiveSheet.Range("B16").Value2
.addr1 = ActiveSheet.Range("B18").Value2
.addr2 = ActiveSheet.Range("B20").Value2
.addr3 = ActiveSheet.Range("B22").Value2
.addr4 = ActiveSheet.Range("B24").Value2
strMsg = strMsg & .addnumber & vbCrLf
strMsg = strMsg & .addname & vbCrLf
strMsg = strMsg & .addtd & vbCrLf
strMsg = strMsg & .addexdate & vbCrLf
strMsg = strMsg & .addunits & vbCrLf
strMsg = strMsg & .addnotional & vbCrLf
strMsg = strMsg & .addlibor & vbCrLf
strMsg = strMsg & .addticker & vbCrLf
strMsg = strMsg & .addr1 & vbCrLf
strMsg = strMsg & .addr2 & vbCrLf
strMsg = strMsg & .addr3 & vbCrLf
strMsg = strMsg & .addr4 & vbCrLf
End With
MsgBox strMsg, vbInformation, "Your Class"
End Sub

ClassModule swapDB:

Private m_number As Double 'cant have Byte=Double or

convert !
Private m_units, m_notional, m_libor, cvalue As Double
Private m_name, m_ticker As String
Private m_td, m_exdate, m_r1, m_r2, m_r3, m_r4 As Date


Public Property Get addnumber() As Double
addnumber = m_number 'new swap #
End Property

Public Property Let addnumber(ByVal newnumber As Double)
m_number = newnumber
End Property

Public Property Get addunits() As Double
addunits = m_units 'new swap units
End Property

Public Property Let addunits(ByVal newunits As Double)
m_units = newunits
End Property

Public Property Get addname() As String
addname = m_name 'new swap name
End Property

Public Property Let addname(ByVal newname As String)
m_name = newname
End Property

Public Property Get addticker() As String
addticker = m_ticker 'new swap ticker
End Property

Public Property Let addticker(ByVal newticker As String)
m_ticker = newticker
End Property

Public Property Get addnotional() As Double
addnotional = m_notional 'new notional amount
End Property

Public Property Let addnotional(ByVal newnotional As

Double)
m_ticker = newnotional
End Property

Public Property Get addlibor() As Double
addlibor = m_libor 'new libor rate
End Property

Public Property Let addlibor(ByVal newlibor As Double)
m_libor = newlibor
End Property

Public Property Get addtd() As Date
addtd = m_td 'trade date
End Property

Public Property Let addtd(ByVal newtd As Date)
m_td = newtd
End Property

Public Property Get addexdate() As Date
addexdate = m_exdate 'expiration date
End Property

Public Property Let addexdate(ByVal newexdate As Date)
m_exdate = newexdate
End Property

Public Property Get addr1() As Date
addr1 = m_r1 'reset date1
End Property

Public Property Let addr1(ByVal newr1 As Date)
m_r1 = newr1
End Property

Public Property Get addr2() As Date
addr2 = m_r2 'reset date2
End Property

Public Property Let addr2(ByVal newr2 As Date)
m_r2 = newr2
End Property

Public Property Get addr3() As Date
addr3 = m_r3 'reset date3
End Property

Public Property Let addr3(ByVal newr3 As Date)
m_r3 = newr3
End Property

Public Property Get addr4() As Date
addr4 = m_r4 'reset date4
End Property

Public Property Let addr4(ByVal newr4 As Date)
m_r4 = newr4
End Property


(Mike) wrote:

someone please explain how to create a class in VBA
eg class named swap
Public swapdb As clsswap
Private m_number As Byte
Private m_units, m_notional, m_libor, cvalue As Double
Private m_name, m_ticker As String
Private m_td, m_exdate, m_r1, m_r2, m_r3, m_r4 As Date





Public Property Get addnumber() As Double
addnumber = m_number 'new swap #
End Property

Public Property Let addnumber(ByVal newnumber As Double)
m_number = newnumber
End Property
Public Property Get addunits() As Double
addunits = m_units 'new swap units
End Property

Public Property Let addunits(ByVal newunits As Double)
m_units = newunits
End Property
Public Property Get addname() As String
addname = m_name 'new swap name
End Property

Public Property Let addname(ByVal newname As String)
m_name = newname
End Property
Public Property Get addticker() As String
addticker = m_ticker 'new swap ticker
End Property

Public Property Let addticker(ByVal newticker As String)
m_ticker = newticker
End Property
Public Property Get addnotional() As Double
addnotional = m_notional 'new notional amount
End Property

Public Property Let addnotional(ByVal newnotional As

Double)
m_ticker = newnotional
End Property
Public Property Get addlibor() As Double
addlibor = m_libor 'new libor rate
End Property

Public Property Let addlibor(ByVal newlibor As Double)
m_libor = newlibor
End Property
Public Property Get addtd() As Date
addtd = m_td 'trade date
End Property

Public Property Let addtd(ByVal newtd As Date)
m_td = newtd
End Property
Public Property Get addexdate() As Date
addexdate = m_exdate 'expiration date
End Property

Public Property Let addexdate(ByVal newexdate As Date)
m_exdate = newexdate
End Property
Public Property Get addr1() As Date
addr1 = m_r1 'reset date1
End Property

Public Property Let addr1(ByVal newr1 As Date)
m_r1 = newr1
End Property
Public Property Get addr2() As Date
addr2 = m_r2 'reset date2
End Property

Public Property Let addr2(ByVal newr2 As Date)
m_r2 = newr2
End Property
Public Property Get addr3() As Date
addr3 = m_r3 'reset date3
End Property

Public Property Let addr3(ByVal newr3 As Date)
m_r3 = newr3
End Property
Public Property Get addr4() As Date
addr4 = m_r4 'reset date4
End Property

Public Property Let addr4(ByVal newr4 As Date)
m_r4 = newr4
End Property

Dim i As Byte
'i = 1
Public swapi As New swapdb

Public Sub addswap()
swapi.addnumber = Range("b2")
swapi.addname = Range("b4")
swapi.addtd = Range("b6")
swapi.addexdate = Range("b8")
swapi.addunits = Range("b10")
swapi.addnotional = Range("b12")
swapi.addlibor = Range("b14")
swapi.addticker = Range("b16")
swapi.addr1 = Range("b18")
swapi.addr2 = Range("b20")
swapi.addr3 = Range("b22")
swapi.addr4 = Range("b24")
end sub

what am i missing?

Thanks


.



All times are GMT +1. The time now is 12:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com