Excel VBA Dim a new - excel

I'm fairly new to vba coding and I'm getting hung up with how to navigate between sheets. I have a workbook that contains a single sheet and that sheet is copied into a new workbook so that it can be edited without damaging the origianl. The issue that I'm having is when the new work book is created I need to copy informaton from a 3rd workbook and paste it into the new one. I've tried a number of things but haven't been able to figure it out. I think the issue is that the new book doesn't have a name just Book# so once I leave to activate the other book I don't have a solid path back.
Sorry for the lack of an actual example. I have added the first 2 lines based on the response I received.
Dim wb As Workbook
Set wb = ActiveWorkbook
' Open the 3rd workbook & copy
Workbooks.Open ("Z:\Terms And Conditions.xlsx")
Windows("Terms And Conditions.xlsx").Activate
ActiveSheet.Shapes.Range(Array("Picture 4")).Select
Selection.Copy
' Return to wb and paste
ActiveWorkbook(wb).Activate 'The code is stopping here
Worksheets("Sheet1").Activate
Range("A534").Select
ActiveSheet.Paste

it would help to see some code but if you are simply copying the sheet to create a new workbook you can store a reference to the activeworkbook
dim wb as workbook
set wb = activeworkbook
and then refer to wb later in the code as required

Related

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

Modify embedded Excel workbook in another Excel workbook VBA

I have an Excel workbook in which I have embedded another Excel workbook. I am able to open it with VBA, but I have no idea how to refer and edit some cells in embedded workbook. Any idea how to do that? Thanks a lot in advance.
Sub openembeddedXL2()
Sheets("sheet1").OLEObjects("SalesFile").Activate
End Sub
As long as a workbook is open you can directly refer to it by its name.
Workbooks("workbook name") etc.
Since you open the workbook with Sheets("sheet1").OLEObjects("SalesFile").Activate the workbook related to the object will then be opened as a file called "Worksheet in your current workbook". You can therefore use:
Dim wb as workbook
Sheets("sheet1").OLEObjects("SalesFile").Activate
set wb = Workbooks("Worksheet in " & ThisWorkbook.Name)
Thisworkbook.sheets("Sheet1").Range("A1").value = wb.sheets("Sheet1").range("A1").Value 'etc. etc.
wb.Close
Thisworkbook is a handy tool here, as it will always refer to the workbook the macro is in, despite which workbook is currently active.

Excel VBA to copy and paste as values from a closed workbook to an active workbook, Mac OS X

I know this is a common type of query, but I have not been able to find an answer yet to my specific instance after active searching. I am also a new VBA user, so please bear with me.
I want to import an entire sheet of data (AllDATA tab in HISTORY.XLSM) that contains live formulae (range A1:HW6000) from a closed workbook into an active workbook, but only paste as values into the active workbook. More simply, want to replicate one sheet from the closed workbook as values (not live formuale) into the active worksheet.
I am on Mac OS X.
This is the code that I have been working with (running the code from the active workbook).
Sub GetRange()
With Range("A1:HW6000")
.Formula = "='C:\[HISTORY.xlsm]ALLDATA'!A1"
.Value = .Value
End With
End Sub
This code seems to import formats but not actual pasted values into my active workbook.
Any ideas?
You should do a real copy-paste like that
Sub ImportData()
Dim App As New Excel.Application 'create a new (hidden) Excel
' remember active sheet
Dim wsActive As Worksheet
Set wsActive = ThisWorkbook.ActiveSheet
' open the import workbook in new Excel (as read only)
Dim wbImport As Workbook
Set wbImport = App.Workbooks.Open(Filename:="C:\History.xlsm", UpdateLinks:=True, ReadOnly:=True)
'copy the data of the import sheet
wbImport.Worksheets("AllDATA").Range("A1:HW6000").Copy
wsActive.Range("A1").PasteSpecial Paste:=xlPasteFormats 'paste formats
wsActive.Range("A1").PasteSpecial Paste:=xlPasteValues 'paste values
App.CutCopyMode = False 'clear clipboard (prevents asking when wb is closed)
wbImport.Close SaveChanges:=False 'close wb without saving
App.Quit 'quit the hidden Excel
End Sub

run time error 1004 in opening two different workbooks

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.

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