vba VBA文本框到单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15443007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA textbox to cell
提问by Israel Gonzales
I have a input user form in excel where a user enters data into a three different textbox. This is my code
我在 excel 中有一个输入用户表单,用户可以在其中将数据输入到三个不同的文本框中。这是我的代码
Private Sub Process_Click()
Worksheets("1").Range("A1").Value = Date.Value
Worksheets("1").Range("B1").Value = Name.Value
Worksheets("1").Range("C1").Value = Amount.Value
End Sub
So the first time I press process it will write the data into those cells A1,b1, and C1. What i want to do is that everytime I open the form and click process on my form I would like for those values to go down a row for example the next time i click process it will go to A2,b2,and c2, and so on.
所以当我第一次按下 process 时,它会将数据写入这些单元格 A1、b1 和 C1。我想要做的是,每次打开表单并单击表单上的进程时,我都希望这些值向下一行,例如,下次我单击进程时,它将转到 A2、b2 和 c2,等等在。
回答by mechanical_meat
This is similar to Barranka's answer.
这类似于巴兰卡的回答。
The basic idea is the same. You check for the last-populated row, and append your data one row lower than that.
基本思想是一样的。您检查最后填充的行,并将您的数据附加到比该行低的一行。
The method used here to find the last-populated row might be slightly more efficient than using a while
loop, but you should test both to see for yourself:
此处用于查找最后填充行的方法可能比使用while
循环更有效,但您应该测试两者以亲自查看:
Private Sub Process_Click()
Dim appendRow As Long
' add 1 to get the row after the last-populated row
appendRow = Worksheets("1").Cells(Rows.Count,1).End(XlUp).Row + 1
Worksheets("1").Range("A" & appendRow).Value = Date.Value
Worksheets("1").Range("B" & appendRow).Value = Name.Value
Worksheets("1").Range("C" & appendRow).Value = Amount.Value
End Sub
回答by Barranka
First, you need to know which was the last filled cell. This can be done easily navigating the range. Assuming that all three columns are filled with data, you can read only one (let's say A
) to get the last row:
首先,您需要知道哪个是最后一个填充的单元格。这可以通过导航范围轻松完成。假设所有三列都填充了数据,您只能读取一个(假设A
)来获取最后一行:
Dim i as Integer
i = 1
while Worksheets("1").Range("A" & i).Value <> "" or Worksheets("1").Range("A" & i).Value <> 0
i = i + 1
wend
Now that you know the last row populated:
现在您知道最后一行已填充:
Worksheets("1").Range("A" & i).Value = Date.Value
Worksheets("1").Range("B" & i).Value = Name.Value
Worksheets("1").Range("C" & i).Value = Amount.Value
It's just a way to solve it, and it works if your data set is not meant to be really big. Downside is, if you have a very long table, the while
can take some time to track the last populated row.
这只是解决它的一种方法,如果您的数据集不是很大,它就可以工作。缺点是,如果您有一个很长的表,则while
可能需要一些时间来跟踪最后填充的行。
Hope this helps you
希望这对你有帮助