Monday, 21 March 2016

Visual Basic with MS Excel: a Button, a Label, and a Field

VB seems to call fields Text Boxes and Buttons Command Buttons. Basically, all is found inside of the Toolbox

We will now tell you how you can quickly write a routine to have something pleasant on your screen.

First, you start a new sheet in Excel, then you activate the Developer's Tab. After you press the button Visual Basic on this tab, you should see the VB interface. Once you are in it, you have to press the second button to the bottom of your ribbon at the top of the page if you have a standard installation. By clicking on that button, you will have access to four options: User Form, Module, Class Module, and Procedure. Click on User Form to get an interface where the user will be able to interact with your program in a very friendly way. You will then use the Toolbox in order to be able to select the option Text Box. Go to the rectangle that appears in front of you when you press User Form and Mouse Over it in the shape you want the user to see the button. Give this box a Control C + Control V sequence of clicks in order to have a perfect copy of the field inside of the rectangle. Now organize it nicely. Select Command Box in the Toolbox and Mouse Over the rectangle again to again create the shape you wish. Select, in last place, Label in the Toolbox, and repeat the actions. 

Now, Mouse Over the Button, and Label to activate the writing and change it into whatever you like, so say Transfer and Result. 

You now have to go to one of the edges of one of the items, so say one of the fields, put the mouse over it, and wait until it gets activated, when you will then click on View Code. That is the same as Edit Code, actually. 

You'd better click over the edge of the Button because that is where you will insert all the commands you need for this routine. In this way, you will end up inside of the right Sub

VB, when inside of Excel or in general, has default names for each one of those items. It will call the first field TextBox1, the second, TextBox2, the Button CommandButton1, and the Label, Label1

With this, type whatever you notice that is not in your program when you get the code in front of you considering the lines below. You will notice that, just like Cobol, VB declares variables at the top. It just uses different words to do that. String for alpha elements and Integer for numbers. You will also notice that it always gives you the first and the last line of the Sub you see below. Just take advantage of that and change names of the elements only if you have plenty. The way it concatenates strings is by adding a commercial and in the middle, as you can see below. The Caption of the Label ends up appearing over it when you execute the program. 

Dim Sum As String

Private Sub CommandButton1_Click()
Sum = TextBox1.Text & TextBox2.Text
Label1.Caption = Sum
End Sub

=> You don't need to tell me what I already know: That this page is worth millions. I know myself how much I struggled to find good material on the Internet to simply do this sort of bullshit. I will still see if I manage to add a video that can be well understood to put here for you, since that is another problem. I went through several and did not think they helped much. I ended up relying more on written material. 

Here you will see the result: 

And here you will see a few inside images:


Please help us keep on inspiring you

No comments:

Post a Comment