Formula syntax

Ezguy4u

Active Member
Joined
Feb 10, 2010
Messages
336
Office Version
  1. 365
Platform
  1. Windows
I have 2 worksheets. On sheet 1 I have D5 in cell D5. On sheet 2 I have D6 and D7 in cells D6 and D7. When I step thru the program, I get D5 in cell A1. I get D6 in cell A2. What I would like to do is combine the 2 methods and get D7 in cell A3. What I get is a run time error. It seems reasonable that I should be able to do this. Any help would be appreciated. Thank you for your time.

VBA Code:
Sub Formula1()

Range("A1").Formula = "=" & Cells(5, 4)

Range("A2").Formula = "=('" & Sheets(2).Name & "'!D6)"

Range("A3").Formula = "=('" & Sheets(2).Name & "' ""!"" '" & Cells(7, 4) & "')"

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I don't believe you can use 'cells(x,y)' in a formula.

You could do it like this:
VBA Code:
Sub Formula1()

Range("A1").Formula = "=" & Cells(5, 4)

Range("A2").Formula = "=('" & Sheets(2).Name & "'!D6)"

Range("A3").FormulaR1C1 = "=('" & Sheets(2).Name & "'!R7C4)"

End Sub
 
Upvote 0
I don't believe you can use 'cells(x,y)' in a formula.
You can if you use its Address property rather than the default property of Value.

@Ezguy4u
I'm not sure why, but you have included a heap more single & double quote marks in the last formula compared to the previous one. You also have parentheses in both the last two that are not required.
Try this for the last one.

VBA Code:
Range("A3").Formula = "='" & Sheets(2).Name & "'!" & Cells(7, 4).Address(0,0)

BTW, not a good idea to name a procedure with a word that vba already uses as part of its nomenclature (eg Formula1).
 
Upvote 0
Solution
Johnny, Your line does work. I will keep it in my toolbox. Peter, Your solution works the best. My 3rd solution was just to give people an idea of what I wanted. It's lik loking at a centance thts has spling errors and stlll know what thy want. Good advice on the naming procedure.
Thank you all for your time
 
Upvote 0
Yeah but my line of code requires less typing. :p
 
Upvote 0
You're welcome.
(I didn't understand one of the sentences in your post though. ;))
 
Upvote 0
My 3rd solution was just to give people an idea of what I wanted. It's lik loking at a centance thts has spling errors and stlll know what thy want.

Most probable conversion =:
My 3rd solution was just to give people an idea of what I wanted. It's like looking at a sentence that has spelling errors and stlll know what they want.
 
Upvote 0
You stlll know what I was saying though. ROTFLMAO
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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