How to determine the most recently viewed worksheet ("ActiveSheet") in EPPlus - excel

When you open a workbook in Excel, it shows you the worksheet you were looking at when you last saved it.
How do you determine which sheet this is when you open a workbook using EPPlus?

ExcelWorksheet activeSheet = Workbook.Worksheets.FirstOrDefault(f => f.View.TabSelected);
https://epplus.codeplex.com/discussions/456307

Related

Copy/Paste worksheet to new workbook without that worksheet's VBA code

What I want to do is to create a normal Excel workbook (.xlsx) where I summarize the inputs/outputs of some simulations by copying some sheets from a Macro-enabled workbook (.xlsm) and pasting in a normal Excel workbook. The original sheets have macros, shapes, named ranges, dropdown lists, and some formatting. I want some properties of the sheets to come over to the new workbook (named ranges, row and column formatting, cell formatting) but not others (shapes, dropdown lists, macros). I also have to distribute the .xlsm workbook to other users, so I want a solution that doesn't require the user to grant permissions.
If I copy/paste like below, then I get all the properties of the worksheet to come over to the new workbook. I have figured out how to delete shapes and remove dropdown list formatting from the sheet in the new workbook, but I can't remove the worksheet's VBA code without modifying the VBA references.
If I paste special, then I can avoid bringing over the worksheet's VBA code, but I can't bring over the named ranges.
CopySheetToWB(sht as string, wb_New as workbook)
Dim sht_Name as string, rng As Range, shp as shape
Dim ws As Worksheet, wb As Workbook, ws_New As Worksheet
' set sheet in CURRENT wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets(sht)
' copy/paste sheet to NEW wb
ws.Copy after:=wb_New.Sheets(wb_New.Sheets.Count)
' delete shapes from NEW ws
Set ws_New = wb_New.Worksheets(sht)
For Each shp In ws_New.Shapes
shp.Delete
Next shp
' remove dropdown lists from copied sheet
ws_New.Cells.Validation.Delete
End Sub
The answer to my problem turned out to be quite simple. My old code that wasn't working and my new code that is working. If you save the file type correctly, then the macros in the worksheets are not a problem as they are removed.
wb_New.SaveAs FileName:=str_fName, FileFormat:=xlWorkbookNormal 'old code
wb_New.SaveAs FileName:=str_fName, FileFormat:=51 'new code
Also make sure to include the file extension in str_fName. VBA doesn't automatically append the correct file extension based on the FileFormat you choose.

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.

selecting worksheet with multiple workbooks open crahses

If I only create one workbook the following works fine
Dim oXl As New Microsoft.Office.Interop.Excel.Application
Dim wb_main As Workbook
wb_main = oXl.Workbooks.Add
...add sheets and data to sheets
CType(wb_main.Worksheets(1), Worksheet).Select()
wb_main.SaveAs(Filename:=_files(0)._file_path.parentDir.parentDir & "out.xlsx")
But with two open, i get an error. Interop errors are not alwasy straightforward to debug.
Dim oXl As New Microsoft.Office.Interop.Excel.Application
Dim wb_main As Workbook
Dim wb_extended As Workbook
wb_main = oXl.Workbooks.Add
wb_extended = oXl.Workbooks.Add
...add sheets and data to sheets in both workbooks, no particular order
CType(wb_main.Worksheets(1), Worksheet).Select()
wb_main.SaveAs(Filename:=_files(0)._file_path.parentDir.parentDir & "out.xlsx")
I get an error HRESULT: 0x800A03EC using excel 2013. A google for the error shows alot of people with different problems as far as I can tell.
.Select() only works on the active workbook. wb_main.Activate() before selecting will solve the issue.
The second workbook open is the active one. making edits to the workbooks by using directly assigned variables does not change the active 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.

Importing Excel spreadsheet data into another Excel spreadsheet containing VBA

We need to write an Excel spreadsheet with VBA code in it; the code reads and performs operations on the data in the first worksheet.
The user will be receiving spreadsheets containing data but that do not contain the VBA code. We need to be able to import the data from the spreadsheets containing the data into the spreadsheet containing the VBA code automatically. The worksheets containing the data have the same column format and datatypes as the worksheet of the spreadsheet containing the data.
Ideally, you would open the spreadsheet containing the VBA code, be presented with a UI allowing the user to navigate to the spreadsheet containing the data, click OK and the data will be imported.
How would you go about doing this? It has to be done using VBA in Excel spreadsheets.
Many thanks.
This should get you started:
Using VBA in your own Excel workbook, have it prompt the user for the filename of their data file,
then just copy that fixed range into your target workbook (that could be either the same workbook as your macro enabled one, or a third workbook).
Here's a quick vba example of how that works:
' Get customer workbook...
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook
' make weak assumption that active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook
' get the customer workbook
filter = "Text files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)
Set customerWorkbook = Application.Workbooks.Open(customerFilename)
' assume range is A1 - C10 in sheet1
' copy data from customer to target workbook
Dim targetSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Dim sourceSheet As Worksheet
Set sourceSheet = customerWorkbook.Worksheets(1)
targetSheet.Range("A1", "C10").Value = sourceSheet.Range("A1", "C10").Value
' Close customer workbook
customerWorkbook.Close
Data can be pulled into an excel from another excel through Workbook method or External reference or through Data Import facility.
If you want to read or even if you want to update another excel workbook, these methods can be used. We may not depend only on VBA for this.
For more info on these techniques, please click here to refer the article

Resources