Excel VBA - Data From One Workbook to Another - excel

I'm probably missing something simple here. It just isn't copying the data from the source workbook to the target workbook and no errors are tripping. The source workbook is opening just fine. The target workbook contains this code. Both workbooks contain a sheet called 'Data'. Any help would be greatly appreciated.
Sub TransferData()
Dim wbTarget As Workbook
Dim wbSource As Workbook
Set wbSource = Workbooks.Open("C:\folder\source.xls")
Set wbTarget = Workbooks.Open("C:\folder\target.xlsm")
wbSource.Activate
Sheets("Data").Select
ActiveSheet.Range("B7").Copy
wbTarget.Activate
Sheets("Data").Select
ActiveSheet.Range("A1").Paste
End Sub

I think your problem is here:
ActiveSheet.Range("B7").Copy
Excel doesn't know which workbook to copy from:
With wbSource
.Activate
.Sheets("Data").Select
.ActiveSheet.Range("B7").Copy
End With
With wbTarget
.Activate
.Sheets("Data").Select
.ActiveSheet.Range("A1").Paste
End With
Try this code without select statements.
Also, you said the code is in your target workbook?
If you're opening both workbooks, the code should be in a third, unrelated workbook.
Sub TransferData()
Dim wbTarget As Workbook
Dim wbSource As Workbook
Set wbSource = Workbooks.Open("C:\folder\source.xls")
Set wbTarget = Workbooks.Open("C:\folder\target.xlsm")
wbSource.Sheets("Data").Range("B7").Copy wbTarget.Sheets("Data").Range("A1")
wbTarget.Close(True)
wbSource.Close(True)
End Sub
Here is code I use on a daily basis to copy a spreadsheet from one workbook to another:
Dim TWB As Workbook
Dim CopyWB As Workbook
Set TWB = ThisWorkbook
Set CopyWB = Workbooks.Open(FName, ReadOnly:=True)
CopyWB.Sheets(TWB.Sheets("Menu").ComboBox1.Text).Cells.Copy TWB.Sheets("DataSheet").Cells
CopyWB.Close (False)

Can't you store the value instead of copy paste
Sub TransferData()
Dim wbTarget As Workbook
Dim wbSource As Workbook
Set wbTarget = Application.ActiveWorkbook
Set wbSource = Workbooks.Open("C:\folder\source.xls")
wbSource.Activate
Sheets("Data").Select
xValue = ActiveSheet.Range("B7").Value
wbTarget.Activate
Sheets("Data").Select
ActiveSheet.Range("A1").Value = xValue
End Sub

Related

Copy Sheet from one Workbook to another workbook by using workbook.open

I am trying to open two workbooks then copying the first sheet of one workbook to last sheet of another workbook. But getting "copy method of worksheet failed" error. could you please help me to sort out the issue.
Sub copysheets()
Dim wb As Workbook
Dim wba As Workbook
Dim wbk As Workbook
Dim i As Integer
Dim lrow As Long
Set wb = ThisWorkbook
lrow = wb.Sheets("Target").Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To lrow
Set wba = Workbooks.Open(wb.Sheets("Target").Range("D3"))
Set wbk = Workbooks.Open(wb.Sheets("Target").Range("A" & i))
wbk.Sheets(1).copy After:=wba.Sheets(wba.Sheets.Count)
wbk.Close savechanges:=False
Next i
End Sub

VBA - How to reference two separate open workbooks without naming them?

