Simple Macro won't Work

waldos

New Member
Joined
Jun 16, 2002
Messages
13
Im going nuts trying to learn this VBA
I have put a button on the tool bar with the following code
Sub CommandButton_Click
If B10 = A3 then
D1 = 2 else
D1 = 5
End If End Sub

Half hour later I still Can't get it to work.
What am I not doing?
Any help greatly appreciated
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Swamp Thing

Active Member
Joined
Aug 16, 2002
Messages
313
You can't use cell addresses (e.g. B2, C4) directly in vba. The entire excel application, consisting of workbooks, worksheets, cells, charts etc etc etc is visible to VBA as a hierarchical collection of objects. You have to manipulate excel's features using these objects and their properties.

For example, a group of cells is a "Range" object in VBA.

Your code will be like this (note use of range object):<pre>
Sub CommandButton_Click
If Range("B10") = Range("A3") Then
Range("D1") = 2
Else
Range("D1") = 5
End If
End Sub</pre>

To get started on your VBA adventure, try recording some macros of simple operations using the macro recorder (Tools > Macros > Record Macro) and study the resulting code.
This message was edited by Swamp Thing on 2002-09-04 01:51
 

Jeff in Holland

New Member
Joined
Jul 22, 2002
Messages
33
This was a great learning experience for me thanks! I do need a little explanation on something though, what does the ELSE do? I plugged the macro in and set A3 and B10 equal and it entered 2 in D1, but what does the else D5 do? Also, it ran once automatically, but after that I had to physically press the run button to get it to go. Is there a way to set it up so that it just runs continuously?
 

Forum statistics

Threads
1,143,613
Messages
5,719,706
Members
422,242
Latest member
hishamkhatri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top