Formulas

finaljustice

Board Regular
Joined
Oct 6, 2010
Messages
175
Hello all, this is probably a really quick fix but, i've been trying to insert a formula through vba into a cell.
Code:
Range("C2").Formula = "=MID(B2;FIND(" - ";B2;1)+2;LEN(B2)-FIND(" - ";B2;1)-5)"

For some reason I keep getting a error.
any suggestions to solve this?

Thanks a million.
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
You have to use double quotes ("") to use quotes in strings.

VBA reads your code:

Code:
"=MID(B2;FIND(" - ";B2;1)+2;LEN(B2)-FIND(" - ";B2;1)-5)"
as "=MID(B2;FIND(" minus ";B2;1)+2;LEN(B2)-FIND(" minus ";B2;1)-5)"

Use single quotes at the beginning of the string and doubles within:

Code:
"=MID(B2;FIND("" - "";B2;1)+2;LEN(B2)-FIND("" - "";B2;1)-5)"
 
Upvote 0
You have to use double quotes ("") to use quotes in strings.

VBA reads your code:

Code:
"=MID(B2;FIND(" - ";B2;1)+2;LEN(B2)-FIND(" - ";B2;1)-5)"
as "=MID(B2;FIND(" minus ";B2;1)+2;LEN(B2)-FIND(" minus ";B2;1)-5)"

Use single quotes at the beginning of the string and doubles within:

Code:
"=MID(B2;FIND("" - "";B2;1)+2;LEN(B2)-FIND("" - "";B2;1)-5)"


Hi thanks for replying, but now im getting a new error.
I'm translating this the best I can but the msg is:
"Error on execution time '1004':
Error of defining an application or defining an object."

Here is my macro:
Code:
Sub teste()
     Range("A1").Select
     Range("C2").Formula = "=MID(B2;FIND("" - "";B2;1)+2;LEN(B2)-FIND("" - "";B2;1)-5)"
 
End Sub
 
Upvote 0
I didn't even notice this late last night... but you used semicolons.

Excel formulas have their arguments separated by commas, just replace the semicolons with commas.
 
Upvote 0
I didn't even notice this late last night... but you used semicolons.

Excel formulas have their arguments separated by commas, just replace the semicolons with commas.

Perfect, that works perfectly! I thought that I should use the same separators as the one used in cells, ergo the ";".

Now I got it. Thanks alot!
 
Upvote 0
I didn't even notice this late last night... but you used semicolons.

Excel formulas have their arguments separated by commas, just replace the semicolons with commas.

Hi could you shine a light for me on a quick issue? This is an exmple:

Code:
Sub formulas()
'Determine the range
x = ActiveWorkbook.Sheets.Count
Sheets(x).Select
ws = ActiveSheet.Name
blimit = Range("D1048576").End(xlUp).Address
blimit = Right(blimit, 3)
Sheets(1).Select
Range("A27").Select
Do While ActiveCell = Empty
varlookup = ActiveCell.Address
linha = Trim(Right(varlookup, 2))
Range("$D$" & linha).Formula = "=Iferror(VLookup(" & varlookup & ", " & ws & "!$D$6:$AR$" & blimit & ", 2, False),"""")" 
Range("$K$" & linha).Formula = "=Iferror(VLookup(" & varlookup & ", " & ws & "!$D$6:$AR$" & blimit & ", 31, False),"""")"
Range("$M$" & linha).Formula = "=Iferror((VLookup(" & varlookup & ", " & ws & "!$D$6:$AR$" & blimit & ", 7, False)*"$K$" " & linha & " ,"""")"  'this is where I am having an issue. Now I am trying to multiply by cell $K$&linha (linha = the line variable), this is where the issue happens: *"$K$" " & linha & "
ActiveCell.Offset(1, 0).Select


Loop


End Sub


Any idea how to code it, i've been trying to do this for sometime.
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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