Coding Help

TaylorGall

New Member
Joined
Jun 6, 2024
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I am familiar with using formulas in excel but am struggling with this one as I believe this requires coding.

What I am looking for this to do is the following.

If I type 'OFFSHORE' into column E, the entire row will be moved to the 'OFFSHORE' sheet. Removing the original row from the 'ONSHORE' sheet.
The same for, If I type 'RECERT' or 'REPAIR' into column E, the entire row will be moved to the 'AWAY FOR REPAIR OR RECERT' sheet. Removing the original row from the 'ONSHORE' sheet.

I would also like this to work the other way round.
If when in sheet 'OFFSHORE', I type 'ONSHORE' into column E, the entire row will be moved to the 'ONSHORE' sheet. Removing the original row from the 'OFFSHORE' sheet.
Or I type 'RECERT' or 'REPAIR' into column E, the entire row will be moved to the 'AWAY FOR REPAIR OR RECERT' sheet. Removing the original row from the 'OFFSHORE' sheet.

If when in sheet 'AWAY FOR REPAIR OR RECERT', I type 'ONSHORE' into column E, the entire row will be moved to the 'ONSHORE' sheet. Removing the original row from the 'AWAY FOR REPAIR OR RECERT' sheet.
Or I type 'OFFSHORE' into column E, the entire row will be moved to the 'OFFSHORE' sheet. Removing the original row from the 'AWAY FOR REPAIR OR RECERT' sheet.

Please see attached for reference.

Any help would be greatly appreciated.

Regards
Taylor
Inventory List Microsoft Code.png
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You will need a macro in the code module for each worksheet . Do the following: right click the tab name for your "ONSHORE" sheet and click 'View Code'. Paste the first macro into the empty code window that opens up. Close the code window to return to your sheet. Repeat these steps for the "OFFSHORE" and "AWAY FOR REPAIR OR RECERT" sheets using the second and third macros respectively.
VBA Code:
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 5 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Select Case Target.Value
        Case "OFFSHORE"
            With Target.EntireRow
                .Copy Sheets("OFFSHORE").Cells(Sheets("OFFSHORE").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
        Case "RECERT", "REPAIR"
            With Target.EntireRow
                .Copy Sheets("AWAY FOR REPAIR OR RECERT").Cells(Sheets("AWAY FOR REPAIR OR RECERT").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
    End Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 5 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Select Case Target.Value
        Case "ONSHORE"
            With Target.EntireRow
                .Copy Sheets("ONSHORE").Cells(Sheets("ONSHORE").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
        Case "RECERT", "REPAIR"
            With Target.EntireRow
                .Copy Sheets("AWAY FOR REPAIR OR RECERT").Cells(Sheets("AWAY FOR REPAIR OR RECERT").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
    End Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Column <> 5 Then Exit Sub
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Select Case Target.Value
        Case "ONSHORE"
            With Target.EntireRow
                .Copy Sheets("ONSHORE").Cells(Sheets("ONSHORE").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
        Case "OFFSHORE"
            With Target.EntireRow
                .Copy Sheets("OFFSHORE").Cells(Sheets("OFFSHORE").Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
    End Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub
Enter a value in column E and press the ENTER key.
 
Upvote 0
Thank you for your help, but I have just tried this a few times and cannot seem to get it to work.
When Entering value in column E and pressing enter, nothing changes.
 
Upvote 0
Hi,

Please see the attached for how I have entered the codes that you sent through.

Thanks.
 

Attachments

  • Inventory List Microsoft Code 1.png
    Inventory List Microsoft Code 1.png
    95.6 KB · Views: 9
  • Inventory List Microsoft Code 2.png
    Inventory List Microsoft Code 2.png
    98.2 KB · Views: 9
  • Inventory List Microsoft Code 3.png
    Inventory List Microsoft Code 3.png
    97.6 KB · Views: 9
Upvote 0
That is correct. If they are not working properly for you, perhaps you could upload a copy of your file (de-sensitized if necessary) 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.
 
Upvote 0
It may be possible that in your various attempts, if you had any VBA code cancel or quit in the middle, you may have turned off events and not turned them back on.
You can turn events back on by running this code manually:
VBA Code:
Sub ReEnableEvents()
    Application.EnableEvents = True
End Sub

You may want to try doing that and running another test, to make sure that isn't the problem.
 
Upvote 0
Hi

See if following will do what you want

1 – make a backup of your workbook & then DELETE all existing event codes in each of the worksheets

2 – Insert a STANDARD module (from developer menu Insert > Module)

3 – Place ALL following codes in the module

Code:
Option Base 1
Sub MoveRow(ByVal Target As Range, ByVal SheetNames As Variant)
    Dim KeyWord     As Variant, m As Variant
    Dim ws          As Worksheet
    
    If Target.CountLarge > 1 Then Exit Sub
    
    On Error GoTo exitsub
    EventsEnable False
    
    KeyWord = Array("ONSHORE", "OFFSHORE", "RECERT", "REPAIR")
    m = Application.Match(UCase(Target.Value), KeyWord, 0)
    If Not IsError(m) Then
        m = IIf(m = 4, 3, m)
        
        If Target.Parent.Name <> SheetNames(m) Then
            Set ws = Worksheets(SheetNames(m))
            
            With Target.EntireRow
                .Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
                .Delete
            End With
        End If
        
    End If
    
exitsub:
    EventsEnable True
End Sub

Sub EventsEnable(ByVal State As Boolean)
    With Application
        .ScreenUpdating = State
        .EnableEvents = State
    End With
End Sub

Note Option Base 1 statement at the top. This MUST be placed at very TOP of the module OUTSIDE any procedure.

4 – In the developer to the left, double click on ThisWorkbook (see pic) & place all following code

Code:
Option Base 1
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim SheetName As Variant, m As Variant
    If Target.Column <> 5 Then Exit Sub
    SheetName = Array("ONSHORE", "OFFSHORE", "AWAY FOR REPAIR OR RECERT")
    m = Application.Match(Sh.Name, SheetName, 0)
    If Not IsError(m) Then MoveRow Target, SheetName
End Sub

Again, note Option Base 1 statement.

Solution only lightly tested but hopefully, if I have understood correctly, this will do what you want

Dave


1717724631227.png
 
Upvote 0
Hi, I have just tried the method above, but still having no luck unfortunately.

Could you please have a look at the attached screenshots to see if I have put the code in correctly?

Thanks.
 

Attachments

  • Inventory List Microsoft Code 4.png
    Inventory List Microsoft Code 4.png
    97.4 KB · Views: 5
  • Inventory List Microsoft Code 5.png
    Inventory List Microsoft Code 5.png
    103.6 KB · Views: 5
Upvote 0
Hi, I have just tried the method above, but still having no luck unfortunately.
by no luck I assume you mean code is not doing anything?

Try CLOSING the Excel application completely & then re-open & see if any change.
If not, do you have any other codes in your project with the following line? If so, please share all with forum

VBA Code:
Application.EnableEvents = False

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,793
Members
451,589
Latest member
Harold14

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