excel VBA范围或单元格()?

时间:2015-09-01 05:38:19

标签: excel-vba vba excel

所以,我正在尝试编写一个用户友好的宏,以便于销售记录 我已经完成了所有输入和计算部分,但是当我尝试使用 cell()函数在excel电子表格中写入结果时,我只能用字符串写。

Range("A2").Select
ActiveCell.End(xlToRight).Select
lastcolumn = ActiveCell.Column
'MsgBox lastcolumn
If lastcolumn = 16384 Then
lastcolumn = 1
End If
Cells(16, lastcolumn + 1).Select
If lbl_scfe = "" Then
Cells(2, lastcolumn + 1).Value = 0
Else
Cells(2, lastcolumn + 1).Value = lbl_scfe
End If

我还写了一个简短的代码来测试范围功能是否有用......

Private Sub CommandButton1_Click()
val_cfe_1 = Val(TextBox1)
val_cfe_2 = Val(TextBox2)
coffee = val_cfe_1 * 10 + val_cfe_2
Label1 = coffee
Range("a1") = coffee
Range("a2") = Label1
Cells(2, 1) = coffee
Cells(2, 2) = Label1
End Sub

两个代码都有一个用户形式,范围函数输出双数据类型,单元格(2,2)输出字符串,和

Cells(2, 1) = coffee

单元格中没有输出。
那么我应该假设 Cells()函数只能输入字符串吗?或者我没有正确使用该功能...
如果我不能使用 cells()函数,那么如何编写范围函数,该函数可以自动转到最后一列并转到单个行? 感谢

1 个答案:

答案 0 :(得分:0)

你应该使用

Cells(2, 1).Value = coffee

而不仅仅是

Cells(2, 1) = coffee

否则,您将单元格与coffee进行比较,而不是单元格值。