I have looked around for awhile and cant seem to locate what I need.
Refer to Workbooks
If you know there are only two workbooks open, you can use the Index property.
Sub ReferToWorkbooks()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks(1)
Set wb2 = Workbooks(2)
Debug.Print wb1.Name
Debug.Print wb2.Name
End Sub
It is better to loop through all open workbooks and then create references to the ones you need.
Sub ReferToWorkbooks2()
Dim wb As Workbook
For Each wb In Workbooks
Debug.Print wb.Name
Next
End Sub
In case you have a worksheet in your code you use the Parent property:
Sub ReferToWorkbooks3()
Dim ws As Worksheet
Dim wb As Workbook
Set ws = Worksheets("Sheet1")
Debug.Print ws.Parent.Name
' or
Set wb = ws.Parent
Debug.Print wb.Name
End Sub
In case you have a range in your code you use the Parent property twice:
Sub ReferToWorkbooks4()
Dim rng As Range
Dim wb As Workbook
Set rng = Range("A1")
Debug.Print rng.Parent.Parent.Name
' or
Set wb = rng.Parent.Parent
Debug.Print wb.Name
End Sub
You should better explain the scenario where you might need this.

Copy/Paste cells & value

I want to copy/paste all worksheet inlcuding the values/formula in the cells to another new workbook.
This code just copy the first ws, but not all other. How can I make sure, that all ws are gettin copied and pasted without writing all the names from the ws in the vba-code?
Sub CopyPaste()
Dim ws As Worksheet, wb As Workbook
Set ws = ActiveSheet
Set wb = Workbooks.Add(xlWBATWorksheet)
ws.Range("A1:G10").Copy
wb.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteValues
wb.Sheets(1).Range("A1").PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End Sub
So i assume you will be saving the second workbook for it to be named? therefore just add your path below where you want to save it, also it now retains the sheet names.
I'm not sure why you are getting a debugger error its working fine for me, try this code and see if you still get it?
Sub newworkbook()
Dim WBN As workbook, WBC As workbook, WB As workbook
Dim WS As String
Dim SHT As Worksheet
Set WBN = Workbooks.Add
For Each WB In Application.Workbooks
If WB.Name <> WBN.Name Then
For Each SHT In WB.Worksheets
SHT.Copy After:=WBN.Sheets(WBN.Worksheets.Count)
WBN.Sheets(WBN.Worksheets.Count).Name = (SHT.Name) & " "
Next SHT
End If
Next WB
Application.DisplayAlerts = False
WBN.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete
WBN.Application.DisplayAlerts = True
ActiveWorkbook.SaveAs "C:\YOURPATH\timetable_v2.xls" 'change path to whatever
End Sub
You can try as follow:
Sub CopyPaste()
Dim aSheet As Worksheet
Dim workbook As workbook
Dim index As Integer
Set workbook = Workbooks.Add(xlWBATWorksheet)
For Each aSheet In Worksheets
aSheet.Range("A1:G10").Copy
workbook.Sheets(index).Range("A1").PasteSpecial Paste:=xlPasteFormulasAndNumberFormats
index = index + 1
Application.CutCopyMode = False
Next aSheet
End Sub
Just had a quick look for you, this seems to do the job:
credit: get digital help
Dim WBN As Workbook, WBC As Workbook, WB As Workbook
Dim WS As String
Dim SHT As Worksheet
Set WBN = Workbooks.Add
For Each WB In Application.Workbooks
If WB.Name <> WBN.Name Then
For Each SHT In WB.Worksheets
SHT.Copy After:=WBN.Sheets(WBN.Worksheets.Count)
WBN.Sheets(WBN.Worksheets.Count).Name = Left(WB.Name, 30 - Len(SHT.Name)) & "-" & SHT.Name
Next SHT
End If
Next WB
Application.DisplayAlerts = False
WBN.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete
WBN.Application.DisplayAlerts = True
I just deleted WBN.Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete
And it works fine
The new workbook is saved as an .xlsx file, but of course I Need it as an .xlsm file....when I just added it into the path, it doesnt work
ActiveWorkbook.SaveAs "U:\Excel\timetable_v2.xlsm"

Copying from one workbook to open workbook

