Macro to move cells to another worksheet and overwrite existing data in the target worksheet

Jamie6325

New Member
Joined
Feb 8, 2022
Messages
9
Office Version
  1. 365
I have a VBA code to move rows in the "Data" worksheet to another sheet, "Active Status" based on the status in Column A. The "Data" spreadsheet will be modified by users on a daily basis. The status may change from Active to Non-Active and other information may be added. I need help regarding moving the rows in the worksheet "Data" to the "Active Status" worksheet and overwriting the data that is currently in the "Active Status" worksheet. Any suggestions? My current code keeps adding to the rows to the "Active Status" worksheet and not overwriting them. That is the part that I cannot figure out.

Below is an example of the data and the VBA code I am currently using.

Sub MoveActive()
Dim xRg As Range
Dim xCell As Range
Dim I As Long
Dim J As Long
Dim K As Long
I = Worksheets("Data").UsedRange.Rows.Count
J = Worksheets("Active Status").UsedRange.Rows.Count
If J = 1 Then
If Application.WorksheetFunction.CountA(Worksheets("Active SH Referrals").UsedRange) = 0 Then J = 0
End If
Set xRg = Worksheets("Data").Range("A1:C" & I)
On Error Resume Next
Application.ScreenUpdating = False
For K = 1 To xRg.Count
If CStr(xRg(K).Value) = "Active Status" Then
xRg(K).EntireRow.Copy Destination:=Worksheets("Active Status").Range("A" & J)
J = J + 1
End If
Next
Application.ScreenUpdating = True
End Sub


Sample of data:
Active StatusDateMonthYearDay of the WeekName
Active7/12/2022July2022Tuesdayxxxxx
Non Active7/12/2022July2022Tuesdayxxxxx
Active7/23/2022July2022Saturdayxxxxx
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:
VBA Code:
Sub MoveRows()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Active Status")
    desWS.UsedRange.Offset(1).ClearContents
    With srcWS
        .Range("A1").CurrentRegion.AutoFilter 1, "Active"
        .AutoFilter.Range.Offset(1).Copy desWS.Range("A2")
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
What was the error message and which line of code was highlighted when you clicked debug?
 
Upvote 0
I get the following:
Run-time error '1004':
Application-defined or object-defined error
 
Upvote 0
When you click Debug, which line of code is highlighted?
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0

Explain in detail what you want to do referring to specific cells, rows, columns and sheets:

If any cell in column A in the 'Data' sheet contains the word "Active", I would like to copy and move the entire row to another sheet called 'Active' starting in A3. If you look at the spreadsheet, it will be easier. The current macro I am using will copy and move the rows to the 'Active' sheet but it does not overwrite the data that is in the 'Active' sheet, it just adds on to it. Every time I run the macro, I would like the data to overwrite whatever is in the 'Active' sheet starting at A3 and not continue to add rows at the bottom.
 
Upvote 0
Try:
VBA Code:
Sub MoveRows()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("Data")
    Set desWS = Sheets("Active Status")
    desWS.UsedRange.Offset(1).ClearContents
    With srcWS.ListObjects("Table1")
        .Range.AutoFilter Field:=1, Criteria1:="Active"
        .AutoFilter.Range.Offset(1).Copy desWS.Range("A2")
        Range("A2").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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