简体   繁体   English

excel VBA范围或cells()?

[英]excel VBA Range or cells()?

So, I'm trying to write a user friendly macro for easy sales recording. 因此,我试图编写一个用户友好的宏以方便进行销售记录。
I've completed all the input and calculation part, however when I tried to write the result in excel spread sheet using the cell() function, I can only write in string. 我已经完成了所有的输入和计算部分,但是当我尝试使用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

I've also wrote a short code to test if the range function would work... 我还编写了一个简短的代码来测试range函数是否可以工作...

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

Both codes had a userform to go with it, the range function output both in double data type, cells(2,2) output in strings, and 这两个代码都有一个用户窗体, 范围函数都以double数据类型输出,cells(2,2)以字符串输出,并且

Cells(2, 1) = coffee

yield no output in the cell. 在单元格中没有输出。
So should I assume that Cells() function can only input string? 所以我应该假设Cells()函数只能输入字符串吗? Or am I not using the function correctly... 还是我没有正确使用该功能...
If I can't use cells() function, then how can I write a range function that can automatically go to the last column and goes to individual row? 如果我不能使用cells()函数,那么我该如何编写一个可以自动转到最后一列并转到单个行的范围函数? Thanks 谢谢

You should use 你应该用

Cells(2, 1).Value = coffee

instead of just 而不只是

Cells(2, 1) = coffee

Otherwise you compare the cell with coffee , instead of the cells value. 否则,您将单元格与coffee而不是单元格值进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM