neke
neke

Reputation: 85

vba - assign value of cell from another worksheet to a variable

I cannot cope with the following issue. I have in a module certain code. This code is working well when I'm in the sheet2. When skipping to sheet1 and run macro it returns me 0. I'd appreciate if you could point at the mistake I've done.

Public Sub Test()

Dim ws As Worksheet
Dim cost As Double

Set ws = Sheets("Sheet1")

cost = Worksheets("Sheet2").Application.Sum(Range("A2:A10"))
MsgBox cost
ws.Range("C2") = cost

End Sub

Upvotes: 0

Views: 10092

Answers (1)

Volkan Yurtseven
Volkan Yurtseven

Reputation: 444

Coz you forget to put sheet2's name in front of range(a2:a10). and no need to specify the sheet name before Applicaiton. it is supposed to be like this

cost = Application.Sum(Worksheets("Sheet2").Range("A2:A10"))

Upvotes: 2

Related Questions