Beginner - Very Simple Code - Type mismatch

ride5150

New Member
Joined
Nov 22, 2023
Messages
2
Hi Everyone,

I am writing a VBA macro but am having a type mismatch error. I pulled out the rest of my code, and cannot get this very simple code to run. Not understanding why a whole number cell value cannot be compared with another. I changed the variable type to "Long" and am still having the error.

Private Sub CommandButton1_Click()

Dim j As Integer

Dim k As Integer

j = Cells(26, 5).Value

k = Cells(27, 5).Value

If [j = k] Then

Cells(28, 5).Value = j

End If

End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
I suspect it's your use of brackets not parentheses [j=k] instead of (j=k) (although you don't need them, you could just say j=k) but if that's all that code is doing, I would just do it in one line:

VBA Code:
If Range("E26") = Range("E27") Then Range("E28") = Range("E26")
 
Upvote 0
I suspect it's your use of brackets not parentheses [j=k] instead of (j=k) (although you don't need them, you could just say j=k) but if that's all that code is doing, I would just do it in one line:

VBA Code:
If Range("E26") = Range("E27") Then Range("E28") = Range("E26")
that worked, thank you!
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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