Copy from a closed workbook to an open workbook - vba

I'm trying to create code to copy from an unopened excel workbook to an open book.
This is the code I've been using:
Sub foo()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("R:\Manufacturing\First Off Log\First Off Log.xlsm", ReadOnly:=True)
Set y = Workbooks.Open("R:\Manufacturing\First Off Log\Analysis\First Off Log Analysis.xlsm")
x.Sheets("Sheet1").Range("A:K").Copy
y.Sheets("Data Input").Range("A:K").PasteSpecial
'Close x:
x.Close
End Sub
'First Off Log Analysis' will already be open. The code above reopens the workbook and causes it to crash!
Any help would be really appreciated!
Thank you! :-)

Concerning that First Off Log.xlsm is the workbook where the code is placed, it is opened already. Thus instead of:
Set x = Workbooks.Open("R:\Manufacturing\First Off Log\First Off Log.xlsm", ReadOnly:=True)
write
Set x = ThisWorkbook

Related

How to refer correct worksheet in specific workbook when multiple workbooks are open?

Sub debug_tester()
Dim A As Workbook
Set A = Workbooks.Open("D:\a.xlsm")
Dim B As Workbook
Set B = Workbooks.Open("D:\b.xlsm")
A.Sheets("sheet1_in_test").range("A1").Value = "test" 'pop out "subscript out of range" on this line
End Sub
And "sheet1_in_test" does exist in A. If I change it to number (i.e. sheet(1)) B is the one changed.
Edit:
Correct the typos. Workbook A has one sheet named "sheet1_in_test". Workbook B has one sheet named "sheet1".
Edit:
Thanks sancho.s! It seems like Workbook.Open cannot refer the workbook when the sub located on it. Using Set A = ThisWorkbook seems work, too. I'm wondering why is that.
This sub, located in a separate workbook, performs as intended
Sub open_test()
Dim wb1 As Workbook
Dim wb2 As Workbook
Set wb1 = Workbooks.Open("C:\Users\user1\Documents\a.xlsx")
Set wb2 = Workbooks.Open("C:\Users\user1\Documents\b.xlsx")
wb1.Sheets("Hoja1").Range("A1").Value = "test"
wb2.Sheets(1).Range("A1").Value = "test2"
End Sub
Try reproducing this.
Using Workbooks.Open() makes the opened workbook the active workbook. That's why using sheet(1) goes into workbook B. It's the last workbook opened and therefore the active workbook. Use Workbook.Activate to make A the active workbook.
Sub debug_tester()
Dim A As Workbook
Set A = Workbooks.Open("D:\a.xlsm")
Dim B As Workbook
Set B = Workbooks.Open("D:\b.xlsm")
A.Activate
A.Sheets("sheet1_in_test").range("A1").Value = "test"
End Sub
If you still get the "subscript out of range" message, the "sheet1_in_test" worksheet is not in the A workbook. Check the spelling between the code and the worksheet name. This assumes that you are running the code from a 3rd workbook.
If you are running this code from workbook A, Excel will close the open instance of A and reopen it but the code won't be running. In that case you can use
Set A = ThisWorkbook
Or if the code is in workbook B use
Set B = ThisWorkbook

How can I copy and paste from a closed workbook to the active workbook?

I was able to make a program that opens the workbook that copies and pastes into a workbook that is called in this case "ALL WIP". However, to make this more convenient, I would like it so no matter which workbook you run this in, it will copy and paste into that specific workbook. Thanks.
Sub AddMaskLevels()
Dim book
Set book = Workbooks.Open("MaskLevels.xlsx")
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks("MaskLevels.xlsx")'Where the information is copied from'
Set y = Workbooks("ALL WIP.xlsm")'I want to make this into the active workbook'
x.Sheets("Sheet1").Range("A1:A81").Copy
y.Sheets("All WIP").Columns(9).End(xlDown).Offset(1, 0).PasteSpecial 'The Sheet and column will always stay the same'
x.Save
x.Close
End Sub
If you want to copy from MaskLevels.xlsx into whatever workbook the macro is running from, try:
Sub AddMaskLevels()
Dim y As Workbook
Dim x As Workbook
Set y = ThisWorkbook
Set x = Workbooks.Open("MaskLevels.xlsx")
x.Sheets("Sheet1").Range("A1:A81").Copy
y.Sheets("All WIP").Columns(9).End(xlDown).Offset(1, 0).PasteSpecial
'Close the workbook (no need to save it - hopefully nothing was changed)
x.Close False
End Sub
If you want to copy from MaskLevels.xlsx into whatever workbook is active when you run the macro, try:
Sub AddMaskLevels()
Dim y As Workbook
Dim x As Workbook
Set y = ActiveWorkbook
Set x = Workbooks.Open("MaskLevels.xlsx")
x.Sheets("Sheet1").Range("A1:A81").Copy
y.Sheets("All WIP").Columns(9).End(xlDown).Offset(1, 0).PasteSpecial
'Close the workbook (no need to save it - hopefully nothing was changed)
x.Close False
End Sub

