Copy Data from Other sheet to current open Sheet? - vba

I am trying to copy data from one excel to another and then refreshing all the pivot tables in open sheet. facing issue here that below Macro telling me to open the sheet always where i need to copy my data. please help me as i dont want to open again my second file.
Sub UReport()
'
' UReport Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Dim x As Workbook
Dim y As Workbook
'## Open both workbooks first:
Set x = Workbooks.Open("C:\Reports\HC Report.xlsx")
Set y = Workbooks.Open("C:\Reports\Main Tracker.xlsx")
'Now, copy what you want from x:
x.Sheets("HC Report").Range("A2:FI7004").Copy
'Now, paste to y worksheet:
y.Sheets("My Data").Range("A2:FI7004").PasteSpecial
Sheets("Dashboard").Select
ActiveWorkbook.RefreshAll
Sheets("My Data").Select
'Close x:
x.Close
End Sub

Sheets("Dashboard").Select
ActiveWorkbook.RefreshAll
Sheets("My Data").Select
This is ambiguous. The ActiveWorkbook is y. If the sheet "Dashboard" doesn't exist in y then you will have an error. If it's this problem then use :
x.Sheets("Dashboard").Select
Just as you did previously with the other sheets !
By the way, if you don't want any screen updating as file opening can be long (and maybe cause errors), use
Application.ScreenUpdating = false 'at the beginning of sub
' code here
Application.ScreenUpdating = true ' at the end of sub

Related

Workbook_Open Subscript out of Range

I am having issues with writing a macro in VBA when it is being initiated with the opening of my workbook. The error reads 'Error 9 - Subscript out of range'.
The Macro should look to see if there is a 'control' sheet in the current workbook, if not then it will open another workbook and copy the control sheet over - closing that workbook as it does so.
This is a strange one because it actually works if I attach it to a button, but doesn't work when I try and initiate the Macro when you open the file.
Here is my code;
Private Sub Workbook_Open()
' CreateEUC Macro
ScreenUpdating = False
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "Control" Then
exists = True
MsgBox ("There is already an EUC slide in this workbook")
End If
Next i
If Not exists Then
' Open Location
Workbooks.Open "T:\Pricing\EUC Inventory\EUC Control Sheet v0.4.xlsx"
' Copy/Paste EUC
Sheets("Control").Copy After:=ThisWorkbook.Sheets(1)
' Close EUC Workbook
Workbooks("EUC Control Sheet v0.4.xlsx").Close savechanges:=False
' Move sheet at front of workbook
Sheets("Control").Move Before:=Sheets(1)
Range("A1:H1").Select
End If
ScreenUpdating = True
End Sub
As you haven't qualified which workbook contains worksheet "Control", you're implicitly referring to the activeworkbook, and you've already proven that worksheet "Control" doesn't exist... Qualify your worksheet reference (oh, and always declare your variables!)
Private Sub Workbook_Open()
Dim i As Integer
Dim exists As Boolean
Application.ScreenUpdating = False
For i = 1 To Worksheets.Count
If Worksheets(i).Name = "Control" Then
exists = True
MsgBox ("There is already an EUC slide in this workbook")
End If
Next i
If Not exists Then
With Workbooks.Open("T:\Pricing\EUC Inventory\EUC Control Sheet v0.4.xlsx") ' Open Location
.Sheets("Control").Copy After:=ThisWorkbook.Sheets(1) ' Copy/Paste EUC
.Close savechanges:=False ' Close EUC Workbook
End With
Sheets("Control").Move Before:=Sheets(1) ' Move sheet to front of workbook
Range("A1:H1").Select
End If
Application.ScreenUpdating = True
End Sub

Copy from one worksheet to another without opening excel using vba

I'm trying to copy cell values from one excel to another using VBA. What I'm trying to do is that by clicking on the script, excel (without getting opened) values should automatically get copied from one worksheet to another. While executing my script I get an error which says Type Mismatch: 'Sheets'. I've read various posts but could not find an answer to this problem. My script looks like this:
ImportData_Click
Sub ImportData_Click()
Dim objExcel2
Set objExcel2 = CreateObject("Excel.Application")
' open the source workbook and select the source sheet
objExcel2.Workbooks.Open "<path>\test1_vbs.xlsx"
' copy the source range
Sheets("Sheet1").Range("A1:B2").Copy
' select current workbook and paste the values
ThisWorkbook.Activate
Sheets("Sheet2").Range("A1:B2").PasteSpecial xlPasteValues
' close the source workbook
Windows("<path>\test1_vbs.xlsx").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
If you are going to tranfer values only between application instances, skip the clipboard and use an array.
Option Explicit
Sub ImportData_Click()
Dim objExcel2 As Object, arr As Variant
Set objExcel2 = CreateObject("Excel.Application")
' open the source workbook and select the source sheet
With objExcel2.Workbooks.Open("c:\test\test2_vbs.xlsx")
' copy the source range to variant array
arr = .Worksheets("Sheet1").Range("A1:B2").Value
' close the source workbook
.Close savechanges:=False
End With
' select current workbook and paste the values
ThisWorkbook.Worksheets("Sheet2").Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
' close the source application instance
objExcel2.Quit
Set objExcel2 = Nothing
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub

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

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

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.