Macro Recorder into VBA, Copy Paste Data Filled Cells

beginvbaanalyst

Board Regular
Joined
Jan 28, 2020
Messages
141
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I have a macro recorder file that takes a selection of data, copies, then pastes into a new sheet on ("A2:B2")
The issue is my range will always differ for my data that I copy.
I have data sets in every 4 columns ("A:D"), ("E:H") and so on but I just want the data from ("A:B"), ("E:F") to copy into my new sheet ("A2:B2") without overwriting data.
My Macro recorder code is provided below:
Sub Testcopypaste()
'
' Testcopypaste Macro
'

'
Range("A2:B27").Select 'Selection of range ("A2:B27")
Selection.Copy 'Copy Selection Above ^
Sheets("PV LIST").Select 'Make "PV LIST" Active
Range("A2").Select 'Select Cell A2 on Tab PV LIST
ActiveSheet.Paste 'Paste selection ("A2:B27") into ("A2")
Sheets("LIST").Select 'Make Sheet "LIST" active
Range("E2:F24").Select 'Select Range ("E2:F24")
Selection.Copy 'Copy Selected Range ^
Sheets("PV LIST").Select 'Make Sheet "PV LIST" Active
Range("A28").Select 'Select Cell ("A28")
ActiveSheet.Paste 'Paste Range ("E2:F24") into cell ("A28")
Sheets("LIST").Select 'Make Sheet "LIST" active
Range("I2:J30").Select 'Select Range ("I2:J30")
Selection.Copy 'Copy Selected Range ^
Sheets("PV LIST").Select 'Make Sheet "PV LIST" active
Range("A51").Select 'Select Cell ("A51")
ActiveSheet.Paste 'Paste selected Range ("I2:J30")
Sheets("LIST").Select 'Make Sheet "LIST" Active
Range("M2:N21").Select 'Select Range[Cells] ("M2:N21")
Selection.Copy 'Copy Selected Range[Cells] above ^
Sheets("PV LIST").Select 'Make Sheet "PV LIST" Active
Range("A80").Select 'Select Cell ("A80")
ActiveSheet.Paste 'Paste Selected Range ("M2:N21")
End Sub
 
Clear contents should work.
I suspect that one of the cells did not get cleared from a previous run.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I understand that you are using the Fluff macro, but anyway I leave you the documentation of my macro.

VBA Code:
Sub Testcopypaste()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, lr1 As Long, lr2 As Long
  
  'Set each of the sheets to variables.
  Set sh1 = Sheets("List")
  Set sh2 = Sheets("PV LIST")
  
  'For each column of the source sheet, from column A and to the last column with data. Advance 4 in 4
  For i = 1 To sh1.Cells(1, Columns.Count).End(xlToLeft).Column Step 4
    'Find the last row with data from the columns to copy
    lr1 = sh1.Range(sh1.Cells(2, i), sh1.Cells(Rows.Count, i + 1)).Find("*", , xlValues, , xlByRows, xlPrevious).Row
    'Find the last row of column A to paste the next block
    lr2 = sh2.UsedRange.Rows(sh2.UsedRange.Rows.Count).Row + 1
    'Copy the data from the source sheet and paste into the destination sheet
    sh1.Range(sh1.Cells(2, i), sh1.Cells(lr1, i + 1)).Copy sh2.Range("A" & lr2)
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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