在另一个工作表上面的单元格中加1

我试图运行一个macros:将1添加到工作簿中另一个工作表上方的单元格中,并将其放置在下面的单元格中。 然而,它添加的单元格是dynamic的,macros添加一个新的条目新条目。 以下是我到目前为止的代码。 cellRef应该添加,但我得到随机数字和错误! (一次失配,其他时间随机数字)

 Sub update_Number() cellRef = ActiveWorkbook.Sheets("RMA").Range("A" & (ActiveCell.Row)).Offset(-1, 0).Value ActiveWorkbook.ActiveSheet.Select customeRef = Range("c" & (ActiveCell.Row)) customerName = Range("d" & (ActiveCell.Row)) customerCountry = Range("e" & (ActiveCell.Row)) customerCompany = Range("f" & (ActiveCell.Row)) datePaid = Range("g" & (ActiveCell.Row)) Dim wks As Worksheet Set wks = Sheets("RMA") With wks Dim RowCount As Long RowCount = .Range("A8").End(xlDown).Row + 1 .Cells(RowCount, 1) = cellRef + 1 .Cells(RowCount, 2) = customeRef .Cells(RowCount, 3) = customerName .Cells(RowCount, 4) = customerCountry .Cells(RowCount, 5) = customerCompany .Cells(RowCount, 6) = datePaid End With End Sub 

你需要做的是完全限定你的对象。 一旦你这样做,你将不会得到随机值。 Excel会知道哪个表格,你指的是哪个单元格。 看到这个例子

 Sub update_Number() Dim Sno As Long, LRow As Long, NewRow As Long Dim wsO As Worksheet, wsI As Worksheet Dim customeRef, customerName, customerCountry Dim customerCompany, datePaid Set wsO = ThisWorkbook.Sheets("RMA") Set wsI = ThisWorkbook.Sheets("Sheet1") '~~> Get values from Sheet1 With wsI customeRef = .Range("c" & (ActiveCell.Row)) customerName = .Range("d" & (ActiveCell.Row)) customerCountry = .Range("e" & (ActiveCell.Row)) customerCompany = .Range("f" & (ActiveCell.Row)) datePaid = .Range("g" & (ActiveCell.Row)) End With '~~> Work with RMA Sheet With wsO '~~> Get the last Row LRow = .Range("A" & .Rows.Count).End(xlUp).Row '~~> Increment the number Sno = .Range("A" & LRow).Value + 1 '~~> New row where we need to write NewRow = LRow + 1 .Cells(NewRow, 1) = Sno .Cells(NewRow, 2) = customeRef .Cells(NewRow, 3) = customerName .Cells(NewRow, 4) = customerCountry .Cells(NewRow, 5) = customerCompany .Cells(NewRow, 6) = datePaid End With End Sub