How can I amend the code for multiple cell copy and paste where "#" is a cell value?

Thanks to sktneer for the code below which I amended so that if a cell value was copied to a destination that already has a value present, it copies the destination value to column I.

I would like to amend the code so that it copies the source cell plus any cells immediately below it, where the cell value = "#"
when I select the destination cell I would like it to paste the source plus associated "#"
Any cells that contain data, with associated "#" cells should be moved to column I

Thanks

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim Rng As Range, cell As Range, val, Answer As Integer

Dim LastrowI As Long
With Sheets("Schedule")
LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
End With

Application.EnableEvents = False
val = Selection.Value
Set Rng = Range(Selection.Address)
On Error Resume Next
Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
If Err = 0 Then
    
If cell.Value <> "" Then
Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
If Answer = vbYes Then
cell.Copy Range("I" & LastrowI + 1)
cell.Value = val
Cancel = True
Rng.ClearContents
Else
    
Application.EnableEvents = True
Exit Sub

End If

End If
cell.Value = val
Cancel = True
Rng.ClearContents
        
End If
   
Application.EnableEvents = True
End Sub

Open in new window

Sample.xlsm
spar-kleOperations DirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following code to do what you are looking for..

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim Rng As Range, cell As Range, val, Answer As Integer

Dim LastrowI As Long
With Sheets("Schedule")
LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
End With

Application.EnableEvents = False
val = Selection.Value
Set Rng = Range(Selection.Address)
If Rng.Offset(1, 0).Value = "#" Then Set Rng = Union(Rng, Rng.Offset(1, 0))
On Error Resume Next
Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
If Err = 0 Then
    
If cell.Value <> "" Then
Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
If Answer = vbYes Then
cell.Copy Range("I" & LastrowI + 1)
Rng.Copy cell
Cancel = True
Rng.ClearContents
Else
    
Application.EnableEvents = True
Exit Sub

End If

End If
Rng.Copy cell
Cancel = True
Rng.ClearContents
        
End If
   
Application.EnableEvents = True
End Sub

Open in new window


Saurabh...
0
spar-kleOperations DirectorAuthor Commented:
Thanks Saurabh for your quick reply.
A few issues in order of priority

1. If I select a destination cell which already contains data, the destination value is copied to column I, but the source data is not pasted into the new destination.
2. If I want to copy to a cell that contains data, I would like to move the data with all cells = "'#" below this cell to column I
3. I would prefer not to copy formatting such as borders
4. I would like to restore emptied cell to clear fill
0
Saurabh Singh TeotiaCommented:
Use this code...

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, cell As Range, val, Answer As Integer

    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    val = Selection.Value
    Set Rng = Range(Selection.Address)
    If Rng.Offset(1, 0).Value = "#" Then Set Rng = Union(Rng, Rng.Offset(1, 0))
    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
                If cell.Offset(1, 0).Value = "#" Then
                    Range(cell, cell.Offset(1, 0)).Copy
                    Range("I" & LastrowI + 1).PasteSpecial xlPasteValues
                    Range(cell, cell.Offset(1, 0)).Clear
                Else
                    cell.Copy
                    Range("I" & LastrowI + 1).PasteSpecial xlPasteValues
                    cell.Clear
                End If

                Rng.Copy
                cell.PasteSpecial xlPasteValues
                Cancel = True
                Rng.Clear
            Else

                Application.EnableEvents = True
                Exit Sub

            End If

        End If
        Rng.Copy
        cell.PasteSpecial xlPasteValues
        Cancel = True
        Rng.Clear

    End If

    Application.EnableEvents = True
End Sub

Open in new window


Saurabh...
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

spar-kleOperations DirectorAuthor Commented:
Thanks again, however that doesn't quite work out.

My explanation probably is not clear.

The code works well when copy/pasting into empty cells, but it doesn't work out when pasting into cells where a value already exists.

I need the source cell data to still be pasted into the destination cell (with associated "#" cells)

If the range of cells that are being pasted into contains data, then I would like to move all of those cell values (with associated "#" cells) into column I
0
spar-kleOperations DirectorAuthor Commented:
I have attached an image showing what I would like to achieve
Image.jpg
0
Saurabh Singh TeotiaCommented:
Use this one...

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, cell As Range, val, Answer As Integer

    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    val = Selection.Value
    Set Rng = Range(Selection.Address)
    If Rng.Offset(1, 0).Value = "#" Then Set Rng = Union(Rng, Rng.Offset(1, 0))
    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
                If cell.Offset(1, 0).Value = "#" Then
                    Range(cell, cell.Offset(1, 0)).Copy
                    Range("I" & LastrowI + 1).PasteSpecial xlPasteValues
                    Range(cell, cell.Offset(1, 0)).Clear
                Else
                    cell.Copy
                    Range("I" & LastrowI + 1).PasteSpecial xlPasteValues
                    cell.Clear
                End If

                Rng.Copy cell
                Cancel = True
                Rng.Clear
            Else

                Application.EnableEvents = True
                Exit Sub

            End If

        End If
        Rng.Copy cell
        Cancel = True
        Rng.Clear

    End If

    Application.EnableEvents = True
