Read values from named ranges with openpyxl - openpyxl

How can I read values from named ranges in Excel with openpyxl?
I've found the sourcecode at http://openpyxl.readthedocs.org/en/latest/_modules/openpyxl/workbook/names/named_range.html but I can't figure out how to use it properly.

wb = openpyxl.load_workbook(excel_file_name)
rng = wb.get_named_range(name_of_range).destinations[0]
sheet = rng[0]
address = rng[1].replace('$','')
val = sheet[address].value

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

Implement Vlookup formula on VBA, and handle error 1004

I start my adventure with VBA. I would like to create formula on VBA, use vlookup but something is going wrong with this.
Also I would like to implement vlookup for cells, when
cells from deferent column will be filled
( for example if WB_WS_Pricing.Range("A4")<>0 then
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
Sub formula()
Set WB_CMSO_MASS_IBERIA = ThisWorkbook
Set WB = ThisWorkbook
Set WB_WS_PRICING = WB.Sheets("Pricing")
Set WB_WS_HEADER = WB.Sheets("Header")
Set WB_WS_DATA = WB.Sheets("DATA")
Set WB_WS_Extension = WB.Sheets("Extension")
Set WB_WS_DELIVERING = WB.Sheets("Delivering")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
End Sub
Enyone has idea what is wrong?? For me the formula seems be fine...
You need to escape the double quotes in your formula with an extra quote in front of each (ie """" not "")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"""")"

Excel VBA Copying from Various cells from various Workbooks

Need some help coding in VBA Excel.
So currently, I have 100+ tables and have to manually input all the data to each table from many separate Excel file from each region.
You can view the table image here: https://i.stack.imgur.com/ftLdE.png
My current code still depends on targeting a range of cells to copy which is not feasible considering if there is a change in the rows/columns.
Is there anyway to collectively get all the data from each region's Excel file and insert it?
Or is it possible to target a header or a table name so that it can fill in automatically?
Pardon me if the solution is so simple and have been asked before.
Thank you so much for the help.
Sub Extract()
Dim x As Workbook
Dim y As Workbook
Dim OpenSource As String
Dim OpenTarget As String
OpenSource = Application.GetOpenFilename("File Type, *.xlsm")
If OpenSource = "False" Then Exit Sub
OpenTarget = Application.GetOpenFilename("File Type, *.xlsm")
If OpenTarget = "False" Then Exit Sub
'## Open both workbooks first:
Set x = Workbooks.Open(OpenSource) 'Source File 'thisworkbook can implement here?
Set y = Workbooks.Open(OpenTarget) 'Destination File
'Now, transfer values from x to y:
y.Sheets("Data").Range("C16:N16").Value = x.Sheets("Data").Range("C19:N19").Value
y.Sheets("Data").Range("C34:N34").Value = x.Sheets("Data").Range("C37:N37").Value
y.Sheets("Data").Range("C52:N52").Value = x.Sheets("Data").Range("C55:N55").Value
y.Sheets("Data").Range("C70:N70").Value = x.Sheets("Data").Range("C73:N73").Value
y.Sheets("Data").Range("C124:N124").Value = x.Sheets("Data").Range("C127:N127").Value
y.Sheets("Data").Range("C286:N286").Value = x.Sheets("Data").Range("C289:N289").Value
y.Sheets("Data").Range("R88:AC88").Value = x.Sheets("Data").Range("R91:AC91").Value
y.Sheets("Data").Range("R106:AC106").Value = x.Sheets("Data").Range("R109:AC109").Value
y.Sheets("Data").Range("R142:AC142").Value = x.Sheets("Data").Range("R145:AC145").Value
y.Sheets("Data").Range("R160:AC160").Value = x.Sheets("Data").Range("R163:AC163").Value
y.Sheets("Data").Range("R178:AC178").Value = x.Sheets("Data").Range("R181:AC181").Value
y.Sheets("Data").Range("R196:AC196").Value = x.Sheets("Data").Range("R199:AC199").Value
y.Sheets("Data").Range("R214:AC214").Value = x.Sheets("Data").Range("R217:AC217").Value
y.Sheets("Data").Range("R232:AC232").Value = x.Sheets("Data").Range("R235:AC235").Value
y.Sheets("Data").Range("R250:AC250").Value = x.Sheets("Data").Range("R253:AC253").Value
y.Sheets("Data").Range("R268:AC268").Value = x.Sheets("Data").Range("R271:AC271").Value
y.Sheets("Data").Range("AG88:AR88").Value = x.Sheets("Data").Range("AG91:AR91").Value
y.Sheets("Data").Range("AG106:AR106").Value = x.Sheets("Data").Range("A109:AR109").Value
y.Sheets("Data").Range("AG142:AR142").Value = x.Sheets("Data").Range("AG145:AR145").Value
y.Sheets("Data").Range("AG160:AR160").Value = x.Sheets("Data").Range("AG163:AR163").Value
y.Sheets("Data").Range("AG178:AR178").Value = x.Sheets("Data").Range("AG181:AR181").Value
y.Sheets("Data").Range("AG196:AR196").Value = x.Sheets("Data").Range("AG199:AR199").Value
y.Sheets("Data").Range("AG214:AR214").Value = x.Sheets("Data").Range("AG217:AR217").Value
y.Sheets("Data").Range("AG232:AR232").Value = x.Sheets("Data").Range("AG235:AR235").Value
y.Sheets("Data").Range("AG250:AR250").Value = x.Sheets("Data").Range("AG253:AR253").Value
y.Sheets("Data").Range("AG268:AR268").Value = x.Sheets("Data").Range("AG271:AR271").Value
MsgBox ("Done")
End Sub
Sure. as long as you know the starting point, you can dynamically count and copy rows, see modification to code below:
x.Sheets("Data").Range("C16:N" & Cells(Rows.Count, 14).End(xlUp).Row).Copy Destination:=y.Sheets("Data").Range("C19")
where i have put Cells(Rows.Count,14), the 14 relates to column N.
Apply the same logic to the rest and you should be fine! let me know how this works as i have not tested it :)
I think we have the Destination and Source the wrong way around as well.
How do I put the code in reverse? E.g. The source should be from row C19:N19 of the source file and to be copied to row C14:N14 of the destination file.
Sub Extract()
Dim x As Workbook
Dim y As Workbook
Dim OpenSource As String
Dim OpenTarget As String
OpenSource = Application.GetOpenFilename("File Type, *.xlsm")
If OpenSource = "False" Then Exit Sub
OpenTarget = Application.GetOpenFilename("File Type, *.xlsm")
If OpenTarget = "False" Then Exit Sub
Set x = Workbooks.Open(OpenSource) 'Source File
Set y = Workbooks.Open(OpenTarget) 'Destination File
x.Sheets("Data").Range("C14:N" & Cells(Rows.Count, 14).End(xlUp).Row).Copy Destination:=y.Sheets("Data").Range("C19")
MsgBox ("Done")
End Sub

Copy a specific range from a source worksheet to a target worksheet with different path

Dim path_feb As String
Dim path_mar As String
Dim wkbk_feb As Workbook
Dim wkbk_mar As Workbook
path_feb = "D:\Tranzit\2016\feb\data_feb.xlsx"
Set wkbk_feb = Workbooks.Open(path_feb)
path_mar = "D:\Tranzit\2016\mar\data_mar.xlsx"
Set wkbk_mar = Workbooks.Open(path_mar)
Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value
Worksheets("monthly").Range("B2:B1000").Value = Windows("wkbk_mar").Worksheet("impuls").Range("A2:A1000").Value
End Sub
I need a little help to work this code.
The issue begin here:
Worksheets("monthly").Range("A2:A1000").Value = Windows("wkbk_feb").Worksheet("impuls").Range("A2:A1000").Value
So, I have 3 files with different path:
D:\Tranzit\2016\feb\data_feb.xlsx
D:\Tranzit\2016\\mar\data_mar.xlsx
D:\Tranzit\2016\data_final.xlsm
I want to copy from file 1 the range A2:A1000 from "Sheet" Impuls to file 3 in range A2:A1000 from "Sheet" monthly.
and
copy from file 2 the range A2:A1000 from "Sheet" Impuls to file 3 in range B2:B1000 from "Sheet" monthly.
You declared wkbk_feb and wkbk_mar as workbook objects so you need to reference them directly:
wkbk_feb.Worksheets("impuls")....
instead of activating or selecting anything you should always specify the workbook or worksheet. So it should look something like
wkbk_total.Worksheets("monthly")... = wkbk_feb.Worksheets("impuls")....

Fill pattern to excel cell using VB.NET

How to fill a pattern to excel cell?
Based on few posts on this forum, I tried to use following, but no success so far.
a)
oWB1.Worksheets(i).Cells(7, 3).Interior.PatternIndex = 5
b)
Dim style As Microsoft.Office.Interop.Excel.Style
style = oWB1.Styles.Add("Style1")
style.Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid
oWB1.Worksheets(i).Cells(7, 3).Style = "Style1"
Any help will be really appreciated.
Thanks
This (Excel VSTO) code works:
Dim rng As Range = activeSheet.Range(1,1)
rng.Style = "Calculation"
"Calculation" is the name of the style.