How do I set a variable to ActiveCell.Row

I would like to be able to return the row number of the active cell then assign that value a variable. It is saying “Object not Defined” for ActiveCell.Row when I attempt to run the Macro. I would like to have cells copied to another sheet based on the value of the active row.

This is the erroneous code

Sub CopyPaste()
Set CurrentRow = ActiveCell.Row
Select Case test_expression

Case CurrentRow = 2
Sheets("Drop-In").Range("A2").EntireRow.Copy Sheets("CompletedV2").Range("A2")

Case Else
MsgBox "This is a sample box"

End Select
End Sub

New contributor

Aaron Sharples is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

This will assign the active row to the CurrentRow variable.
If the CurrentRow is two it will paste that into row 2 on the second sheet.
If it’s between 3 and 5 it will paste it into the relevant row.

Not sure why you’re using the Select Case statement though.

Public Sub CopyPaste()

    Dim CurrentRow As Long
    CurrentRow = ActiveCell.Row
    
    Select Case CurrentRow
        Case 2
            ThisWorkbook.Worksheets("Drop-In").Rows(CurrentRow).Copy Destination:=ThisWorkbook.Worksheets("CompletedV2").Rows(2)
        Case 3 To 5
            ThisWorkbook.Worksheets("Drop-In").Rows(CurrentRow).Copy Destination:=ThisWorkbook.Worksheets("CompletedV2").Rows(CurrentRow)
        Case Else
            MsgBox "This is a sample box", vbInformation + vbOKOnly
    End Select

End Sub

Copy Entire Row If String in Column Matches

  • Here is a simple scenario that might get you on your feet.
  • To keep things relatively safe, I’ve implemented a few restrictions that unfortunately complicated the code. Best, create a copy of your workbook and try the code there. Don’t forget to close the original.
  • If the value in column A of the active cell’s row is Test, it copies the entire (active cell’s) row to the first available cell in column A of the destination sheet. Otherwise, it displays a message explaining why it ‘failed’. In a nutshell, if you have selected a row where the cell in column A is equal to Test, the row will be copied.
  • To get an accurate answer, you need to explain what you’re trying to do in more detail e.g. as I did above. Edit your post (question) adding more information. If my code is close, you could paste it into another question and explain what it does, what it should do, and what you have tried to achieve the desired functionality.
Option Explicit

Sub CopyIfMatch()
    
    ' Define constants.
    Const SRC_SHEET_NAME As String = "Drop-In"
    Const DST_SHEET_NAME As String = "CompletedV2"
    Const TEST_STRING As String = "Test" ' the string to match
    
    ' Source
    
    Dim scell As Range: Set scell = ActiveCell
    If scell Is Nothing Then Exit Sub ' no active cell
    
    Dim sws As Worksheet: Set sws = scell.Worksheet
    
    If StrComp(sws.Name, SRC_SHEET_NAME, vbTextCompare) <> 0 Then ' (A=a)
        MsgBox "This only works for sheet """ & SRC_SHEET_NAME & """!", _
            vbExclamation
        Exit Sub
    End If
    
    Dim SourceString As String: ' the value in column 'A' of the active row
    SourceString = CStr(scell.EntireRow.Columns("A").Value)
    
    If SourceString <> TEST_STRING Then ' is equal (case-sensitive i.e. 'a<>A')
        MsgBox "The Source string """ & SourceString _
            & """ is different than the Test string """ & TEST_STRING & """!", _
            vbExclamation
        Exit Sub
    End If
    
    ' Destination (different sheet but same workbook as source)
    Dim dws As Worksheet: Set dws = sws.Parent.Sheets(DST_SHEET_NAME)
    Dim dcell As Range: ' first 'available' cell in column 'A'
    Set dcell = dws.Cells(dws.Rows.Count, "A").End(xlUp).Offset(1)
    ' Caution, the previous doesn't work if column 'A' in source could be empty!
    ' Keep in mind that you can copy entire rows only to column 'A'!
   
    ' Copy
    scell.EntireRow.Copy dcell

    ' Inform.
    MsgBox "Matching source row " & scell.Row _
        & " copied to destination row " & dcell.Row & ".", vbInformation

End Sub

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật