Dim Cell. no "as"

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I would appreciate if you can help me understand the code below
1) What does Dim Cell mean? why "as" was omitted
2) What does "&gt" do here?
3) Is these easier implementation of similar code? Thank you.


Code:
Sub Highlight_Duplicates(Values As Range)
Dim Cell
 
For Each Cell In Values
    If WorksheetFunction.CountIf(Values, Cell.Value) > 1 Then
        Cell.Interior.ColorIndex = 6
    End If
 
Next Cell
End Sub

https://www.automateexcel.com/vba/highlight-duplicates-in-range/
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Dim Cell (type omitted) is equivalent to Dim Cell as Variant

& gt ; (without spaces) is the HTML equivalent to > , that is greater than

M.
 
Last edited:
Upvote 0
Sorry another question. How to call that sub?
I create a button in my excel sheet and tried to "Assign" to macro but the macro was not in the list!
Another thing, I just noticed that this macro has an argument!
Thank you.
 
Upvote 0
Alt+F8 to show the Macros dialog.

You know you can do this with conditional formatting?
 
Upvote 0
You can't pass a Range object using a macro assigned to a button. You'd need to change the argument to say a String and then pass the address of the range.
 
Upvote 0
Thanks all for your help. I inserted button. I changed the argument to String but then when I right clicked on the button to assign macro, I do not see any macro. Then I removed the argument from the macro and now I can see the macro.
My question, how can I pass an argument using button. if not possible, then how can I call that macro. Thanks alot.
 
Upvote 0
Thanks all for your help. I inserted button. I changed the argument to String but then when I right clicked on the button to assign macro, I do not see any macro. Then I removed the argument from the macro and now I can see the macro.
My question, how can I pass an argument using button. if not possible, then how can I call that macro. Thanks alot.
You cannot pass an argument into a macro... the lack of an argument is what makes it a macro, otherwise you have a normal subroutine which can only be called from other VB code.

Originally, how exactly were you planning to pass an argument into the code from a button push???
 
Last edited:
Upvote 0
You can select the range and assign this macro to the button

Code:
Sub Highlight_Duplicates()
    Dim Cell
 
    For Each Cell In Selection
        If WorksheetFunction.CountIf(Selection, Cell.Value) > 1 Then
            Cell.Interior.ColorIndex = 6
        End If
     
    Next Cell
End Sub

M.
 
Upvote 0
You cannot pass an argument into a macro... the lack of an argument is what makes it a macro, otherwise you have a normal subroutine which can only be called from other VB code.
Of course, you need to be careful of nomenclature here - people often use "Macro" and "VBA" interchangeably. In VBA, you can have "Procedures" and "Functions".
You can pass arguments to both Procedures and Functions, but maybe not in all instances.

I am not sure how one would pass an argument to a Procedure that is called by a button, if that is possible at all.
Where would the value come from?

I think the following suggests that you cannot call any procedure that has an input argument with a button:
I changed the argument to String but then when I right clicked on the button to assign macro, I do not see any macro. Then I removed the argument from the macro and now I can see the macro.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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
Back
Top