簡體   English   中英

Excel VBA-復制到特定工作表的代碼中的錯誤,具體取決於范圍

[英]Excel VBA - Error in code to copy to specific sheet, dependent on range

JNevill非常友好地清理並改進了我放在一起的宏,不幸的是它返回了錯誤,並且我無法對原始帖子做出回應。 希望有人可以在他離開的地方接機。

我得到的錯誤是-與行lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)對象變量或未設置塊變量

Sub Macro1()
'Make a variable to store the cell found
Dim lastCell as Range

'find the last cell in Column A of the active sheet
lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)

'Paste in the I16 value
lastCell.value = RangE("I16").value

'Grab whatever is hanging out in Column B next to the last cell and stick it in J20
Range("J20").value = lastCell.Offset(0,1).value 

'Test to see if I16 has value "R"
If Range("I16").value = "R" Then

    'Find the last row in Sheet7, Column B and store it to the variable
    lastCell = Range("B" & Rows.Count).End(xlUp).Offset(1)

    'Copy J20 value to the lastCell in Sheet 7, Column B
    lastCell = Range("J20").value
End if


End Sub

原帖是在這里ORIGINAL

您只是缺少Set (因為它是一個對象變量):

Set lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)

根據下面的評論,我認為這是您想要的東西:

Sub Macro1()
    Dim ws                    As Worksheet
    'Make a variable to store the cell found
    Dim lastCell              As Range

    'find the last cell in Column A of the active sheet
    Set lastCell = Range("A" & Rows.Count).End(xlUp).Offset(1)

    'Paste in the I16 value
    lastCell.Value = Range("I16").Value

    'Grab whatever is hanging out in Column B next to the last cell and stick it in J20
    Range("J20").Value = lastCell.Offset(0, 1).Value

    'Test to see if I16 has value "R"
    Select Case VBA.UCase$(Range("I16").Value2)
        Case "R"
            Set ws = sheet7
        Case "C"
            Set ws = sheet3
        Case "P"
            Set ws = Sheet1
        Case "S"
            Set ws = Sheet2
    End Select

    If Not ws Is Nothing Then
        'Find the last row in Sheet7, Column B and store it to the variable
        Set lastCell = ws.Range("B" & Rows.Count).End(xlUp).Offset(1)

        'Copy J20 value to the lastCell in Sheet 7, Column B
        lastCell = Range("J20").Value
        Application.Goto lastCell, True
    End If

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM