run time error 1004 in opening two different workbooks - excel

I am trying to open two different workbooks for transferring data. The complete location of workbooks are in two cells of the current workbook. First workbook opens correctly but there is error in opening other workbook. It says:
run time error 1004. File can't be found.
However, if i use path of workbook directly in the code, then it works fine. Anybody please tell me what I am doing wrong.
Sub ProcessReport()
Dim MainWb As Workbook
Dim DestWb As Workbook
' Load source and destination files
Set DestWb = Workbooks.Open(Range("E10").Value)
Set MainWb = Workbooks.Open(Range("E6").Value)
' Code for manipulation
End Sub

In your original code the second workbooks.open command is reading the cell "E6" from the workbook "DestWb" because that is the activeWorkbook at the time that command is executed, rather than the workbook where the macro is saved.
You can fix this by changing:
Set DestWb = Workbooks.Open(Range("E10").Value)
Set MainWb = Workbooks.Open(Range("E6").Value)
To this:
Set Ws = ThisWorkbook.Sheets("Sheet1")
Set DestWb = Workbooks.Open(Ws.Range("E10").Value)
Set MainWb = Workbooks.Open(Ws.Range("E6").Value)
This will save "Sheet1" from the workbook where the macro is running as an object reference so that your macro tries to use the filepaths in "E10" and "E16" from the workbook where the macro is saved. Range("E6").Value is now qualified with the worksheet ws.
You can change "Sheet1" to whatever the tab is where the filepaths are in your macro workbook.

Related

Try to Copy Data From one Workbook to another Workbook in Excel

I'm trying to create a macro in Excel that allow me to copy and paste data from one workbook to another workbook. The workbook names are always going to be different, so I used GetOpenFilename() method and stored it in a variable, so I can specifically choose the workbook I want. However, I am getting a runtime 9 error.
Here's the code so far. (macro1 is just another macro I created that crunches numbers)
Sub Everything()
Dim f1 As String
Dim f2 As String
Dim wb1 As Workbook
Dim wb2 As Workbook
f1 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb1 = Workbooks.Open(f1)
f2 = Application.GetOpenFilename(FileFilter:="Excel Files,*xl*;*xm*;")
Set wb2 = Workbooks.Open(f2)
Call Macro1
Workbooks(wb2.Name).Worksheets("Sheet1").Range("D4:D25").Copy _
Workbooks(wb1.Name).Worksheets("Sheet1").Range("E11:E32")
End Sub
I get the run time error on the very last two lines of code.
Please help, thank you!

Copy a range from a closed workbook to a specific sheet

I am currently working on a VBA script to automate a excel sheet. The goal is to have the code open a file from using a file path in cell A2 on a sheet called Reports (the file path is dynamic and is formed using information from the sheet) , copy the data from the file for range A1:E200 and to paste the data into the original workbook on a sheet called HOURS starting at A1. At the moment i have gotten to the point where the file is opened but there is a "Mismatch" error when trying to copy the information across. Below I've attached the code used. I was hoping that someone would be able to help to make sense of the error! I am having the same problem with the close section as well. Note: I am a rookie on VBA so if you could be as clear as possible
Sub Button1_Click()
Call Test
Call Copy_Method
Call CloseWorkbook
End Sub
Sub Test()
Dim strFName As String
strFName = Sheet4.Range("A2").Value
Workbooks.Open Filename:=strFName
End Sub
Sub Copy_Method()
'Copy range to another workbook using Range.Copy Method
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Set wb2 = ThisWorkbook
Set ws2 = wb2.Sheets("HOURS")
Set wb1 = ThisWorkbook.Worksheets("Reports").Range("A2")
Set ws1 = wb1.Sheets("Sheet")
ws2.Range("A1:E200") = ws1.Range("A1:E200").Value
End Sub
Sub CloseWorkbook()
Workbooks("venues_theeway_hours_August2020.XLS").Close SaveChanges:=True
End Sub
Have you tried this ?
ws2.Range("A1:E200").Value = ws1.Range("A1:E200").Value
You're making life quite difficult for yourself there, splitting the code out across 3 subs. Better to
rename the references to make them easier to differentiate source/destination.
keep it all together so the workbooks/worksheets can still be referenced as they're created:
Apologies if I've misread your requirements, my code does the following:
Reads the original workbook, sheet "Reports", range A2 for a filename.
Opens that filename as a 'source' workbook
Copies data from..
that 'source' workbook, sheet "Sheet", range A1:E200
..to original workbook, sheet "HOURS", range A1:E200
and then closes the 'source' workbook, unsaved as you've not made any changes.
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim strFName As String
Set wbDest = ThisWorkbook
Set wsDest = wbDest.Sheets("HOURS")
strFName = wbDest.Worksheets("Reports").Range("A2").Value
Set wbSource = Workbooks.Open(strFName)
Set wsSource = wbSource.Worksheets("Sheet")
wsDest.Range("A1:E200").Value = wsSource.Range("A1:E200").Value
wbSource.Close SaveChanges:=False
I'm a little puzzled about your workbook close with save? Perhaps you actually want to close the source sheet unsaved and maybe save the destination sheet you're adding data to? In that case you'll need to add this line to the end of the above code.
wbDest.Close SaveChanges:=True

