1

I am trying to write macros code where, when the value of drop down under a column in table with header "Status", changes to "completed", then Sub Completedarc should run automatically. I am able to write a code when action status is changed to completed in one column but not the entire column in a table. Also,Sub Completedarc() is only cut pasting values in another sheet but not deleting the overcall row and it is left blank

Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Address = Range "Open_Project_Details[[#Headers],[Status]]") Then
   Select Case Target.Value
      Case "Completed"
        Call Completedarc
   End Select
 End If
End Sub

Sub Completedarc()
Rows(ActiveCell.Row).EntireRow.Cut
Sheets("Completed Archive").Select
Range("Completed_Archive[[#Headers],[Stack Rank]]").Select
Selection.End(xlDown).Select
If ActiveCell = "" Then
   ActiveSheet.Paste
Else
   ActiveCell.Offset(1).Activate
   ActiveSheet.Paste
End If
End Sub
FaneDuru
  • 38,298
  • 4
  • 19
  • 27
Shankar
  • 27
  • 4
  • You might benefit from reading [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). – Pᴇʜ Mar 02 '21 at 07:55
  • I think you mix up the words `columns` and `rows` in your text. Please re-read and clarify. Especially *"… when action status is changed to completed in one column but not the entire column in a table."* does not make any sense to me. Maybe a screenshot can help to explain better and to clarify. – Pᴇʜ Mar 02 '21 at 07:57
  • Check if `Target.Column` = Column number of Field `Status` – Foxfire And Burns And Burns Mar 02 '21 at 08:18
  • Are you referring to the line `Rows(ActiveCell.Row).EntireRow.Cut` when you need "deleting the overcall row"? – FaneDuru Mar 02 '21 at 08:21
  • So there are 2 sheets and one table in each. In the sheet "Open Project Details" there is a table, with one column header "Status" and all the cells under this column will have drop down values and when selected "Completed" in dropdown, that entire column should be cut and pasted in another sheet table in A:A inserting a new row after the table last filled one (next empty) one – Shankar Mar 03 '21 at 05:48

2 Answers2

0

If I well understood your question, this changed event will do what you need:

Private Sub Worksheet_Change(ByVal Target As Range)
 If Not Target.ListObject Is Nothing Then
    If Intersect(Target.ListObject.HeaderRowRange, _
            Target.EntireColumn).value = "Status" Then
        If Target.value = "Completed" Then
             Call Completedarc(Target) 'added an argument...
        End If
    End If
 End If
End Sub

About the Completedarc sub, I do not understand what is to be done. Does your "Open_Project_Details" table starts from column A:A and you want copying the table Target row in the first empty cell of the "Completed_Archive" table/column "Stack Rank"? Do you want to copy it inserting a new row after the table last one?

If this last one supposition is what you want, please use the next code:

Sub Completedarc(Target As Range)
  Dim TRows As Long, shCA As Worksheet
  
  Set shCA = Worksheets("Completed Archive")
  TRows = shCA.Range("Completed_Archive[Stack Rank]").cells.count 
  If TRows = 1 Then TRows = TRows + 1
  Intersect(Target.ListObject.DataBodyRange, Target.EntireRow).Copy _
                 shCA.Range("Completed_Archive[Stack Rank]").cells(TRows)
  'the next code line only selects the row to be deleted. If it selects what you need
  'you would only replace `Select` with `Delete` and the code will delete such rows
  Target.EntireRow.Select 'Delete
