Copying Data from a worksheet to another workbook - vba

How to Copy data from one excel file to another file in different folder using VBA?
From the above link I got the codes that I was looking for.
My Query hasn't been solved yet.
I made few changes to the code to my requirements and still I get wrong results.
Below mentioned is the code that I used.
Sub TransferDataV2() 'transfer stuff from this workbook to workbook 2
Dim strPath2 As String
Dim wbkWorkbook1 As Workbook
Dim wbkWorkbook2 As Workbook
'define paths and filenames
strPath2 = "E:\Purchase Register 2015-16.xlsx"
'open files
Set wbkWorkbook1 = ThisWorkbook '### changed this
Set wbkWorkbook2 = Workbooks.Open(strPath2)
'copy the values across
'### change the sheet and range to what you need
wbkWorkbook1.Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
wbkWorkbook2.Worksheets("Sheet1").Activate
Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveCell.Offset(1).Select
ActiveCell.PasteSpecial
'close the workbook
wbkWorkbook2.Close (True)
End Sub
Now whenever I update some data in this workbook and try to save to another workbook, the copied data replaces the already available data in the strPath2 = "C:\put in this.xlsx".
The result I require is the copied data must be saved in the put in this.xlsx
but at the end of the table. Instead This code copies the data and saves them in from A2:G5. Kinldy Advice

After Selection.Copy try:
wbkWorkbook2.Worksheets("Sheet1").Cells(wbkWorkbook2.Worksheets("Sheet1").UsedRange.Row + 1, 1).PasteSpecial
'close the workbook
wbkWorkbook2.Close (True)

Related

Create a macro that copy and pastes data

I'm trying to create a macro that opens a file and copy and pastes data from that file into a new excel sheet.
Problem is, the file is updated every month. So I have a sheet on excel where I have copy pasted the path to that file (Instructions and its in cell A2). I want to know how I can adjust my code to open that file, copy its data and close the file. I also want to create a button that I can press to run the macro.
This is my code so far:
Sub ImportData_Click()
'open the source workbook and select the source sheet
Workbooks.Open Filename:="'Instructions!'$A$2" 'this is the part of the
code that I'm having trouble with
Sheets("ABC").Select
' copy the source range
Sheets("ABC").Range("C:AI").Select
Selection.Copy
' select current workbook and paste the values
ThisWorkbook.Activate
Sheets("ABC DUMP").Select
Sheets("ABC DUMP").Range("A:").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'close the source workbook
Windows("'Instructions!'$A$2").Activate
ActiveWorkbook.Close
End Sub
You have several minor syntax errors. With A2 data like:
C:\TestFolder\ABC.xls
this appears to work just fine:
Sub ImportData_Click()
'open the source workbook and select the source
Dim wb As Workbook
Workbooks.Open Filename:=Sheets("Instructions").Range("$A$2").Value
Set wb = ActiveWorkbook
Sheets("ABC").Select
' copy the source range
Sheets("ABC").Range("C:AI").Select
Selection.Copy
' select current workbook and paste the values
ThisWorkbook.Activate
Sheets("ABC DUMP").Select
Sheets("ABC DUMP").Range("A1").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'close the source workbook
wb.Close
End Sub
This can be re-coded to avoid Select
If you define a variable as a string and then set it to equal your filename ('Instructions!'$A$2) you can then use this variable in the workbooks.open function.
Your workbooks.open function also requires a pathname for this workbook; therefore define another variable for your pathname and you should be able to use:
Workbooks.Open Filename:=PathName & Filename

VBA copy selection range in loop

