Copy the last sheet from file to another with ignore empty whole column

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
428
Office Version
  1. 2016
Platform
  1. Windows
Hello
I have two files in the same folder"C:\Users\MKK\Desktop\MACROS\"
the closed file contains many sheets and add new sheets every time . every sheet contains many columns . what I want importing the last sheet from closed file to open file with just copy specific columns A,B, and the last column put in column C . the last column in last sheet for closed file will change in location so the last column will be dynamic .
the last column when brings from last sheet in closed file shouldn't be empty . I mean will be some columns are empty after headers in row1 should ignore it , just brings the last column contain numeric values under header even contains some blank cells for the same column contains numbers.
I don't want to keep closed file open when import to open file .
every time run the macro in open file should delete the last imported sheet before importing to replace old data with new data .
thanks .
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What is the full name of the closed file including extension?
 
Upvote 0
both files are xlsm of extension
the closed file's name is STOCK
 
Last edited:
Upvote 0
Place this macro in the open file. Change the destination sheet name (in red) to suit your needs.
Rich (BB code):
Sub CopyData()
    Application.ScreenUpdating = False
    Dim desWS As Worksheet, srcWS As Worksheet, lRow As Long, lCol As Long
    Set desWS = ThisWorkbook.Sheets("Sheet1")
    desWS.UsedRange.Delete
    Workbooks.Open "C:\Users\MKK\Desktop\MACROS\STOCK.xlsm"
    Set srcWS = Sheets(Sheets.Count)
    With srcWS
        lRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        lCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
        .Range("A1", .Range("B" & .Rows.Count).End(xlUp)).Copy desWS.Range("A1")
        .Range(.Cells(1, lCol), .Cells(lRow, lCol)).Copy desWS.Range("C1")
    End With
    ActiveWorkbook.Close False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Excellent !
I need for help about this line
VBA Code:
    desWS.UsedRange.Delete
actually I add columns after C in open file , so just delete data from column A to C when importing data , should not delete after column C .
thanks again
 
Upvote 0
Replace that line of code with this line:
Code:
desWS.Range("A1", desWS.Range("C” & Rows.Count).End(xlUp)).ClearContents
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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