+ Reply to Thread
Results 1 to 13 of 13

Thread: Move filled cell to right and insert blank cell

  1. #1
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Move filled cell to right and insert blank cell

    Hi,

    Please could someone advise me on how to do the following. I am creating a task tracker to help me keep track of jobs. I would like it so that i can keep adding updates to the task until it is complete. The problem is i would like to be able to see the latest update without scrolling right all the time.

    Is there some VBA that i can put in so that when i add an update in column k (attached) it auto moves the comments to the right one cell.

    This would then mean that i can see the latest update without scrolling accross and only when i need to will i have to check the previous updates.

    Any help would be appreciated. Thanks in advance.

    Chemist
    Attached Files Attached Files
    Last edited by Chemistification; 08-18-2011 at 08:40 AM. Reason: Problem sloved

  2. #2
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Move filled cell to right and insert blank cell

    Hi,

    I assume that after you enter a new value in column K and after the other comments have been shifted over one cell you want that value to be copied across to the now blank cell on the right. If not just leave out the second line in the code below which should be entered at the end of the Sheet Change event.

    If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
    End If
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  3. #3
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Hi richard,

    Thanks for taking the time to answer my question. I am having trouble entering the code into the macro that is already in there. In the macro that is already in there it finishes with

    End With
    Target.EntireRow.Delete

    I have tried adding the code after that bit (then press alt + Q to save) however i cant get it to work. I must confessi do know very little about VBA.

  4. #4
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Move filled cell to right and insert blank cell

    Hi,

    Put it immediately before your main IF..End IF block.

    Regards
    Last edited by Richard Buttrey; 08-17-2011 at 07:38 AM.
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  5. #5
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Hi Richard. Thanks again. Am i right in thinking i just cut and paste it before the main If block? If so then i still can't get it to work. I have it looking like:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim sColumn As String
        Dim lNextRow As Long
        Dim shTo As Worksheet
        Dim r As Range
    
        '********************************
        'Change these to the correct values
        sColumn = "J"
        Set shTo = Worksheets("Open Tasks")
        '********************************
        If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
    End If
        If Target.Count = 1 Then
            Set r = Target.Worksheet.Columns(sColumn & ":" & sColumn)
            If Not Intersect(Target, r) Is Nothing Then
                If Target.Value = "O" Then
                    With shTo
                        lNextRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                        Target.EntireRow.Copy .Range("A" & lNextRow).EntireRow
                    End With
                    Target.EntireRow.Delete
                End If
            End If
        End If
    End Sub

  6. #6
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Ah okay. I worked out where i was going wrong. I was working on the wrong worksheet. oops. Thanks to Richard for all your help with this. It's really appreciated.

    One last thing that would make it even better. When it moves the text over to the right it still leaves the origional text in column K. Is there anyway to modify this so that when the text moves over column K becomes blank again?

    Many thanks

    Chemist

  7. #7
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Move filled cell to right and insert blank cell

    Hi,

    Yes that's the right place to put it. Are you sure you've added it to the sheet change event of the 'Open Tasks' sheet and not the 'Closed Tasks' sheet?

    Here's a slight modification. It doesn't affect what I originally gave you which should run OK, but it just stops the macro using the IF test a second time when the entry is copied to the adjacent cell.

    If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Application.EnableEvents = False
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
            Application.EnableEvents = True
    End If
    Last edited by Richard Buttrey; 08-17-2011 at 09:50 AM.
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  8. #8
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Move filled cell to right and insert blank cell

    Hi, just add Target = "" as the penultimate line. i.e.

    If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Application.EnableEvents = False
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
            Target = ""
            Application.EnableEvents = True
    End If
    Regards
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  9. #9
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Yes you called it correct. I was putting it in the closed task sheet. Obvious now but frustrating when i couldnt get it too work. Any idea on how i odify the code to keep the cell in column K blank after it has copied accross?

    Sorry didn't see the above.

    Thanks for your help with this
    Last edited by Chemistification; 08-17-2011 at 10:03 AM.

  10. #10
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Hi,

    I am not sure if this should be a new post. If it is please let me know and i will start a new thread.

    The inclusion of the code to make the data move to the right seems to be impacting on my original code. Now when i try using the original code it comes up with errors. Is there an obvious reason why from looking at the below?

    I have attached a copy of the spreadsheet as well.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim sColumn As String
        Dim lNextRow As Long
        Dim shTo As Worksheet
        Dim r As Range
    
        '********************************
        'Change these to the correct values
        sColumn = "J"
        Set shTo = Worksheets("Closed Tasks")
        '********************************
    
        If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Application.EnableEvents = False
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
            Target = ""
            Application.EnableEvents = True
    End If
        If Target.Count = 1 Then
            Set r = Target.Worksheet.Columns(sColumn & ":" & sColumn)
            If Not Intersect(Target, r) Is Nothing Then
                If Target.Value = "C" Then
                    With shTo
                        lNextRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                        Target.EntireRow.Copy .Range("A" & lNextRow).EntireRow
                    End With
                    Target.EntireRow.Delete
                End If
            End If
        End If
    End Sub

  11. #11
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Does anyone know why i would be getting a run time error 1004 with the above script. It has two functions. One moves shifts data to the right one column when it is entered into column K

    The other moves the entire row to the closed tab when column J is set to C. Both work independently of each other but not when combined. The columns are the same in the closed tab as the open.

    When i go to debug it highlights the following line:
    Target.Copy Destination:=Target.Cells(1, 2)
    Any ideas? Again if this needs to be a new thread please let me know.

    Edit: Also noted that deleting a row throws up the same error. It also knocks the whole row out of line by 1 column to the right?
    Last edited by Chemistification; 08-18-2011 at 06:37 AM.

  12. #12
    Forum Moderator Richard Buttrey's Avatar
    Join Date
    02-15-2008
    Location
    Grappenhall, UK
    MS-Off Ver
    Excel for Windows & Mac - all versions.
    Posts
    6,566

    Re: Move filled cell to right and insert blank cell

    Hi,

    It seems to work OK for me. I'm pasting the whole code below since I've added the two Application.EnableEvents True/False lines in the second main IF.

    Also you should delete the code in the 'Closed Tasks' sheet change event. It's redundant since everything is driven by the 'Open Tasks' sheet change event.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim sColumn As String
        Dim lNextRow As Long
        Dim shTo As Worksheet
        Dim r As Range
    
        '********************************
        'Change these to the correct values
        sColumn = "J"
        Set shTo = Worksheets("Closed Tasks")
        '********************************
    
        If Not Intersect(Target, Range("K6:K1000")) Is Nothing Then
            Application.EnableEvents = False
            Target.Cells(1, 2).Insert Shift:=xlToRight
            Target.Copy Destination:=Target.Cells(1, 2)
            Target = ""
            Application.EnableEvents = True
        End If
        
        If Target.Count = 1 Then
            Set r = Target.Worksheet.Columns(sColumn & ":" & sColumn)
            If Not Intersect(Target, r) Is Nothing Then
                Application.EnableEvents = False
                If Target.Value = "C" Then
                    With shTo
                        lNextRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                        Target.EntireRow.Copy .Range("A" & lNextRow).EntireRow
                    End With
                     Target.EntireRow.Delete
                End If
            End If
            Application.EnableEvents = True
        End If
    End Sub
    Richard Buttrey

    If this was useful then please rate it appropriately.

    Click the small star icon at the bottom left of my post.

  13. #13
    Forum Contributor
    Join Date
    08-19-2009
    Location
    uk
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Move filled cell to right and insert blank cell

    Cheers Richard. Works beautifully. Thanks for all the help over two days. I am really pleased with the outcome and couldn't have done it on my own. Should hopefully keep me a bit more organised.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0