Reputation: 1379
I have two worksheets 'Sheet A' and 'Sheet B' (note the space in the sheet name) in my excel workbook. The requirement is that I need to use values from cells in 'Sheet A', in 'Sheet B'.
The macro code I wrote for this is something like this:
Sub Ratios()
B4 = 'Sheet A'!A25 / 'Sheet A'!A11
End Sub
Excel said that there was a syntax error. So I tried something like:
Sub Ratios()
[B4] = ['Sheet A'!A25] / ['Sheet A'!A11]
End Sub
...but to no avail. The error this time was that there was a type mismatch. The values in the cell A25 and A11 are integers.
Any idea on what's wrong?
Thanks!
Upvotes: 0
Views: 165
Reputation: 50019
Your syntax for pointing to a cell on another sheet is way off. Instead:
B4 = Sheets("Sheet A").Range("A25")/Sheets("Sheet A").Range("A11")
You can also used Cells()
to point to cells by Row and Column number, which is helpful at times:
B4 = Sheets("Sheet A").cells(25, 1)/Sheets("Sheet A").cells(11, 1)
Edited to add: This will store the value of that division in Variable B4. If you wanting to write the result of the division to cell B4
in "Sheet B"
then:
Sheets("Sheet B").Range("B4").value = Sheets("Sheet A").Range("A25")/Sheets("Sheet A").Range("A11")
But at that point, why use VBA? Just use a formula and save yourself an unnecessary headache.
Upvotes: 2