Open a workbook using a link and work with it

In the code below, I am trying to open an Excel file from another program and copy a value from it. I managed to get the file to open using the FollowHyperlink command, but I cannot refer to the file to copy anything from it. It seems it doesn't actually open the file until the very end of the Sub. Does anyone know how I can refer to the Excel file I'm trying to open so I can copy a value from it? I would really appreciate some help or tips.
Here is the code to open the file:
ActiveWorkbook.FollowHyperlink Address:="cdb://byname/classname/document/CDB_View/interactive?zeichnung.z_nummer=D00846554&zeichnung.z_index=01"
How can I add to it so that I can copy a value out of it?
This code doesn't work and gives an error:
ActiveWorkbook.FollowHyperlink Address:="cdb://byname/classname/document/CDB_View/interactive?zeichnung.z_nummer=D00846554&zeichnung.z_index=01"
Workbooks("D00846554-01.xlsx").Worksheets("ZACSAANA").Range("A1").Copy ThisWorkbook.Sheets(1).Range("A2")
Try something like this:
Sub RunIt()
Dim Wb As Workbook 'Your Original Workbook
Dim sh As Worksheet 'Sheet where you want to copy to
Dim Sh2 As Worksheet 'Sheet where you're grabbing data from
Dim Wb2 As Workbook 'The Workbook you are opening via hyperlink
Set Wb = ThisWorkbook
ActiveWorkbook.FollowHyperlink ("Your HyperLink")
Set Wb2 = Workbooks(ActiveWorkbook.Name)
Set sh = Wb.sheets("Sheet in Wb1")
With Wb2
Set Sh2 = Wb2.sheets("Sheet in Wb2")
'Copy values from sh2 to sh1
End With
Wb2.Close (False) 'Close Wb2 without saving
End Sub

excel VBA error when copying a worksheet from workbook opened using activeworkbook.path

I need to copy a summary worksheet from 12 different workbooks into a regional workbook. I get a debugging error on the line that should copy the worksheet into the regional workbook. I tried using both the name and worksheet number on both the source and current workbooks. The code is included below.
Sub copyfc()
Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Set currentworkbook = ThisWorkbook
Set sourceworkbook = Workbooks.Open("C:\Country Files\BE FC12 BU19.xlsx")
sourceworkbook.Sheets("BE").Copy after:=currentworkbook.Sheets("End")
sourceworkbook.Close
End Sub
The line below will place the worksheet at the end:
sourceworkbook.Sheets("BE").Copy After:=currentworkbook.Sheets(currentworkbook.Sheets.Count)

VBA EXCEL update cell in another workbook without opening the workbook

I have a directory with list of Workbooks, I want to loop through them withouth opening them and update a Cell in a certain Sheet
I have tried to use
Dim wb As Workbook
Set wb = Workbooks("Z:\dir\bla.xls") 'THIS WILL COME TRHOUGH WHEN I LOOP
Set ws2 = wb.Sheets("TestSheet") 'SHEET NAME
Set CurCell_2 = ws2.Range("A1")
CurCell_2.Value = 5
The Problem comes it only works when I have the Workbook already open. I can use:
Workbooks.Open
But then It opens up in the background and takes to long to run through them all.
Can anyone help please
You cannot do that without opening the workbooks. However, I have found in my case that using Application.EnableEvents and setting it to false sped up greatly the process because we have macros on workbook open event.

Resources