How to Use Class Interfaces in Excel VBA

How to Use Class Interfaces in Excel VBA

Excel Macro Mastery

4 года назад

77,779 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Nader Mounir
Nader Mounir - 10.09.2023 19:51

The video is very nice and I am learning from it.

Ответить
Shane Williams
Shane Williams - 28.08.2023 17:28

It was not too complicated,🙂 it was perfect!!

Ответить
Khalid Alisawi
Khalid Alisawi - 30.07.2023 11:33

thank you about this details

Ответить
ClintEJr
ClintEJr - 04.05.2023 21:01

Great video. I am starting to understand class modules and the implements feature. I hope to implement in the application I am working on. Thanks for your video on these topics.

Ответить
Marek Chudy
Marek Chudy - 15.04.2023 18:01

Thanks you

Ответить
Christopher King
Christopher King - 26.02.2023 08:37

Not wanting to clutter Classes Module with 1,000 interest types. Instantiate one oInterest class with interest type, it does all if calcs. Print function is not value-added. Debug.print oInterest.<property>" in VBA main. Do you have a useful elegant real-world example? I will think more also. BTW what I really want is Excel cell formula to give VBA Object property without a module wrapper function. Thx.

Ответить
aszarek
aszarek - 03.02.2023 16:34

Hi,
you're the ultimate-VBA Boss!
I program in VBA since 1998, in Excel and AutoCAD. Till now I thought Im quite good at it. Now I have to change my mind ;)
Thank you!

Ответить
DoOver2006
DoOver2006 - 23.01.2023 18:06

Nice video, but to much jumping around.

Ответить
Cess Romer
Cess Romer - 15.01.2023 20:24

We implemented these interface to four readings from an electronic instrument and was fantastic. I created a class interface and read current, voltage dc and ac and frequency of the signal in real time from the created object. now i will correlate all these reading implementing a reading every second calling another time object from a generic class., thanks for the tips it was very helpful.

Ответить
Peter Swinoff
Peter Swinoff - 18.12.2022 14:56

I have been writing in VBA for many years and all that time I wanted to have this functionality in VBA as well as in other languages. But I thought that there were no interfaces in VBA. I have never seen them in any VBA code. Now I want to run through all of my alive projects and rewrite them all.

Ответить
Hot Flash Foto
Hot Flash Foto - 24.10.2022 14:34

It's going to take some experimenting to wrap my head around this. Thank you for the instruction!

I hate ElseIf. I tend to use Select Case. It's a lot cleaner and easier to understand, plus they can be used in "reverse".

For example:
Select Case True
Case intA = rngA
'do something
Case intB > fnTemp(67.5)
'do something else
Case rngC = "Excel Macro Mastery", "How to Use Class Interfaces in Excel VBA"
'do other stuff
Case blnQ (or if you'd rather, Case blnQ = True)
'do stuff if Boolean variable is T
Case Else
'oops!
End Select

In this example, it finds the first True statement in the order that you set. Using If/ElseIf would take a lot more work and be very hard to manage.

Ответить
Nicu Stanciu
Nicu Stanciu - 15.10.2022 18:54

i understand that the video is trying to showcase the power of interfaces, but wouldn't it be much easier in this scenario to handle all those cases with a 'Select Case' statement? Thanks Paul, great content.

Ответить
Leonardo
Leonardo - 12.10.2022 13:11

Great demonstration of class interface. Its a little bit a pity that VBA has not the power as JAVA in class programming. Thanks a lot. It will be nice to get more of these Videos to optimize and integrate Web as Interface from VBA.

Ответить
Eiger67
Eiger67 - 29.09.2022 15:03

Hi, thank you for this and all your amaizing videos/tutorials. In this example you say you'll keep it simple but it could be very complicated, and i asking me how much complicated could be.
To print the result in the debug window show how the code work, but how could we implement it to use it in all day tasks? Do plant to bring some real exaple they could be used in all day life? thank you again

Ответить
Naved Akhtar
Naved Akhtar - 05.08.2022 21:48

Hi Paul, i really appreciate the way you teach and explain the code, can you make a video how to import Text from a particular layer of AutoCAD using VBA, it would be great help to understand. i see there are lot of code made in LSP but i want to learn how to do this in VBA.

Ответить
DD1876
DD1876 - 21.07.2022 11:15

Hello and thank you!
But if the class A must have a function of its own, we are still forced to declare it in the interface? or there is a trick?
It bothers me to have a SpecificToA() function available in the implementation of B....

Ответить
Adam On Journey
Adam On Journey - 15.07.2022 23:44

So glad about this video. Thanks a lot! Would you mind to make another one about events in VBA? Maybe the last missing piece in this vba collection of gems. :)

