简体   繁体   中英

How do i add + 1 to activecell in vba

I am kept on getting run time 13 error. all i am trying to do add 1 to activecell.

     dim rang as range
     rang = activecell + 1
     msgbox rang

You're not setting the rang reference.

Set rang = ActiveCell

One problem down.

You're relying on implicit conversions and default properties, which makes it much harder than it needs to be, to understand what's going on.

rang.Value = CLng(ActiveCell.Value) + 1
MsgBox rang.Value

If that blows up with a runtime error 13, then ActiveCell.Value isn't an integer value. You can't legally add +1 to a String ... Or do anything (including mere comparison) with an error value. Use IsError to determine if the cell contains a value you can work with, before you work with it:

If Not IsError(rang.Value) Then
    MsgBox rang.Value
End If

You want to reference the active cell's value, like this

rang = ActiveCell.Value + 1

Edit - Runtime error 13 is a type mismatch, basically you are trying to combine incompatible data types (assuming the active cell contains a number, not a number formatted as text). Try this.

Dim rang as Integer
rang = ActiveCell.Value + 1
msgbox rang

You can convert numbers formatted as text to numeric values by copying a cell with a one in it, then paste special, and select multiply (assuming the content is valid as a number)

Sub addOne()
ActiveCell = ActiveCell.Value + 1
End Sub

Try This. This will simply add 1 in current value. Make sure its not a string value in active cell. Hope This help.

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