每次用vba填写表单时,都要在excel中增加Y值

时间:2015-01-23 18:49:17

标签: excel vba excel-vba

我在Excel中有一个表单,我使用Vba收集数据并将其放在表格中。每次我填写新信息时,它所做的就是更换旧信息。这是因为Visual Basic的outout不是动态的。 我的问题是每次填写表格时如何增加Y轴。我需要一个Range / Cells中的变量,每次按下宏按钮时都会增加1。

Sub aksjer()

Dim stockname As String
Dim price As Integer
Dim number As Integer
Dim totalprice As Integer
Dim pe As Integer
Dim mkt As Long
Dim volume As Integer
Dim eps As Integer
Dim divye As Integer

stockname = Range("M5").Value
price = Range("M6").Value
number = Range("M7").Value
totalprice = Range("M8").Value
mkt = Range("M9").Value
pe = Range("M10").Value
eps = Range("M11").Value
divye = Range("M12").Value
volume = Range("M13").Value

Range("A6") = Format(Date, "dd-mm-yyyy")
Range("B6") = stockname
Range("C6") = price
Range("D6") = number
Range("E6") = totalprice
Range("G6") = pe
Range("F6") = mkt
Range("J6") = volume
Range("H6") = eps
Range("I6") = divye

Range("M5:M13") = ""

End Sub

2 个答案:

答案 0 :(得分:0)

您可以检查此

已经使用了多少行
usedrows = ActiveSheet.UsedRange.End(xlDown).Row

然后使用您的日期,将您的单元格称为Cells而不是Range,并为行号示例添加+1:

Cells( usedrows + 1 , 1) = Format(Date, "dd-mm-yyyy")

答案 1 :(得分:0)

无需将值传递给变量,然后将它们传递回单元格。浪费代码。试试这个:

Sub aksjer()

Dim LastRow As Long

LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1

Range("A" & LastRow).Value = Format(Date, "dd-mm-yyyy")
Range("B" & LastRow).Value = Range("M5").Value
Range("C" & LastRow).Value = Range("M6").Value
Range("D" & LastRow).Value = Range("M7").Value
Range("E" & LastRow).Value = Range("M8").Value
Range("F" & LastRow).Value = Range("M9").Value
Range("G" & LastRow).Value = Range("M10").Value
Range("H" & LastRow).Value = Range("M11").Value
Range("I" & LastRow).Value = Range("M12").Value
Range("J" & LastRow).Value = Range("M13").Value

Range("M5:M13").ClearContents

End Sub