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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,611
Messages
6,125,829
Members
449,266
Latest member
davinroach

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