I want to ask about loop in VBA. I have this VBA code:
Sub OpenCopyPaste()
' open the source workbook and select the source sheet
Workbooks.Open Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx"
Sheets("Sheet1").Select
' copy the source range
Sheets("Sheet1").Range("C2:E2,C3:E3,C4:E4,C5:E5,C6:E6,C7:E7,C8:E8,C9:E9").Select
Selection.Copy
' select current workbook and paste the values starting at A1
Windows("report.xlsx").Activate
Sheets("Sheet1").Select
Sheets("Sheet1").Range("C3").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
End Sub
I want to select all data in range "C2:E2" to "C9:E9" and paste it to other workbook.
In my current code, I need to type one by one range from C2 - C9.
I want to select data from C2-C9 in loop.
Is there any way to do it in loop?
You can copy the entire range with Range("C2:E9").Copy.
Also, there is no need to use Select, Activate and Selection, it slows down the code run-time, just use fully qulifed Wroksheets and Range instead.
Code
Option Explicit
Sub OpenCopyPaste()
Dim wb As Workbook
Dim Reportwb As Workbook
' set report workbook to workbook object (works only is the file is already open)
Set Reportwb = Workbooks("report.xlsx")
' open the source workbook and select the source sheet
Set wb = Workbooks.Open(Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx")
' copy the source range and paste in 1 line , paste at "C3"
wb.Sheets("Sheet1").Range("C2:E9").Copy Destination:=Reportwb.Sheets("Sheet1").Range("C3")
Application.CutCopyMode = False
Reportwb.Save
End Sub
Instead use C2:E9
Sub OpenCopyPaste()
' open the source workbook and select the source sheet
Workbooks.Open Filename:="C:\Users\Adryan Permana\Downloads\Test\part8.xlsx"
Sheets("Sheet1").Select
' copy the source range
Sheets("Sheet1").Range("C2:E9").Select
Selection.Copy
' select current workbook and paste the values starting at A1
Windows("report.xlsx").Activate
Sheets("Sheet1").Select
Sheets("Sheet1").Range("C3").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
End Sub

Copying from closed workbook

I have been searching for over a hour now and can not find anything that works for this and would appreciate any help at all. I have been using the following code:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").Copy After:=ThisWorkbook.Sheets(1)
srcBook.Close False
End Sub
This copies as expected however, in the copied workbook it creates a new sheet "Sheet1(2)" instead of adding this to the existing sheet1. If repeated it creates "Sheet1(3)", "Sheet1(4)", "Sheet1(5)", etc...
I am really stuck with this and cannot find an answer anywhere.
There are two possible tasks at issue here:
Copying a sheet, which your code does successfully
Copying the contents of a sheet, which is also called a Range
It sounds like what you actually want to do is copy a Range to an existing worksheet. Try this instead:
Sub copySheetContents()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy
ThisWorkbook.Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
srcBook.Close False
End Sub
Note that the above assumes that your used range in the source workbook starts at cell A1.
If you are trying to just overwrite what is in your current sheet1:
Sub copySheet()
Dim srcBook As Workbook
Set srcBook = Application.Workbooks.Open(ThisWorkbook.Path & "\Book1.xlsx")
srcBook.Sheets("Sheet1").UsedRange.Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(1,1)
srcBook.Close False
End Sub

How can I code to close an open workbook using its directory path instead of its name using vba in excel?

So I've written a script that opens a certain workbook using its directory pathway (through text from a userform textbox) and I want to be able to close it at the end of the script.
My script currently opens a workbook using the file directory and copies something from that workbook and pastes it into the current workbook. The only thing I want to add is that workbook closing at the end of the sub.
Sub A()
Dim wbk As Workbook
strFirstFile = Userform1.Command.Text
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Sheet1")
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
End With
Set wbk = ThisWorkbook
wbk.Sheets("dog").Range("A1").Insert
End Sub
Bear with me I'm a super newbie.
To close the Workbook:
wbk.Close
If you want to save the workbook beforehand, do:
wbk.Save
wbk.Close

How can I disable the clipboard prompt in excel vba when I close the workbook?

So I'm working with multiple workbooks from which I copy all the data from Sheet1 from each one into their respective sheet on the master workbook. After I do that, I have the multiple workbooks encoded to close. However, an annoying prompt asking if I want to keep the copied data on clipboard consistently pops up and I want to either have it not pop up or when it does, I want "No" to be automatically chosen.
I know there's a similar question that's been asked but it hasn't worked for me and I'm thinking it's because I have a rectangular area of data instead of just a column? I really new at vba but I tried messing around with the code in Disable clipboard prompt in Excel VBA on workbook close but I've had no luck.
Here's my original code:
Sub CFM56copydata()
Dim wbk As Workbook
'The workbook is opened using the text from a textbox in a userform i.e:
strFirstFile = Userform1.dog.Text
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Sheet1")
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
End With
Set wbk2 = ThisWorkbook
wbk2.Sheets("dog").Range("A1").Insert
wbk.Close
End Sub
and here's how I tried to tweak it so I avoided using the clipbaord at all. (Didn't work, gives me a debug error on line 12)
Sub fix()
Dim wbk As Workbook
strFirstFile = Userform1.CFM56path.Text
Set wbk = Workbooks.Open(strFirstFile)
Set wbk2 = ThisWorkbook
Dim rSrc As Range
Dim rDst As Range
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set rSrc = Selection
Set rDst = wbk2.Sheets("dog").Cells("A1").Resize(rSrc.Rows.Count, rSrc.Columns.Count)
rDst = rSrc
wbk.Close
End Sub
If the clipboard prompt is the problem, just empty it after your done pasting.
wbk2.Sheets("dog").Range("A1").Insert
Application.CutCopyMode = False
wbk.Close
It should work if you change your wbk.Close statement to:
Application.DisplayAlerts = False
wbk.Close
Application.DisplayAlerts = True