End Sub
FaneDuru
  • 38,298
  • 4
  • 19
  • 27
  • Thank you very much for your input, code looks good but I am getting run time 13 error (although the action is taking place and every time I need to stop/reset and run again. It should the below code in yellow:................................................................................................ If Intersect(Target.ListObject.HeaderRowRange, _ Target.EntireColumn).Value = "Status" Then – Shankar Mar 03 '21 at 05:38
  • @Shankar: Does your sheet where the event exists contain a "Status" Header name in a table? It the code reaches this line, it means that you changed a cell inside of a table. The above code should work for a change in any table (from the active sheet, of course) and process only the changed cells of a table column named "Status"... If still a problem, if not something confidential, can you share the workbook you try processing? – FaneDuru Mar 03 '21 at 09:41
  • Sure I could do that, may I know how I can do that? or the location/link? I am using this website for the first time – Shankar Mar 04 '21 at 09:33
  • You can use [this transfer site](https://easyupload.io/). It is free and easy to be used... But, send the workbook having my code inside it. – FaneDuru Mar 04 '21 at 09:48
  • @Shankar: OK. I will download it and see what is it about... – FaneDuru Mar 04 '21 at 13:33
  • Sure If you could try to update multiple ones to complete, may be we will be able to identify the issue, thanks for your help – Shankar Mar 04 '21 at 13:34
  • @Shankar: I checked and it looks that there are two issues: you have an empty cell at "Stack Rank" column in sheet "Open Project Details" and when the code counts the cells on the "Completed Archive" - column "Stack Rank" , it cannot count the last copied row. It overwrite it. The second problem is that for cells count = 1 it places the copied range one row before. Anyhow, I will adapt the the function code, adding only one line `If TRows = 1 Then TRows = TRows + 1` and it should work. I will also send the corrected workbook. Please, test it. – FaneDuru Mar 04 '21 at 14:18
  • @Shankar: Please, download the corrected workbook from [here](https://easyupload.io/solmnx), test it and send some feedback. – FaneDuru Mar 04 '21 at 14:55
  • @Shankar: Didn't you find some time to test your own workbook? – FaneDuru Mar 05 '21 at 14:37
  • Hi, thank you so much for your help, I used the excat sheet you send me with only change to make the "completed" ones to unassigned (to test if I change drop down to complete) if it works and then in the code I changed Target.Parent.Activate: Target.EntireRow.Delete (to.delete) since once the line item is copied to "completed archive" I want the empty row to be deleted, I get runtime error 13, I have attached the same for your reference https://easyupload.io/dumhh8 – Shankar Mar 05 '21 at 18:18
  • @Shankar: My above code does not contain any `Target.Parent.Activate`. I added it only for testing purposes, because I used to run the code line by line in the "Completed Archive" sheet and in order to select a row, the sheet in discussion must be the active one. In order to make it delete the row, this part must be deleted. The code must look like the above one. – FaneDuru Mar 06 '21 at 10:57
  • @Shankar: Please, download the working Workbook from [here](https://easyupload.io/wtxhqe). But, we here when somebody spends some time and answer our question, vote the code up, and more important tick the code left side check box, in order to make it **accepted answer**. In this way, somebody else searching for a similar issue will know that the code works... – FaneDuru Mar 06 '21 at 11:00
  • 1
    This was really helpful, thank you for your time and effort. Yes I did mark this as accepted answer – Shankar Mar 07 '21 at 08:18
0

Cut/Paste Table Row on Cell Change

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    completeArchive Target
End Sub

Sub completeArchive(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 Then
        Dim ws As Worksheet: Set ws = Target.Worksheet
        Dim rg As Range
        Set rg = Intersect(Target, ws.Range("Open_Project_Details[Status]"))
        If Not rg Is Nothing Then
            If rg.Value = "Completed" Then
                Set rg = Intersect(ws.Rows(rg.Row), _
                    ws.Range("Open_Project_Details"))
                With ws.Parent.Worksheets("Completed Archive")
                    With .Range("Completed_Archive[Stack Rank]")
                        rg.Copy .Cells(.Rows.Count + 1)
                        rg.Delete
                    End With
                End With
            End If
        End If
    End If
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
  • Should I paste the entire code into sheet macros or one sub in module and one in worksheet. Sorry but I am new to it and did not understand Option Explicit and when I pasted the entire code to worksheet, it did not do any action – Shankar Mar 03 '21 at 06:18
  • You can put both into the sheet module. The first has to be in the sheet module, while the second can be in a standard module. I was assuming that the worksheet containing this code has a table named `Open_Project_Details` that has a column named `Status` and that there is a worksheet named `Completed Archive` that has a table named `Completed_Archive` that has a column named `Stack Rank`. – VBasic2008 Mar 03 '21 at 06:37
  • To make the code simple (more understandable), it is restricted to occur when changing one cell only. If you change multiple cells at once (copy/paste) nothing will happen. Note that you need another event if the values (`Completed`) are changed by a formula. Also, if you need case-insensitivity (`C = c`), then replace `If rg.Value = "Completed" Then` with `If StrComp(rg.Value, "Completed", vbTextCompare) = 0 Then` – VBasic2008 Mar 03 '21 at 07:27