I am trying to copy an object from a closed workbook to the currently open workbook, the code I have bee experimenting with is:
Sub test()
Dim WB1 As Workbook
Dim WBDest As Workbook
Set WBDest = Workbooks(ActiveWorkbook.Path & "\" & ActiveWorkbook.Name)
'Open up your first workbook, copy data
Set WB1 = Workbooks.Open("path to the folder\testbook.xlsx")
WB1.Sheets("Sheet1").Range("A1:F12").Copy
'paste in second workbook
WBDest.Sheets("Sheet1").Range("A1").PasteSpecial
'Close first workbook
WB1.Close savechanges:=False
End Sub
I keep getting a "subscript out of range" error with this, if I remove the WBDest info and used activeworkbook instead, it copies the object and pastes it in the same workbook as it is the activeworkbook at the time.
Could someone please guide me on this and help me figure out what I am doing wrong.
Thanks.
As mentioned by AndyG, it should be WBDest = Workbooks.Open(..). The replacement is then:
Sub test()
Dim WB1 As Workbook
Dim WBDest As Workbook
Set WBDest = Workbooks.Open(ActiveWorkbook.Path & "\" & ActiveWorkbook.Name)
'Open up your first workbook, copy data
Set WB1 = Workbooks.Open("path to the folder\testbook.xlsx")
WB1.Sheets("Sheet1").Range("A1:A7").Copy
'paste in second workbook
WBDest.Sheets("Sheet1").Range("A1:A7").PasteSpecial
'Close first workbook
WB1.Close savechanges:=False
End Sub
Note that on the 5th line you could as easily write WBDest = ActiveWorkbook if the workbook is already open as you suggest.

Copying between workbooks and worksheets

I am trying to do a copy and paste of data in between workbooks and worksheets. I have the following codes but it seems to be taking up much time. I was wondering if there is any simpler way in copying?
Sub Test1()
Dim wb As Workbook, x As String, y As String, wb1 As Workbook
For Each wb In Application.Workbooks
If wb.Name <> ThisWorkbook.Name Then x = wb.Name
Next wb
Workbooks(x).Activate
Sheets("Sheet1").Range("A:E").Copy
ActiveWindow.WindowState = xlMinimized
Sheets("Sheet1").Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteAll
Sheets("Sheet1").Range("A1").Select
Workbooks(x).Activate
ActiveWindow.WindowState = xlNormal
Sheets("Sheet1").Range("F:F").Copy
ActiveWindow.WindowState = xlMinimized
Sheets("Sheet1").Range("G:G").Select
Selection.PasteSpecial Paste:=xlPasteAll
Workbooks(x).Activate
ActiveWindow.WindowState = xlNormal
End Sub
Some headsup:- Use
Sub Test1()
Application.Screenupdating = False
'yourcode
Application.Screenupdating = True
End Sub
in your code to execute it faster
for copy paste a short verion that can be used is
Sheets("Sheet1").Range("F:F").Copy Sheets("Sheet1").Range("G:G")
Instead of activating certain books try pasting directly to the destination as mentioned in the above code.
you can remove "ActiveWindow.WindowState = xlMinimized"
EDIT:- as per added comments
dim wb1 as workbook
dim wb2 as workbook
set wb1 = ("Filename.xlsx")
set wb2 = ("filename.xlsx")
wb1.sheetname.range("A1").copy wb2.sheetname.range("A1")
you can further decalre your sheetname as well
dim ws as worksheet
set ws = worksheets("Sheetname")
Edit as per second comment (add variable to newly opened workbook)
Dim path as variant
dim wsb as workbook
path = \\C:your path ' not the sheet name
Set wsb = Workbooks.Open(filename:=myfolder & "\" & "filename".xlsm")
'your codes
I got some idea from JMAX and found a way which is as follows:
Sub test()
Dim wb As Workbook, wb2 As Workbook
Dim ws As Worksheet
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
vFile = Application.GetOpenFilename("Excel-files,*.xls", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
'Set targetworkbook
Set wb2 = ActiveWorkbook
'For instance, copy data from a range in the first workbook to another range in the other workbook
wb2.Worksheets("Sheet1").Range("A:B").Copy
wb.Worksheets("Sheet1").Activate
wb.Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteAll
End Sub

Resources