Excel VBA lastrow& currentrow,如果找到数据,则将数据添加到行或添加新行

时间:2015-07-30 16:39:08

标签: excel vba excel-vba

我有一个Excel表单,我使用它,它包含3个工作表 - 表单,数据和条目。基本上当您使用Button调用新表单时,它会在条目表(范围" A")中查找lastrow,并添加1,这就是您的新表单ID。有多个用户使用此表单我设置它还在填写表单之前在条目表中添加数字,以便保留它,以便下一个用户不能获得重复。

在这里,我被赶上了。我希望currentntrow找到那个工作表ID并将数据粘贴到该行中,但我的代码只是添加一个新行。

这就是它的样子。

Sub Submit()
Dim rng1 As Range
Dim rng2 As Range
Dim multiplerange As Range
Dim currentrow As Long
Dim sheetid As String
Set rng1 = Range("M1,M2,B2,F6,B7,B8,B9,B11,D11,B13,D13,I6,M6,I7,I8,I9,I11,K11,I13,K13,B15,B18,B22,E22,B23,B24,B26,E26,I22,J22,M22,K24,M24,I24,I26,K26,M26,B29,E29,B30,B31,B33,E33,I29,J29,M29,K31,M31,I31,I33,K33,M33,B36,E36,B37,B38,B40,E40,I36,J36,M36,K38,M38,I38,I40,K40,M40")
Set rng2 = Range("G2")
Set multiplerange = Union(rng1, rng2)
Dim WSEntries As Worksheet
Dim WSForm As Worksheet
Set WSEntries = Sheets("Entries")
Set WSForm = Sheets("Form")
sheetid = WSForm.Range("M1").Text
Dim lastrow As Integer
lastrow = WSEntries.Cells(Rows.Count, "A").End(xlUp).Row
For currentrow = 3 To lastrow
If Cells(currentrow, 1) = sheetid Then

Dim i As Integer
i = 1
For Each c In multiplerange
    WSEntries.Cells(currentrow + 1, i) = c
    i = i + 1
Next

Else
i = 1
For Each c In multiplerange
    WSEntries.Cells(lastrow + 1, i) = c
    i = i + 1
Next

End If
Next


End Sub

任何帮助都会很棒。感谢

编辑:我也没有添加所有声明的项目,但这是脚本的另一部分。我其他所有工作只是我遇到问题的粘贴部分

1 个答案:

答案 0 :(得分:0)

搞定了!切换到Range方法

lastrow = WSEntries.Cells(Rows.Count, "A").End(xlUp).Row
For currentrow = 3 To lastrow
If WSEntries.Range("A" & currentrow) = sheetid Then

Dim i As Integer
i = 1
For Each c In multiplerange
    WSEntries.Cells(currentrow, i) = c
    i = i + 1