如何使用VBA在Excel中增加单元格值?错误:类型不匹配 [英] How do I Increase cell value in Excel using VBA? Error: Type mismatch

查看:252
本文介绍了如何使用VBA在Excel中增加单元格值?错误:类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个宏,但是因为我是第一次在vb中工作,所以遇到了问题.

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

我的代码:

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

我的代码应该是:

Cells(1,1).Value+=1

我收到错误类型不匹配".我该怎么做?

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

编辑:单元格1的第1行已合并.它由4列组成,所以它是第1行,单元1,2,3,4的一列.如果该单元格未合并,则没有问题.

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.

推荐答案

这又如何:

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

不需要在VBA中选择要使用的单元格和范围(并且速度要慢得多),您可以直接引用它们.我实际上不是肯定的,如果Cells(1,1)的值为"hello"或没有数字,上述方法将如何工作,但是如果有效,它将起作用.如果要测试单元格中的值以确保确定,可以使用IsNumeric().

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.

让我知道是否有帮助!

这篇关于如何使用VBA在Excel中增加单元格值?错误:类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