Macro Help

amwalo

New Member
Joined
Jun 9, 2022
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I am trying to make my code work on the entire column by clicking a button. I am trying to get it to work with out having to highlight and having the one click take car of the entire column if that makes sense. The code deletes an entire row with completed in the column and moves it to another sheet. Side question is there a way to have it work by just hitting enter after entering completed?

Here is my code:

'VBA Code by Scott from SpreadsheetPlanet.com
Sub move_rows_to_another_sheet()
For Each mycell In Selection.Columns(9).Cells
If mycell.Value = "Completed" Then
mycell.EntireRow.Copy Worksheets("Completed").Range("A" & Rows.Count).End(3)(2)
mycell.EntireRow.Delete
End If

Next

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try changing this:
For Each mycell In Selection.Columns(9).Cells
to this:
For Each mycell In columns("I1:I1000").select

NOTE: Change the I1000 to however many rows you have in the sheet.

You can have it run automatically. On the sheet where you would type completed, add the following:
VBA Code:
Private Sub Worksheet_Change(ByVal target As Range)

 If Not Intersect(Range("I1:I1000"), target) Is Nothing Then
   If target.Value = "Completed" Then

 'run your macro here

   End If

End If
End Sub
 
Upvote 0
Try changing this:
For Each mycell In Selection.Columns(9).Cells
to this:
For Each mycell In columns("I1:I1000").select

NOTE: Change the I1000 to however many rows you have in the sheet.

You can have it run automatically. On the sheet where you would type completed, add the following:
VBA Code:
Private Sub Worksheet_Change(ByVal target As Range)

 If Not Intersect(Range("I1:I1000"), target) Is Nothing Then
   If target.Value = "Completed" Then

 'run your macro here

   End If

End If
End Sub
When I changed the line it comes up with a application-defined or object defined error. I added the other code to the specific sheet so we will see if that works.
 
Upvote 0
Right...rather than using the select, try using the range:

For Each mycell in range("I1:I1000")
 
Upvote 0
When I changed the line it comes up with a application-defined or object defined error. I added the other code to the specific sheet so we will see if that works.
Do I need to change anything about my code to add it to the VBA code you supplied for the worksheet?
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal target As Range)
    If Intersect(target, Range("I:I")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If target = "Completed" Then
        target.EntireRow.Copy Worksheets("Completed").Range("A" & Rows.Count).End(3)(2)
        target.EntireRow.Delete
    End If
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal target As Range)
    If Intersect(target, Range("I:I")) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If target = "Completed" Then
        target.EntireRow.Copy Worksheets("Completed").Range("A" & Rows.Count).End(3)(2)
        target.EntireRow.Delete
    End If
    Application.EnableEvents = True
End Sub
So it says type mismatch on the If target = "Completed" Then. Thank you for helping me lol I have been trying to figure this out for two days now.
 
Upvote 0

Forum statistics

Threads
1,215,333
Messages
6,124,317
Members
449,153
Latest member
JazzSingerNL

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