Copy range of cells from sheet1 and paste to sheet2 into last row by using macro. Problem

reinsermat

New Member
Joined
May 5, 2020
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello!

Excel. Macros VBA.
I'am new on that.
I would like to copy range of cells from sheet1 and paste to sheet2 into last row by using macro.
Problem is, that if in sheet2 column A (or B; C) is empty, then last action (paste) will owerwrite the last row in destination (sheet2)

Just can't get it correct.

Thank you for any ideas!

VBA Code:
    Private Sub vaart_md_sis_Click()

    Dim SourceWS As Worksheet, DestWS As Worksheet
    Dim SourceRng As Range, DestCell As Range
    Dim lloop As Long
    Set SourceWS = Sheets("md")
    Set DestWS = Sheets("y_koond")
    Application.ScreenUpdating = 0
    With SourceWS
    Set DestCell = DestWS.Range("a" & Rows.Count).End(xlUp).Offset(1)
    For lloop = 1 To 7
    Set SourceRng = Choose(lloop, .Range("A3"), _
    .Range("B3"), .Range("C3"), .Range("F3"), .Range("G3"), .Range("H3"), .Range("I3"))
    SourceRng.Copy
    DestCell.Offset(, lloop - 1).PasteSpecial xlPasteValues
    Next lloop
    End With
    With Application
    .CutCopyMode = 0
    .ScreenUpdating = 1
    End With 
    End Sub
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
How about
VBA Code:
Private Sub vaart_md_sis_Click()
   Dim SourceWS As Worksheet, DestWS As Worksheet
   Dim NxtRw As Long
   
   Set SourceWS = Sheets("md")
   Set DestWS = Sheets("y_koond")
   Application.ScreenUpdating = 0
   NxtRw = DestWS.Cells.Find("*", , , , xlByRows, xlPrevious, , , False).Row + 1
   SourceWS.Range("A3:C3,F3:I3").Copy
   DestWS.Range("A" & NxtRw).PasteSpecial xlPasteValues
   Application.CutCopyMode = False
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub vaart_md_sis_Click()
   Dim SourceWS As Worksheet, DestWS As Worksheet
   Dim NxtRw As Long
  
   Set SourceWS = Sheets("md")
   Set DestWS = Sheets("y_koond")
   Application.ScreenUpdating = 0
   NxtRw = DestWS.Cells.Find("*", , , , xlByRows, xlPrevious, , , False).Row + 1
   SourceWS.Range("A3:C3,F3:I3").Copy
   DestWS.Range("A" & NxtRw).PasteSpecial xlPasteValues
   Application.CutCopyMode = False
End Sub


Thank you! But this does not seem to work for me. Nothing happens, just blank cells.
added screen how it should appear in sheet2. Last empty row.
 

Attachments

  • problem2.JPG
    problem2.JPG
    33.4 KB · Views: 10
Upvote 0
If you add a messgae box as shown, what does it say?
Rich (BB code):
   NxtRw = DestWS.Cells.Find("*", , , , xlByRows, xlPrevious, , , False).Row + 1
   MsgBox NxtRw
   SourceWS.Range("A3:C3,F3:I3").Copy
 
Upvote 0
How about
VBA Code:
Private Sub vaart_md_sis_Click()
   Dim SourceWS As Worksheet, DestWS As Worksheet
   Dim NxtRw As Long
  
   Set SourceWS = Sheets("md")
   Set DestWS = Sheets("y_koond")
   Application.ScreenUpdating = 0
   NxtRw = DestWS.Cells.Find("*", , , , xlByRows, xlPrevious, , , False).Row + 1
   SourceWS.Range("A3:C3,F3:I3").Copy
   DestWS.Range("A" & NxtRw).PasteSpecial xlPasteValues
   Application.CutCopyMode = False
End Sub


My bad!
Everything just perfect! Thank you! I love U!

Rein Sermat
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Ok, but somehow I do have problem. My Paste works well, but I lost my "Next enter ID"
I need to show at sheet "P" in Cell "A1" last number on a row from sheet "p_koond" Column A, but it does not appear as expected...
This thing is way over my head.

Thank you for any help!


Private Sub vaart_sis_GI_Click()
P_vaart_G.Hide
Dim SourceWS As Worksheet, DestWS As Worksheet
Dim NxtRw As Long

Set SourceWS = Sheets("P")
Set DestWS = Sheets("p_koond")
Application.ScreenUpdating = 0
NxtRw = DestWS.Cells.Find("*", , , , xlByRows, xlPrevious, , , False).Row + 1
SourceWS.Range("B3:N3").Copy
DestWS.Range("B" & NxtRw).PasteSpecial xlPasteValues
Application.CutCopyMode = False


Range("D3:N6").Select
Range("N6").Activate
Selection.ClearContents


With Sheets("p_koond") '<--| reference "source" sheet
With Range(.Cells(.Rows.Count, "A").End(xlUp), _
.Cells(.Cells(.Rows.Count, "A").End(xlUp).Row, .Columns.Count).End(xlToLeft)) '<--| reference is range from its column A last not empty cell to this latter cell row last not empty cell
Worksheets("p").Range("A1").Resize(, .Columns.Count).Value = .Value '<--| paste values to "target" sheet starting from its cell A1
End With
End With
End Sub
 
Upvote 0
As this is a different question, you will need to start a new thread. Thanks
 
Upvote 0

Forum statistics

Threads
1,223,264
Messages
6,171,085
Members
452,378
Latest member
Hoodzy01

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