0

I want to do the multiplication as below, but keep getting an error. I know it's because the quotation marks are missing - probably at the end, but I don't know where to add it

Dim val1 As Double
Dim val2 As Double

' here will be IF Then 

val1 = Range("G" & i).Value
val2 = Range("H" & i).Value

'or
val1 = 2.1
val2 = Range("A150").value

'or
val1 = sheet("Anotherworksheet").Range("F" & lastRow).value
val2 = findValue() 'function return value

'it is not important where and what data I get


Range("D4").Formula = "=" & val1 & "*" & val2 
'formula is important because I need to know what values I get

*question updated

vipmaciej
  • 13
  • 10
  • 1
    Why not just use `Range("D4").Formula = "=G5*G6"` ? There are no quotation marks needed in a formula that has no string values. – braX Jan 29 '21 at 09:32
  • because ultimately cells will be variable (based on for i = 1 to lastRow) – vipmaciej Jan 29 '21 at 10:09
  • Then you may want to update your question that demonstrates this. It seems you may have oversimplified things. – braX Jan 29 '21 at 10:10
  • Note that `Range("D4:D100").Formula = "=G5*G6"` will write `=G5*G6, =G6*G7, =G7*G8...` in cells `D4, D5, D6`... respectively. It would be best you post the complete code with explanations what you are trying to achieve. An image or two (before, after) may also be helpful. – VBasic2008 Jan 29 '21 at 10:17
  • question updated, but I don't think it matters. Basically, I want to be able to multiply variable values ​​(val1, val2) and add them to the cell through a formula. – vipmaciej Jan 29 '21 at 10:26
  • If you can't show the complete code, could you explain what is wrong with your code? If the values are `3` and `4` then the formula in cell `D4` will be `=3*4` but the cell will display `12`. If the values are "Yes" and "No" the formula in cell `D4` will be `=Yes*No` but the cell will display `#Name?`. But it's not going to happen if you're trying to do this in a `UDF` (user-defined function). – VBasic2008 Jan 29 '21 at 12:08
  • `val1, val2` aren't variables containing cell addresses, so trying to put them in a spreadsheet formula rather than simply computing the product in code is unmotivated at best. They won't update automatically. What exactly are you trying to do? – John Coleman Jan 29 '21 at 12:40
  • Also -- the code that you posted throws no error (beyond the `Sheet` for `Sheets` typo). Please provide a [mcve]. As it is, you haven't given anyone enough information to replicate the problem. – John Coleman Jan 29 '21 at 12:48
  • You write "formula is important because I need to know what values I get". That makes some sense, and what you have posted should actually work. But, if the intention is for a user to be able to know where a certain number comes from, perhaps you can add that as a comment (easy enough to do from VBA) rather than using a formula with constant terms as a de facto comment. – John Coleman Jan 29 '21 at 13:01
  • everything I wrote is correct. if I want to multiply something times 2.1 the value (val) has to be a string. Then it works. If a value is taken from a formula or function, you can pass it through a function like: https://stackoverflow.com/questions/27905412/vba-double-as-string-with-comma-and-not-point – vipmaciej Feb 01 '21 at 12:18

0 Answers0