1

Suppose I have Source.xlsm( it has only one sheet file) ,it has data in that sheet. and I have 10 different destination.xlsx file in that we have multiple sheets suppose abc, efg, ijk. I have to copy whole data present in Source.xlsx file to 10 different destination excel (under sheet_name ijk). I want to have macro code in source.xlsm when i click on the button it should copy whole data to 10 different destination files(under sheet_name ijk).

stuck for days please help me

    Private Sub CommandButton1_click()
Dim wb As Workbook
Dim lRow As Long
Dim lcol As Long
Dim total As String

ThisWorkbook.Worksheets("ViewList").Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row
lcol = Cells(1, Columns.Count).End(xlToLeft).Column
Range(Cells(1, 1), Cells(lRow, lcol)).Copy




Set wb = Workbooks.Open("C:\Users\Desktop\Destination.xlsx")
wb.Worksheets("abc").Activate

ActiveSheet.Paste
ActiveWorkbook.Save
ActiveWorkbook.Close savechanges = True
ThisWorkbook.Worksheets("ViewList").Activate
ThisWorkbook.Worksheets("ViewList").Cells(1, 1).Select
Application.CutCopyMode = False




End Sub

This code is pasting data in the middle of destination file i want to paste it in from cell (1,1)

kakaji
  • 161
  • 9
  • 1
    I am afraid that a .xlsx workbook cannot have VBA. If .xlsm, you dan do it, but can you show us what you tried by your own? – FaneDuru May 19 '21 at 10:16
  • [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) – Pᴇʜ May 19 '21 at 10:23
  • yeah .xslsm @FaneDuru can you help me – kakaji May 19 '21 at 10:35
  • If you do now show any interest in trying something by your own, at least showing what you searched and where the problem is, we cannot help you... – FaneDuru May 19 '21 at 10:56
  • @AnuragSinghKushwaha Please read [No attempt was made](http://idownvotedbecau.se/noattempt/) to understand the problem with your question and what we need to give an answer. – Pᴇʜ May 19 '21 at 11:18
  • Private Sub CommandButton1_click() Dim wb As Workbook Thisworkbok.Worksheets("ViewList").Select Selection.Copy Set wb = Workbooks.Open("C:\Users\Desktop\Destination.xslx") wb.Worksheets("abc").Activate ActiveSheet.Paste ActiveWorkbook.Save ActiveWorkbook.Close savechanges = True ThisWorkbook.Worksheets("ViewList").Activate ThisWorkbook.Worksheets("ViewList").Cells(1, 1).Select Application.CutCopyMode = False End Sub @FaneDuru I already tried – kakaji May 19 '21 at 11:18
  • Please [edit] your original question to add code. Include a proper error description and a description of what you expect your code to do versus what your code actually does. We can only help you to fix your issues if you can tell us what your issue is. – Pᴇʜ May 19 '21 at 11:20
  • I recommend you to read this [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) and use it. It will solve your issue. The problem is you use `.Select` and `Selection` and this is a bad practice and does not work as you expected. – Pᴇʜ May 19 '21 at 11:48
  • I check what you have suggested but their is one issue they are giving or i can say they giving particular column what to copy but in future in my source file column might increase or decrease @Pᴇʜ can suggest something please – kakaji May 19 '21 at 11:54
  • Do some research on `UsedRange` and copy that. It will only copy the range with data in it. Sorry for my vague comments I cannot write an answer until this question get's re-opened by the community. – Pᴇʜ May 19 '21 at 11:55
  • @Pᴇʜ i have done some update in code but i am unable resolve one issue can please check the question i have updated question – kakaji May 19 '21 at 13:51
  • @FaneDuru can you please help – kakaji May 19 '21 at 13:53
  • @AnuragSinghKushwaha Thanks, for updating the code, I recommend you to dig a bit more into how to use full references and avoid selecting or activating. This is a good practice to avoid issues in the first place and to write better and faster code. See my answer below. – Pᴇʜ May 20 '21 at 06:16

1 Answers1

0

I recommend the following

Private Sub CommandButton1_click()
    Dim wsViewList As Worksheet
    Set wsViewList = ThisWorkbook.Worksheets("ViewList")

    Dim lRow As Long
    lRow = wsViewList.Cells(wsViewList.Rows.Count, 1).End(xlUp).Row

    Dim lcol As Long
    lcol = wsViewList.Cells(1, wsViewList.Columns.Count).End(xlToLeft).Column

    Dim wb As Workbook
    Set wb = Workbooks.Open("C:\Users\Desktop\Destination.xlsx")

    wsViewList.Range("A1", wsViewList.Cells(lRow, lcol)).Copy Destination:=wb.Worksheets("abc").Range("A1").Paste
    
    'wb.Save 'this statement is not needed because you save on closing the workbook. Otherwise you would save twice which takes twice the time.
    wb.Close SaveChanges:=True
    wsViewList.Cells(1, 1).Select 'this is actually not needed unless you want to move the users view to that cell. If that is not what you need remove that line.
    Application.CutCopyMode = False
End Sub

Your code would work without any .Select or .Activate statements. Also your code needs to know where exactly you want to paste so you should specify the destination cell not only the worksheet. Also it is a good practice do do the copy and past in one statemant or at least don't do any further steps between copy and paste because that can interfere with the copyied range.

Finally named parameters need to be submitted with := not with = sign. I highly recommend you to activate Option Explicit because the line

ActiveWorkbook.Close savechanges = True

does actually do the oposite of what you think:

  1. savechanges = True because of the missing := sees savechanges as an undeclared variable of type Variant and compares if this is True. Since it is not declared and not initialized with any value the result of this statement is False.
  2. Finally you submit that result False as first parameter to ActiveWorkbook.Close so it is the same as writing ActiveWorkbook.Close False. So what your code actually does is it closes the workbook without saving changes.

If you used Option Explicit it would have notified you that savechanges is an undeclared variable. This way this fault would have been prevented. Without that notification it is much harder to see and find that issue.

Therefore I recommend always to activate Option Explicit: In the VBA editor go to ToolsOptionsRequire Variable Declaration.

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73