EPPlus Worksheets copied into a new workbook remain selected when opened in Excel - epplus

Here's the problem:
I copy worksheets from another workbook into a new ExcelPackage and save that new package.
When I open that package in Excel, the sheets that were copied appear selected.
See OutFile.xlsx here: https://1drv.ms/u/s!Aoz3mqCqpAZbjldtXKcClJkstXQK?e=yL4vQR
How do I deselect all worksheets in EPPlus before saving the workbook?
I am using EPPlus v4.5.3.3
Here's code to illustrate the problem:
ExcelPackage xlPackage = new ExcelPackage();
ExcelWorkbook xlWorkbook = xlPackage.Workbook;
xlWorkbook.Worksheets.Add("AddedSheet1");
xlWorkbook.Worksheets.Add("AddedSheet2");
xlWorkbook.Worksheets.Add("AddedSheet3");
ExcelPackage sourcePackage = new ExcelPackage(new FileInfo(#"c:\temp\AnyBlankExcelFile.xlsx"));
ExcelWorksheet sheet1 = sourcePackage.Workbook.Worksheets["Sheet1"];
xlWorkbook.Worksheets.Add("Copied1", sheet1);
xlWorkbook.Worksheets.Add("Copied2", sheet1);
xlWorkbook.Worksheets.Add("Copied3", sheet1);
xlWorkbook.Worksheets.Add("AddedSheet4");
xlPackage.SaveAs(new FileInfo(#"C:\temp\OutFile.xlsx"));
xlPackage.Dispose();
sourcePackage.Dispose();

Seems epplus selects the copied tab by default. Just need to get the worksheet which you can do with the Add call and tell it to unselect:
var c1 = xlWorkbook.Worksheets.Add("Copied1", sheet1);
c1.View.SetTabSelected(false);
var c2 = xlWorkbook.Worksheets.Add("Copied2", sheet1);
c2.View.SetTabSelected(false);
var c3 = xlWorkbook.Worksheets.Add("Copied3", sheet1);
c3.View.SetTabSelected(false);

Related

How to make Pandas Excel writer append to an existing sheet in a workbook instead of creating a new worksheet?

I have a excel workbook with two sheets ('variable','fixed'). In parallel, I have a data frame (pandas) with some data. I want to append the data in the data frame to the sheet ('variable') below the existing data in that sheet. But, the following code creates a new sheet called 'variable1' and dumps the data instead of appending to sheet 'variable'.
path = "data.xlsx"
book = load_workbook(path)
writer = pd.ExcelWriter(path, engine='openpyxl', mode='a')
writer.book = book
df3.to_excel(writer, sheet_name="variable",startrow=9,
index=False,header=False)
writer.save()
writer.close()
I have tried the above code. df3 is my pandas dataframe. I want my data to be pasted from row 9 as the existing data is until row 8 in sheet 'variable'. The code creates a new sheet ('variable1') and dumps data from row 9. I want it to paste the info in sheet ('variable') and not create a new one.
Can someone help me understand this dynamic?
To append an excel document use:
# open the workbook
wb = openpyxl.load_workbook('file_name.xlsx')
# assign a var to worksheet
ws = wb['sheet_name']
Then you can do things like:
# write to cell
wb['sheet_name'].cell(row=4, column=1).value = string_to_enter_to_cell
# format cells
for row in ws['A4:L4']:
for cell in row:
cell.value = None
cell.border = no_border

VBA Excel - Generate New Workbook with macros from Old Workbook

I currently have a workbook which contains a bunch of data on 2 of the sheets, which i use to construct a chart. Here's what it looks like:
So, In CRC I have a bunch of macros which pull through data from the Generator and MPNT and Construct the Chart in the CRC when a button on the MPNT is pressed.
MY ISSUE
For some reason, when I copy the CRC sheet out into a new worksheet using the function below, and then I use one of the buttons on the worksheet to hide/unhide data columns on the chart, it opens up the original file and uses the macros in that to run these buttons?
'''Extract Worksheet Button Function
Dim OriginalWB As Workbook, NewCRCWB As Workbook
Set OriginalWB = ThisWorkbook
Set NewCRCWB = Workbooks.Add
OriginalWB.Sheets("Generator").Copy Before:=NewCRCWB.Sheets("Sheet1")
OriginalWB.Sheets("Module Part Number Tracker").Copy Before:=NewCRCWB.Sheets("Generator")
OriginalWB.Sheets("CRC").Copy Before:=NewCRCWB.Sheets("Module Part Number Tracker")
Application.DisplayAlerts = False
NewCRCWB.Worksheets("Generator").Visible = False
NewCRCWB.Worksheets("Module Part Number Tracker").Visible = False
NewCRCWB.Worksheets("Sheet1").Delete
NewCRCWB.Activate
Application.DisplayAlerts = True
End Sub
MY AIM
To get around this I need to develop a function which will create a new workbook, and do all of the generation straight into that workbook and transfer the macros into there, however I'm not sure how to do this hence
MY QUESTION
How do I:
Open a New Workbook
Run the macros from CRC in current
workbook onto a CRC sheet on the newly opened workbook
In the process ensure that all the macros are copied through to the CRC
sheet in the New Workbook
Thanks!!

How to replace a sheet using VBA ?

I'd like to copy a sheet from a workbook and paste it in the 2nd sheet in my active workbook. I'm new with vba, it doesn't seem difficult but my code doesn't work. The sheet, which is copied, is opened in a new workbook and not in my active workbook.
Thanks for your help !
My code :
Sub copyPaste()
Dim classeur1 As Excel.Workbook
Dim classeur2 As Excel.Workbook
Set classeur1 = Workbooks.Open("Macintosh HD:Users:LouiseDhainaut:Documents:Stage:test_modifiable.xlsx")
Set classeur2 = ThisWorkbook
classeur1.Sheets(1).Copy
classeur1.Sheets(1).Paste Destination:=ThisWorkbook.Sheets(2).Range("A1")
classeur2.Save
classeur1.Close
End Sub
Try:
classeur1.Sheets(1).Copy Before:=ThisWorkbook.Sheets(2)
Instead of the
classeur1.Sheets(1).Copy
classeur1.Sheets(1).Paste Destination:=ThisWorkbook.Sheets(2).Range("A1")
Although note that copying an entire worksheet will copy the entire worksheet, not just the contents of it.
If you are looking to copy only the contents, you will need a different code, for example:
classeur1.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets(2).Range("A1")

Using macros to create and rename a new sheet in Excel 2013

My objective is simple, when a certain field consists of text, I need a button that transfers this text into a sheet.
For example, I write "GOOGLE LTD", then I click that button, a new sheet is created and renamed too "GOOGLE LTD". Refer to picture below.
I have searched through the internet, but cannot find a simple tutorial that describes how to create and rename a sheet by using macros.
Here is a backbone you will need to change the txt variable to = your range:
Sub create()
Dim txt As String
Dim sht As Worksheet
txt = ActiveSheet.Range("D4").value
Set sht = Sheets.Add(After:=Worksheet(Sheets.Count))
sht.Name = txt
End Sub

apache poi: change the font of existing entire sheet

I has a existing workbook containing multiple sheets. I have a sheet which contains some bold cells in it. I want to change the font of this entire sheet retaining the boldness of some of the cells. Can you please provide me the code snippet?
You will have to create more than one style for the workbook:
XSSFWorkbook wb = new XSSFWorkbook();
XSSFCellStyle fontStyleBold = wb.createCellStyle();
XSSFCellStyle fontStyleNormal = wb.createCellStyle();
and assign the cells you want.