Adapt code to goto last empty column and run again

Chewyhairball

Active Member
Joined
Nov 30, 2017
Messages
312
Office Version
  1. 365
Platform
  1. Windows
Hi

I have this bit of code someone did for me that copies data from one worksheet to another.
copies the data into Cell A3.

This works great for 1 worksheet and if you use it on a different worksheet then it just overides the data which is what I wanted.

I would like to be able to add data from additional worksheets without over riding but instead going to the last populated cell in row5, offset up 2 cells and paste the data there.
If i could do this as many times as I wanted it would be perfect.

So if it was the first time i copied data into it it goes to cell A3 as in the code but any subsequent imports goto as described above.

thanks in advance for any help.


VBA Code:
Sub Copy_Stats()
'
    Dim srcWB As Workbook
    Dim srcSht As Worksheet
    Dim srcRng As Range
    Dim destWB As Workbook
    Dim destSht As Worksheet
    
    Set srcWB = ActiveWorkbook
   
    Set srcSht = srcWB.Worksheets("Controls")
    
    Set srcRng = srcSht.Range("A1:DZ321")
    
    Set destWB = ThisWorkbook
    Set destSht = destWB.Worksheets("Data from Stats")
    
    Application.ScreenUpdating = False
    
    destSht.Range("A3").Resize(srcRng.Rows.Count, srcRng.Columns.Count).Value = srcRng.Value
  
    srcWB.Close savechanges:=False
   Application.ScreenUpdating = True
    Sheets("stats review form").Select

MsgBox "Data successfully imported." & Chr(10) & Chr(10) & "Note: It is recommended the file is saved at this point."

    
    Exit Sub

    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
managed to use this bit of code and it seems to do what I want :)

VBA Code:
destSht.Range("XFD5").End(xlToLeft).Offset(-2, 0).Resize(srcRng.Rows.Count, srcRng.Columns.Count).Value = srcRng.Value
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,548
Members
449,038
Latest member
Guest1337

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