Copying a range of cells from one sheet to another not working

Zedinator

New Member
Joined
Jun 4, 2019
Messages
5
Hi! I'm trying to copy a range of cells to a second sheet and then delete the row from the first sheet. It seems like it should be pretty simple but the code is throwing an unknown error and highlighting the line "Sheets("Sheet1").Range(cell1, cell2).Copy"
Any ideas why this would be happening?

As a side note, ideally this code would paste into the first empty row in sheet 2, but I'm doing things one step at a time and want to get this first basic step debugged first.

Code:
Public Sub FindTag()


'Creating an input box
Dim myValue As Variant
myValue = InputBox("Enter Tag being found")


'Searching for the input to delete


      Dim x As String, firstCell As Variant, secCell As Variant, NextRow As Variant
      
      Dim Found As Boolean
      ' Select first line of data.
      Range("A2").Select
      ' Set search variable value.
      x = myValue
      ' Set Boolean variable "found" to false.
      Found = False
      ' Set Do loop to stop at empty cell.
      Do Until IsEmpty(ActiveCell)
         ' Check active cell for search value.
         If ActiveCell.Value = x Then
            Found = True
            Exit Do
         End If
         ' Step down 1 row from present location.
         ActiveCell.Offset(1, 0).Select
      Loop
   ' Check for found.
      If Found = True Then
        Column = Mid(ActiveCell.Address, 4, 1)
cell1 = "A" & Column
        cell2 = "M" & Column
        
        'Copy the data
        Sheets("Sheet1").Range(cell1, cell2).Copy
        'Activate the destination worksheet
        Sheets("Sheet2").Activate
        'Select the target range
        Range("A2").Select
        'Paste in the target destination
        ActiveSheet.Paste


        Application.CutCopyMode = False
        
        ActiveCell.EntireRow.Delete
        MsgBox "Tag copied!"
      Else
         MsgBox "Value not found"
      End If
End Sub
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,669
Try:
Code:
cell1 = Cells(Activecell.Row,"A")
cell2 = Cells(Activecell.Row,"M")
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,974
Office Version
2007
Platform
Windows
If you allow me I show you some improvements in your code

Code:
Public Sub FindTag()
    Dim myValue As Variant, f As Range, lr As Long
    
    'Creating an input box
    myValue = InputBox("Enter Tag being found")
    If myValue = "" Then Exit Sub
    
    'Searching for the input
    Set f = Range("A:A").Find(myValue, LookIn:=xlValues, lookat:=xlWhole)
    If Not f Is Nothing Then
        'first empty row in sheet 2
        lr = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
        'Copy the data and paste
        f.EntireRow.Copy Destination:=Sheets("Sheet2").Range("A" & lr)
        f.EntireRow.Delete
        MsgBox "Tag copied!"
    Else
        MsgBox "Value not found"
    End If
End Sub
Let me know if you have any doubt.
 

Forum statistics

Threads
1,082,601
Messages
5,366,571
Members
400,902
Latest member
fathima

Some videos you may like

This Week's Hot Topics

Top