End Sub

Open in new window

0
spar-kleOperations DirectorAuthor Commented:
Sorry Saurabh, that still doesn't work for me.

The data from the copied source cells are lost.
This data should be copied into the destination cells.
Please see the attached screen shots that I posted 18:01

You will also see that there may be 1 or more "#"
0
Saurabh Singh TeotiaCommented:
Sparkle,

Sorry my bad i didn't go through the logic that you put across completely and tweaked that logic to do what you are looking for..However i found that in that logic i need to tweak particular aspect further to refine it or to make the code work basis of your requirements..Here is the revised code which does what you are looking for..

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, cell As Range, val, Answer As Integer

    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    val = Selection.Value
    Set Rng = Range(Selection.Address)
    If Selection.Offset(1, 0).Value = "#" Then Set Rng = Union(Rng, Selection.Offset(1, 0))
    If Selection.Offset(2, 0).Value = "#" Then Set Rng = Union(Rng, Selection.Offset(2, 0))
    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
                cell.Copy Range("I" & LastrowI + 1)
                Rng.Copy cell
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else

                Application.EnableEvents = True
                Exit Sub

            End If
        Else

            Rng.Copy cell
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If


    End If

    Application.EnableEvents = True
End Sub

Open in new window


Saurabh...
0
spar-kleOperations DirectorAuthor Commented:
Thanks Surabh, no need to be sorry, grateful for your help

That's better ...getting there.
However, if the destination cell has a value, then I need to copy values along with cells below with "#" to column I

Also there may be up to 10 "#" cells, so need a routine something like:

Dim x as Integer
for x = 1 to 10
If Selection.Offset(1, 0).Value<> "#" Then goto Skip1
next x
Skip1
x=x-1
Set Rng = Union(Rng, Selection.Offset(x, 0))

Open in new window


I have also noted that the border formatting is also transferred with each paste

There is another problem that deserves another question which I will open after, hope that's ok
0
spar-kleOperations DirectorAuthor Commented:
If Selection.Offset(x, 0).Value<> "#" Then goto Skip1

Open in new window

0
Saurabh Singh TeotiaCommented:
If you ask me i will do something like this..

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, cell As Range, val, Answer As Integer
    Dim k As Long
    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    val = Selection.Value
    Set Rng = Range(Selection.Address)
    For k = 1 To 10
        If Selection.Offset(k, 0).Value = "#" Then
            Set Rng = Union(Rng, Selection.Offset(1, 0))
        Else
            Exit For
        End If

    Next k


    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
                cell.Copy Range("I" & LastrowI + 1)
                Rng.Copy cell
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else

                Application.EnableEvents = True
                Exit Sub

            End If
        Else

            Rng.Copy cell
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If


    End If

    Application.EnableEvents = True
End Sub

Open in new window

0
spar-kleOperations DirectorAuthor Commented:
Sorry Saurabh but that's not right either
Only one # cell is being copied from Source and no # cells are being copied to column I

Source
Name
#
#
#
#

Destination
Name
#
#

If Source with # & # & # & # is copied to Destination, then Destination with # & # needs to be copied to column I
0
spar-kleOperations DirectorAuthor Commented:
ah spotted it
should be:
Set Rng = Union(Rng, Selection.Offset(k, 0))
0
spar-kleOperations DirectorAuthor Commented:
...So all that's needed is a similar routine to copy the cells from the destination with #'s to column I
0
Saurabh Singh TeotiaCommented:
I had a feeling you will come and ask for this..Use this code...

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, cell As Range, val, Answer As Integer
    Dim k As Long, r As Range, z As Long
    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    val = Selection.Value
    Set Rng = Range(Selection.Address)
    For k = 1 To 10
        If Selection.Offset(k, 0).Value = "#" Then
            Set Rng = Union(Rng, Selection.Offset(1, 0))
        Else
            Exit For
        End If

    Next k


    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
                Set r = cell
                For z = 1 To 10
                    If cell.Offset(z, 0).Value = "#" Then
                        Set r = Union(cell, cell.Offset(z, 0))
                    Else
                        Exit For
                    End If

                Next cell

                r.Copy Range("I" & LastrowI + 1)
                r.ClearContents
                r.Interior.ColorIndex = xlNone
                Rng.Copy cell
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else

                Application.EnableEvents = True
                Exit Sub

            End If
        Else

            Rng.Copy cell
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If


    End If

    Application.EnableEvents = True
End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spar-kleOperations DirectorAuthor Commented:
Thanks Saurabh

I got an error "Invalid Next control variable reference" on line "Next cell"

I amended your original code as below.
Not sure if this is the best but seems to work

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, Rng1 As Range, cell As Range, Answer As Integer
    Dim k As Long
    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    Set Rng = Range(Selection.Address)
    For k = 1 To 10
        If Selection.Offset(k, 0).Value = "#" Then
            Set Rng = Union(Rng, Selection.Offset(k, 0))
        Else
            Exit For
        End If
    Next k

    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
            
