apache poi: change the font of existing entire sheet - apache

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.

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

Old text in one color, new text in another color. Excel VBA

Each day there is a new worksheet where I write some notes. I would like to have a button that creates a new worksheet everyday, puts the old text in black and when I start writing new text (can be in a cell with text already) it should be blue.
Is there a way to do that? I can use VBA as well.
Whenever you run it, the cells with values on the activesheet will be blue and on the empty cells you will be writing in black:
Public Sub TestMe()
Cells.Font.Color = vbBlack
ActiveSheet.UsedRange.Font.Color = vbBlue
End Sub

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")

Inserting a Worksheet between two specific worksheets

I have created an excel macro which allows a user to insert a new client and based off the clients name a worksheet is created. How I have this created is by using the following code: Sheets("Template").Copy After:=Sheets("Template")
Sheets("Template (2)").Visible = True
Sheets("Template (2)").Name = ClientAbbrev
So what this does is create a copy of the template sheet which is hidden and creates a new tab which is named after the client abbreviation. This new 'entry' is generally inserted between two 'book-ends' - Template and Template End. So the newly inserted tab would go here:
Template, NEW TAB HERE, Template End.
Now that I've hidden both Template and Template End, when I Insert New Clients instead of having it appear like above, it appears like this:
Template, Template End, NEW TAB HERE.
This is a problem as I have a summing array which takes all the data between the Start and End Tabs and now it doesn't seem to work unless the Book-End Tab isn't hidden. Can anyone shed some light on this or tell me how to write a macro to insert tabs specifically between those two tabs?
Thanks!
Try this:
Option Explicit
Public Sub addNewSheet()
Dim t As Worksheet, ws As Worksheet
Set t = ThisWorkbook.Worksheets("Template")
t.Copy After:=t
Set ws = ThisWorkbook.Worksheets(t.Index + 1)
With ws
.Visible = True
.Name = "New client"
End With
End Sub