variable inside an if formula vba help!@ easy fixi

jordanburch

Active Member
Joined
Jun 10, 2016
Messages
440
Office Version
  1. 2016
HI All,

I have the below. It seems like an east fix I just can figure out the syntax. Can anyone help?
Its the variable parts that wrong and I cant figure it out. I keep trying different combinations with the variable inside the formula but can get it right.

Any ideas?

Jordan


Range("h2:h10000").SpecialCells(xlCellTypeVisible).Formula = "=+IF(RC[-6]=""" & Right(ThisWorkbook.Worksheets("Variables").Range("A10").Value, 2) & "/" & Left(ThisWorkbook.Worksheets("Variables").Range("A10").Value, 3) & """ , RC[-1],(RC[-1]*-1))"
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The easy way to find out where the error is when you are writing formula with vba is to remove the first equals sign so that you are just writing text to the cell. Then after you have run the vba, select the cell and put the equals sign back in, Excel will then tell you where the error is
 
Upvote 0
The easy way to find out where the error is when you are writing formula with vba is to remove the first equals sign so that you are just writing text to the cell. Then after you have run the vba, select the cell and put the equals sign back in, Excel will then tell you where the error is
I tried that and it just errors out right before the first =. It does not show like I thought it would
 
Upvote 0
your problem is ( or at least the one I have spotted) is that you have got your double quotes wrong. this is a problem when you are writing formula ( which are basically text) when you want to include double quotes in the formula, excel treats them as the start and end of the text string. To get around this and to make the final string easier to differentiate between the uses of the double quotesd i always use the ascii chr(34) function to put double quotes into the string. I have started to change your code doing this to show you what the problem is and how to solve it:
VBA Code:
Range("h2:h10").Formula = "+IF(RC[-6]=" & Chr(34) & " & Right(ThisWorkbook.Worksheets(" & Chr(34) & "Variables" & Chr(34) & ".Range"
 
Upvote 0
What is the final formula meant to be? As that code works for me & I get
Excel Formula:
=IF(B31="23/abc", G31,(G31*-1))
 
Upvote 0
your problem is ( or at least the one I have spotted) is that you have got your double quotes wrong. this is a problem when you are writing formula ( which are basically text) when you want to include double quotes in the formula, excel treats them as the start and end of the text string. To get around this and to make the final string easier to differentiate between the uses of the double quotesd i always use the ascii chr(34) function to put double quotes into the string. I have started to change your code doing this to show you what the problem is and how to solve it:
VBA Code:
Range("h2:h10").Formula = "+IF(RC[-6]=" & Chr(34) & " & Right(ThisWorkbook.Worksheets(" & Chr(34) & "Variables" & Chr(34) & ".Range"
VBA Code:
 Range("h2:h10000").SpecialCells(xlCellTypeVisible).Formula = "=+IF(RC[-6]=" & Chr(34) & " & Right(ThisWorkbook.Worksheets(" & Chr(34) & "Variables" & Chr(34) & ".Range("A10").Value, 2) & "/" & Left(ThisWorkbook.Worksheets("Variables").Range("A10").Value, 3) & """ , RC[-1],(RC[-1]*-1))"

So like the above? its still not working for whatever reason? Thanks for your help!
 
Upvote 0
Then in what way isn't it working for you?
 
Upvote 0
Theres
Then in what way isn't it working for you?
something wrong with the quotes or double quotes.
Then in what way isn't it working for you?
I could not figure out the syntax for an array. I have the below. Can you please help? I simplified it.

VBA Code:
ActiveSheet.Range("$A$1:$BA$10000").AutoFilter Field:=2, Criteria1:=Array( _
         (Right(thisworkbook.worksheets("Variables").Range("A10").value, 2)-2), (Right(thisworkbook.worksheets("Variables").Range("A10").value, 2)-1), (right(thisworkbook.worksheets("Variables").Range("A10").value, 2)), Operator:=xlFilterValues

This part above I cannot figure out.



this part below works
VBA Code:
  Columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("H489").Select
    ActiveCell.FormulaR1C1 = "=+-RC[-1]"
    Range("H489").Select
    Selection.FillDown

Any help is appreciated thanks Fluff!
 
Upvote 0
That is a totally different & so needs a new thread. Have you got the formula sorted out?
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,330
Members
449,155
Latest member
ravioli44

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