I want to copy a cell from one workbook to an active workbook using vba code

Sub Hungry4Gages()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\run_10296500.xlsm")
Set y = ActiveWorkbook
'Now, copy what you want from x:
x.Sheets("dashboard").Range("D17").Copy
'Now, paste to y worksheet:
y.Sheets("Class1").Range("A1").PasteSpecial
'Close x:
x.Close
End Sub
"dashboard " is the name of the sheet I am copying it from, and "Class1" is the sheet I am pasting on the active workbook.
I am getting the error "Subscript out of range" for the y.sheets(Class1)....
In your current code you are doing
Set x = Workbooks.Open("C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\run_10296500.xlsm")
Set y = ActiveWorkbook
The Workbooks.Open statement opens a workbook which then becomes the active workbook. Therefore both x and y are pointing to the same workbook.
So you need to switch the order around:
Set y = ActiveWorkbook
Set x = Workbooks.Open("C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\run_10296500.xlsm")
As Jeeped pointed out, if your macros are in the workbook that you are setting y to, you could use Set y = ThisWorkbook and then the order would not be important, or you could just use ThisWorkbook everywhere where you otherwise would have used y.
(Personally, I would still set the reference to the "static", for want of a better word, workbooks first and then go and open other workbooks and set their object references - but that is just my personal preference based on the way I think about what I am doing.)
You can get the value a bit faster without opening the source Workbook with external reference:
[Class1!A1].Formula = "='C:\Users\dguitron\Documents\Belize and Sam Project\Class 1\[run_10296500.xlsm]dashboard'!D17"
[Class1!A1] = [Class1!A1].Value ' optional to convert the formula to value

VBA excel comp ability mode issue

Good afternoon everyone. I create a first ever Macro with some of your help and it worked fine until I tested with actual source file that comes from Service-Now report and the only option there .XLS so when I open Source file in Excel 2013 it open in Compatibility Mode and macro give me 'Run time error '9' "Subscript out of range". What should I do to make it work?
Sub HELLO()
Dim x As Workbook
Sheets("Sheet1").Cells.Clear
'## Open workbook first:
Set x = Workbooks.Open("C:\Users\500722\Desktop\dashboard\task.xls")
'Now, transfer values from x to y:
Sheet1.Cells(1, 1) = x.Sheets("Sheet1").Range("A1")
With x.Sheets("Sheet1").UsedRange
'Now, paste to y worksheet:
Sheet1.Range("A1").Resize( _
.Rows.Count, .Columns.Count) = .Value
End With
x.Close
End Sub
As you don't have a worksheet called "Sheet1" in the workbook you opened, your code will fail when you try to access that sheet.
Change all occurrences of x.Sheets("Sheet1") to x.Sheets("Page1") and your problem will probably go away.

copying ranges from workbooks (without folder path version)

I've recently been looking for ways to speed up copying data from one worksheet to another. And I came across this nice piece of code (however this was posted in 2013).
Could you please help? I don't want to specify any path to workbooks (like in the example below). I have both worksheets open and would like to address them by filename.
I've tried changing "workbooks.open" to "window("xxx").activate" but that doesn't work.
thank you!
Sub foo()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open(" path to copying book ")
Set y = Workbooks.Open(" path to destination book ")
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial
End Sub
Sub foo()
Dim x As Workbook
Dim y As Workbook
'Replace the text between the "" with the exact name of the workbook
Set x = Workbooks("ActualNameOfWorkBook.xls")
Set y = Workbooks("ActualNameOfOtherWorkBook.xls")
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial
End Sub
When using PasteSpecial you need to add the XlPasteTypewhat (what parameter/s from the copied range you want to use). Some options of XlPasteTypewhat are: xlPasteAll , xlPasteFormulas, xlPasteValues etc.
You can read more about it at MSDN.
In the example below I am using xlPasteAll.
Code
Sub foo()
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("file_name_x.xslx") '<-- don;t forget to add the extension, .xslx or .xlsm
Set y = Workbooks.Open("file_name_y.xslx") '<-- don;t forget to add the extension, .xslx or .xlsm
x.Sheets("name of copying sheet").Range("A1").Copy
y.Sheets("sheetname").Range("A1").PasteSpecial xlPasteAll '<-- add parameter after the PasteSpecial
End Sub