Click here to Skip to main content
15,885,115 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Good evening everyone,

I have a VBA problem in copying only the values of another file example c:\Example.xlsx (sheet name "Data") and see Table:

=============================================================
Copy to From Example.xlsx || Paste to Shopping.xlsxs (Sheet name "Shoppings")
=============================================================
E2:G30000 || A2:C30000
=============================================================
I2:M30000 || D2:H30000
=============================================================
O2:Q30000 || I2:K30000
=============================================================
R2:X30000 || M2:T30000
=============================================================

and ask if you can automatically sort column D (in alphabetical order) and M (oldest to newest)


Thanks again.
Then I have to paste in another file Invoice.xlsx on the “Shopping” folder on A2.

the file must run upon opening.

Thanks for your help

What I have tried:

Dim ws As Worksheet
Set ws = ActiveSheet
Dim tbl As ListObject
Posted
Updated 13-Dec-21 7:08am
Comments
OriginalGriff 13-Dec-21 4:02am    
And?
What have you tried?
Where are you stuck?
What help do you need?

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 13-Dec-21 4:37am    
You can generate most of the code quite simply by using the "Macro Record" feature of Excel.
LOGOS ITALIASRL 13-Dec-21 4:55am    
to copy the values into the table.

Thank you

1 solution

Rather than use VBA to open these files and extract the data I suggest that you use Power Query to do that part of the task - see About Power Query in Excel[^].
You can parameterise the Connections if there are going to be different file names - see Parameters - Power Query | Microsoft Docs[^]

As part of those queries you can order any of the columns any way you like.

To get this to run on opening add a call into the code for ThisWorkBook to refresh the queries e.g.
VB
Private Sub Workbook_Open()
    ThisWorkbook.RefreshAll
    MsgBox "Data has refreshed"
End Sub


An alternative as suggested by Richard MacCutchan is to ensure the "Developer" tab is visible in the Ribbon and choose Macros, Record Macro. You can then record all the steps you use while doing this manually.

Once you have recorded everything (and I recommend that you do that in a series of shorter steps rather than attempting to do everything all at once) you should examine the "code" generated and tidy it up.

For example DO NOT use stuff like this
VB
Range("A1:E90").Select
    Selection.Copy
    Windows("Book1").Activate
    Range("A1").Select
    ActiveSheet.Paste
    Range("K24").Select
It is better to explicitly specify each range and then just assign the value from the source range to the value of the target range. E.g. that same code becomes
VB
WorkBooks("Book1").Sheets(1).Range("A1:E90").Value = ThisWorkBook.Sheets(1).Range("A1:E90")
You can also tidy up all the extraneous code generated as the macro recorder will explicitly include code for all the default parameters. e.g. the sort might record as
VB
Columns("A:F").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
        "D1:D4783"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:F4783")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
could be just
VB
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D1:D4783")
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("A1:F4783")
    .Apply
End With
Finally, where you see ActiveWorkbook change it to be the explicit reference to the workbook in question even if that is only ThisWorkbook

Of course you could also use a mixture of the two techniques
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900