VBA Formula Code Won't Work

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I wonder if someone could help. I have the following VBA code:

Sub Rectangle1_Click()

Name = "Mike"

Cells(4, 4) = "=if(a=1," & Name & ",2)"

End Sub


What I want it to give when I run it is:

=If( a=1, "Mike",2)

What I get is

=If( a=1, Mike,2)

i.e. it's looking for a range name called Mike.

Thanks,

MikeG
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Code:
Sub Rectangle1_Click()

   Const sName As String = "Mike"
   Cells(4, 4).Formula = "=IF(a=1," & Chr(34) & sName & Chr(34) & ",2)"

End Sub
 
Upvote 0
Alternatively,

Code:
Sub Rectangle1_Click()

   Const sName As String = "Mike"
   Cells(4, 4).Formula = "=IF(a=1,""" & sName & """,2)"

End Sub
 
Upvote 0
Code:
Sub Rectangle1_Click()

   Const sName As String = "Mike"
   Cells(4, 4).Formula = "=IF(a=1," & Chr(34) & sName & Chr(34) & ",2)"

End Sub

Thanks very much wigi - I got the first one working ok. Could you help me with one more thing.

I actually now want to make the variable mike itself based on a range in the sheet called mike so I tried


Const sName As String = Range("Mike")

but this did not work. Do you know what I am doing wrong?
 
Upvote 0
Maybe (depending oin sheet name and cell address):

Code:
Sub Rectangle1_Click()

   Dim sName As String
   sName = ThisWorkbook.Sheets("Mike").Range("A1").Value
   Cells(4, 4).Formula = "=IF(a=1," & Chr(34) & sName & Chr(34) & ",2)"

End Sub
 
Upvote 0
Maybe (depending oin sheet name and cell address):

Code:
Sub Rectangle1_Click()

   Dim sName As String
   sName = ThisWorkbook.Sheets("Mike").Range("A1").Value
   Cells(4, 4).Formula = "=IF(a=1," & Chr(34) & sName & Chr(34) & ",2)"

End Sub

Perfect!

I really appreciate the help.

MikeG
 
Upvote 0

Forum statistics

Threads
1,214,587
Messages
6,120,406
Members
448,958
Latest member
Hat4Life

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