VBA to Move Rows Based on Status in Excel

bgwhalen

New Member
Joined
Sep 11, 2024
Messages
2
Office Version
  1. Prefer Not To Say
Platform
  1. Windows
Hello everyone,

I'm new to VBA programming in Excel and I'm trying to automate a process where I move a row from my "Training" sheet to a "Historical" sheet when the value in Column G (Status) is set to "Inactive." However, I keep encountering an error with my code, and I'm not sure how to fix it.

Here's a brief overview of what I have done so far:

1. **Trigger**: The code should run when the status in Column G is changed to "Inactive."
2. **Action**: Move the entire row from the "Training" sheet to the "Historical" sheet.
3. **Action**: The move button when clicked should move the row that has “Inactive” selected in Column G.
3. **Error**: I’m encountering an error when I run my code, but I’m unsure what the issue is.


I have attached pictures of my code and the relevant parts of my Excel file for reference.

Questions:
- What could be causing the error in my code?
- Is there a better way to structure this code?
- How can I ensure this runs automatically when the status is changed?

I appreciate any help or guidance you can provide!

Thank you!
 

Attachments

  • Screenshot (32).jpeg
    Screenshot (32).jpeg
    91.6 KB · Views: 29
  • Screenshot (33).jpeg
    Screenshot (33).jpeg
    94 KB · Views: 29
  • Screenshot (35).jpeg
    Screenshot (35).jpeg
    67.6 KB · Views: 28

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Here is what the sheet looks like with the Move row button
 

Attachments

  • Screenshot (36).jpeg
    Screenshot (36).jpeg
    111.4 KB · Views: 17
Upvote 0
If you want the script to run when you enter "Inactive" into column "G"
You do not need a Button.

The below script will run when you enter the value "Inactive" into column G

Right click on the Sheet Tab named ""Training" Choose View and enter this script

Then any time you enter "Inactive" into any cell in column G that row will be copied to sheet named "Historical" and deleted from sheet named "Training"

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 7 Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim Lastrow As Long
Lastrow = Sheets("Historical").Cells(Rows.Count, "G").End(xlUp).Row + 1
If Target.Value = "Inactive" Then
Rows(Target.Row).Copy Sheets("Historical").Cells(Lastrow, 1)
Rows(Target.Row).Delete

End If
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
The solutions provided by @My Aswer Is This, is the way to go but in terms of
- What could be causing the error in my code?
The error you are getting indicates that it can't find the sheet "Training".
Looking at you image the little green line under the Training sheet name looks to goes past the end of the "g" and indicates you have a space after the word Training.
 
Upvote 1

Forum statistics

Threads
1,224,810
Messages
6,181,079
Members
453,021
Latest member
Justyna P

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