tags:

views:

27

answers:

1

I'm writing a macro, but because I'm working for first time in vb, I faced problems.

My code:

Cells (1, 1).Select
Dim tempvar As Integer
tempvar = Val(Selection.Value) // error
Selection.Value = tempvar + 1

What my code should be:

Cells(1,1).Value+=1

I get error "type mismatch". How do I accomplish that?

EDIT: Row 1, Cell 1 is merged. It consists of 4 columns, so it is Row 1, Cell 1,2,3,4 in one column. If the cell is not merged then there is no problem.

+1  A: 

How about just this:

Cells(1,1).Value = Cells(1,1).Value + 1

Selecting cells and ranges to work with them in VBA isn't necessary (and is much slower), you can just refer to them directly. I'm actually not positive what above will do if Cells(1,1) has the value "hello" or not a number, but it will work if its valid. You can use IsNumeric() if you want to test the value in the cell to be sure.

Let me know if that helps!

goggin13
This solves the problem, thank you!
John