Ответить
Valentin Neufeld
Valentin Neufeld - 30.04.2022 22:47

Nice

Ответить
Hadi
Hadi - 19.04.2022 20:54

👍👍 great video. interfaces are little known because of most VBA programmers learned to do things related to the workbooks and worksheets in a straight forward way, pure VB6 devs used the object oriented aspect of VB in a more advanced programmers way. still C++ dev were a bit disappointed not having all the OO features they used to have. btw not sure if the overload of operators is possible in VBA?

Ответить
Robert Stiekema
Robert Stiekema - 04.03.2022 23:49

Mind = blown

Ответить
Matt Gregoire
Matt Gregoire - 12.02.2022 06:26

Holly crap! I just leveled up today!

Ответить
Rice Blues
Rice Blues - 01.12.2021 13:53

Cool, except that the classfactory should be a class itself.

Ответить
Conan
Conan - 20.11.2021 12:26

No need for classes no need for interface, just ointerest=result(amount,interestType) and a function "result" with all the interest types, no 3 times the same printing routine needed.

Ответить
O
O - 16.11.2021 16:44

i have a question cant we just do these things with udf why we need class
and cant we use sometimes arrays instead of class ?
thanks in advance

Ответить
reevesjim
reevesjim - 08.11.2021 21:21

This is a type of video i think i will benefit from if i watch it a couple of more times. Thank you!

Ответить
Johny Why
Johny Why - 25.10.2021 09:09

Confused. You said "VBA will go to the correct type, whether it's A or B." But Excel doesn't magically "figure it out". That only happens because you EXPLICITLY DEFINE oInterest to be clsInterestA or clsInterestB. Also, the Class Factory doesn't "create a class", you created the class. The factory creates an object instance of a class.

Ответить
fun for kids
fun for kids - 19.10.2021 06:28

very nice !

Ответить
warm day
warm day - 16.09.2021 14:28

Whenever I use classes, I prefer not pass pass arguments.

Instead, I make use of the properties.

So in your code, you have:

' Calculate the interest

Sub iInterest_Calculate(ByVal amount As Double)
m_Amount = amount * 1.1
End Sub

I have adapted it like this:

iInterest looks like this:

Private m_Amount As Double

Sub Calculate()
End Sub

Sub PrintResult()
End Sub

Public Property Get Amount() As Double
Amount = m_Amount
End Property

Public Property Let Amount(ByVal A As Double)
m_Amount = A
End Property

clsInterestA looks like this:

Implements iInterest

Private m_Amount As Double

' Calculate the interest

Sub iInterest_Calculate() '*** THE MAIN DIFFERENCE, DOESN;T TAKE ARGUMENT.
m_Amount = iInterest_Amount * 1.1
End Sub

' Print the result to the Immediate Window

Sub iInterest_PrintResult()
Debug.Print TypeName(Me) & ": " & iInterest_Amount
End Sub

Public Property Get iInterest_Amount() As Double
iInterest_Amount = m_Amount
End Property

Public Property Let iInterest_Amount(ByVal A As Double)
m_Amount = A
End Property

The For Next loop in modMain is now:

Dim i As Long, result As Double
For i = 2 To rg.Rows.Count

' read the current row to variables

Amount = rg.Cells(i, 1).Value
interestType = rg.Cells(i, 2).Value

' Get the interest object

Set oInterest = ClassFactory(interestType)

' Calculate the interest

oInterest.Amount = Amount '*** ADDED THIS, SO DOESN'T PASS ARGUMENT TO CALCULATE

oInterest.Calculate

' some code

' Print the interest to the Immediate Window

oInterest.PrintResult

Next i

It returns the same results as yours.

Can you tell me if my way is just different (not passing arguments to Calculate) or is it more work for no extra benefit?

Ответить
Anon67
Anon67 - 10.08.2021 23:42

Great explanation.
I’m wondering if it is necessary to set the oInterest object to Nothing at the end of the class factory or does VBA automatically do this?
Also before the code was copied to the class factory, what happens to the old oInterestA objects when a New one is created from the clsInterestA class? Do they lose scope and that area of memory then becomes reusable?

Ответить
Giang Trọng
Giang Trọng - 31.07.2021 07:49

Thank you so much! It help me a lot to know more about Interface in VBA.

