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
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try:
Code:
cell1 = Cells(Activecell.Row,"A")
cell2 = Cells(Activecell.Row,"M")
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,194
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top