简体   繁体   中英

Reference a cell from another worksheet

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM