Copy selected range to another worksheet, stop, and go to the next record to select (VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello....

I want to copy selected range to another worksheet, stop (as the user need to check the result), and the user will go the source sheet again to select another record.

I found this code in the internet and piece it together to achieve what I wanted to create and it works well.
The only problem is that before pasting it to another worksheet, I need to clear the cells as the range that I copy can be long or short.
I inserted the first two lines of the code to clear the previous record. But became error when I run it. I think it's because of the selection in the other sheet to copy it?
Any idea on how to do this?
I also attached the source sheet (where I want to copy it from).

Thank you in advance for the help.

VBA Code:
Private Sub CommandButton1_Click()
 
   Sheets("Search").Range("D6:G31").Select
   Selection.Clear
 
     
    Dim srcRange As Range
    Dim destSheet As Worksheet
    Dim destRange As Range
   
   
       
    ' Assuming the highlighted range is currently selected
    Set srcRange = Selection
   
    ' Set the destination worksheet and range
    Set destSheet = Worksheets("Search")
    Set destRange = destSheet.Range("D6").Resize(srcRange.Rows.Count, srcRange.Columns.Count)
   
    ' Copy the selected range to the destination
    srcRange.Copy destRange
End Sub



'activex commandbutton will always be at top right
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
        On Error GoTo 0
        With Cells(Windows(1).ScrollRow, Windows(1).ScrollColumn)
            CommandButton1.Top = .Top + 100
            CommandButton1.Left = .Left + 300 - CommandButton1.Width + 1000
        End With

 
End Sub

先日付確認用.xlsm
ABCD
1
2AES010321GL721
32GL712
43BM433
54GM735
65GH7310
76BM7010
8AES010321GL721
92GL712
103BM433
114GM735
125GH7310
136BM7010
147BM715
15ASD010611BM4310
16ASD010611BM4310
17ASS010411BM431
182BM731
193GL411
204GL421
215GL431
226GL441
237GL451
248GH731
259GH831
先日付受注
Cell Formulas
RangeFormula
B2,B8,B15:B17B2=IFERROR(IF(C2<>"",1,""),"")
B3:B7,B9:B14,B18:B25B3=IF(C3="","",IF(AND(C3<>"",B2=""),1,B2+1))
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Just replace your first 2 lines with this one:
VBA Code:
   Worksheets("Search").Range("D6:G31").Clear

To use paste you only need to identify the top left corner of the paste area (so you don't need the resize)
VBA Code:
    Set destRange = destSheet.Range("D6")

If later you decide to copy values only using an assignment (ie destRange.Value = srcRange.Value) then your current code resizing the destination would come in very handy.
 
Upvote 0
Solution
Just replace your first 2 lines with this one:
VBA Code:
   Worksheets("Search").Range("D6:G31").Clear

To use paste you only need to identify the top left corner of the paste area (so you don't need the resize)
VBA Code:
    Set destRange = destSheet.Range("D6")

If later you decide to copy values only using an assignment (ie destRange.Value = srcRange.Value) then your current code resizing the destination would come in very handy.

Thank you..... it works perfectly.
and thank you for the suggestion for resizing the destination. I changed that too.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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