繁体   English   中英

将 1 添加到另一个工作表中的上方单元格

[英]Add 1 to the cell above in another sheet

我正在尝试运行一个宏:将 1 添加到工作簿中另一张工作表上方的单元格中,并将其放置在下方的单元格中。 然而,它添加到的单元格是动态的,宏为新条目添加了一个新行。 下面是我到目前为止的代码。 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

暂无
暂无

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

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