VBA Formula Code Won't Work

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
816
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Code:
Sub Rectangle1_Click()

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

End Sub
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Alternatively,

Code:
Sub Rectangle1_Click()

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

End Sub
 

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
816
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?
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,958
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
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
 

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
816
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,455
Members
417,025
Latest member
MusterDuster

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