Set Rng1 = cell
      For k = 1 To 10
        If cell.Offset(k, 0).Value = "#" Then
            Set Rng1 = Union(Rng1, cell.Offset(k, 0))
        Else
            Exit For
        End If
    Next k
            
            
                Rng1.Copy Range("I" & LastrowI + 1)
                Rng.Copy cell
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else

                Application.EnableEvents = True
                Exit Sub

            End If
        Else

            Rng.Copy cell
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If


    End If

    Application.EnableEvents = True
End Sub

Open in new window

0
spar-kleOperations DirectorAuthor Commented:
Only problem now is, if I copy a Source cell that has no # cells below it to a destination cell that has # cells, the # cells are left in place as well as copied to column I
0
spar-kleOperations DirectorAuthor Commented:
ok this seems to solve all issues ...but is it good code Saurabh

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, Rng1 As Range, cell As Range, Answer As Integer
    Dim k As Long
    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    Set Rng = Range(Selection.Address)
    For k = 1 To 10
        If Selection.Offset(k, 0).Value = "#" Then
            Set Rng = Union(Rng, Selection.Offset(k, 0))
        Else
            Exit For
        End If
    Next k

    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
            
Set Rng1 = cell
      For k = 1 To 10
        If cell.Offset(k, 0).Value = "#" Then
            Set Rng1 = Union(Rng1, cell.Offset(k, 0))
        Else
            Exit For
        End If
    Next k
            
            
                Rng1.Copy Range("I" & LastrowI + 1)
                Rng1.ClearContents
                Rng.Copy cell
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else

                Application.EnableEvents = True
                Exit Sub

            End If
        Else

            Rng.Copy cell
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If


    End If

    Application.EnableEvents = True
End Sub

Open in new window

0
Saurabh Singh TeotiaCommented:
This line of the code...

 For z = 1 To 10
                    If cell.Offset(z, 0).Value = "#" Then
                        Set r = Union(cell, cell.Offset(z, 0))
                    Else
                        Exit For
                    End If

                Next cell

Open in new window


Will be this..

 For z = 1 To 10
                    If cell.Offset(z, 0).Value = "#" Then
                        Set r = Union(cell, cell.Offset(z, 0))
                    Else
                        Exit For
                    End If

                Next z

Open in new window


I'm so used to use for each cell one loop that by default i use next cell..

Saurabh...
0
spar-kleOperations DirectorAuthor Commented:
You are an absolute star Saurabh
I'm not sure why, but my code above 23:03 works, but your code doesn't quite resolve the #'s
Are you ok for me to close this question?
...I will of course award you all of the points

I will then open a couple of other questions.
One regarding the formatting, which I'm sure is easily sorted, the others going to be a bit more difficult!!
0
spar-kleOperations DirectorAuthor Commented:
Final code, with formatting resolved:

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Dim Rng As Range, Rng1 As Range, cell As Range, Answer As Integer
    Dim k As Long
    Dim LastrowI As Long
    With Sheets("Schedule")
        LastrowI = .Cells(.Rows.Count, "I").End(xlUp).Row
    End With

    Application.EnableEvents = False
    Set Rng = Range(Selection.Address)
    For k = 1 To 10
        If Selection.Offset(k, 0).Value = "#" Then
            Set Rng = Union(Rng, Selection.Offset(k, 0))
        Else
            Exit For
        End If
    Next k

    On Error Resume Next
    Set cell = Application.InputBox("Please select the destination cell.", Type:=8)       'Select the destination cell
    If Err = 0 Then

        If cell.Value <> "" Then
            Answer = MsgBox("Are you sure you want to replace cell contents?", vbYesNo + vbQuestion, "Replace cell contents")
            If Answer = vbYes Then
Set Rng1 = cell
      For k = 1 To 10
        If cell.Offset(k, 0).Value = "#" Then
            Set Rng1 = Union(Rng1, cell.Offset(k, 0))
        Else
            Exit For
        End If
    Next k
                Rng1.Copy
                Range("I" & LastrowI + 1).PasteSpecial xlPasteValues
                Rng1.ClearContents
                Rng.Copy
                cell.PasteSpecial xlPasteValues
                Cancel = True
                Rng.ClearContents
                Rng.Interior.ColorIndex = xlNone
            Else
                Application.EnableEvents = True
                Exit Sub
            End If
        Else
            Rng.Copy
            cell.PasteSpecial xlPasteValues
            Cancel = True
            Rng.ClearContents
            Rng.Interior.ColorIndex = xlNone
        End If
    End If
    Application.EnableEvents = True
End Sub

Open in new window

0
Saurabh Singh TeotiaCommented:
Sparkle,

Always Happy to help.. :-)

Saurabh...
0
spar-kleOperations DirectorAuthor Commented:
Brilliant Saurabh
Thanks for your persistence!
Really appreciate your help!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.