Ответить
Jon Kirk
Jon Kirk - 23.07.2021 03:02

This is incredibly useful. Thanks very much.

Ответить
Александр Ганов
Александр Ганов - 22.07.2021 12:04

surprised, never knew that VBA can implement interfaces or classes methods/properties although i use classes in my practice. As it's spoken: live and learn forever. Thanks a lot. The best VBA channel I've ever met

Ответить
Richard Bannister
Richard Bannister - 29.05.2021 11:49

I've learnt a lot from your videos but this one seems odd. Wouldn't it be better to have 1 class that handles all interest types? Then adding in more types means just adding to 1 class module.

Ответить
Jing Wang
Jing Wang - 16.05.2021 16:57

Wouldn't it be better to just create one class and move the if statements to the class function?

Ответить
Steven Nye
Steven Nye - 14.05.2021 16:01

Your work is very helpful, when I first see your videos, I think that is very interesting, but don't necessarily have a problem that needs the solution, several months later, the light bulb goes off, Oh...that is where this is useful.

Ответить
Phat
Phat - 10.05.2021 13:54

Finally I found someone who’s using OOP in VBA. After several tutorials on VBA I thought that there’s no OOP in VBA at all.

Ответить
vivek sreekumar
vivek sreekumar - 20.04.2021 10:23

And here I was thinking I knew a lot about VBA. Man was I wrong.

Ответить
Carl Runge
Carl Runge - 11.04.2021 11:58

Brilliant! I code in Microsoft Access VBA but the principles are the same. Really struggled with Interfaces before seeing this video!

Ответить
shiva sankar
shiva sankar - 02.04.2021 07:29

Interfaces in VBA is something nobody thinks of and actually lot of VBA developers don't know about it as me. How do you discover all these things and hats off to your research and patience. Very very well done. Thank you and hope see more videos like these

Ответить
Userme
Userme - 30.03.2021 00:26

can you tell me please, why I need to learn Object oriented concept before or as a prerequisite to learn VBA? Can you give me an example from real life as an analogy so I can understand it better? Thank you

Ответить
Philip McDonnell
Philip McDonnell - 26.03.2021 21:29

I think I'm finally starting to understand this....famous last words.

Ответить
Ja Go
Ja Go - 24.02.2021 14:46

¡Chapeau! Or if you prefer:"Me quito el sombrero"...

Ответить
Gilles Chercuitte
Gilles Chercuitte - 19.02.2021 23:38

All your videos are too fast. When explain a part of the code you often go from one part of the window to another and the audience has just about a second to focus on the code and listen to the explanation which mean you’ve missed just about everything. You know the code and techniques by heart but for the many in the audience, we are seeing both for the first time. I think if you take a little more time to pause at different screen shots it would be much easier to follow. These actions happen so fast and repeatedly that it even makes it impossible to pause the video and keep track of what’s going on. I don’t know how successful you are with these instructional videos but I find them took quick, and I have been programming in VBA for quite some time.

Your methods are great and really help to creat good code. I’ve worked on VBA programs with basically thousands of line of code in different workbooks including VBA add-ins and I can’t tell you how many hours were spent to sift through jumbled code to make improvement, to change functionality, or fix problems. Thanks for your fine work in conveying these lessons.

Ответить
Ray Ng
Ray Ng - 15.02.2021 20:12

Amazing video. :)
Can you think of a way to infer class type without using factories? That's the only non scalable part of the solution. Perhaps using reflections of some sort ?

Ответить
sedat aksakal
sedat aksakal - 27.01.2021 20:30

Teşekkür ederim. Harikasınız :)

Ответить
Anandh Raj
Anandh Raj - 06.01.2021 18:42

Thanks for the video. It is new and exciting but I have a question. Why I can't use more than one variable in the interface?
It worked perfectly when I used 1 variable but when I tried to use 2 or more, it gives me an error "Expected: =". I can't understand why. Can you please explain?

Ответить
scouter3000
scouter3000 - 18.12.2020 08:49

Great tutorial ! However, when I click on the suggested link for the code, I only get a blank page. Any thoughts ?

Ответить
Munim Rashid
Munim Rashid - 14.12.2020 22:31

I had no idea and didn't expect VBA to have Interface like full-fledged OOP languages (although isn't a complete match) and I am sure too few people are aware of it's existence in VBA. Paul's enthusiasm in exploring both vertically and horizontally has touched every possible segments of VBA. Having confidence on teachers credentials/abilities important for learners and Paul has earned that very successfully.

Ответить