excel-lookup-formulas
How to write INDEX&MATCH, VLOOKUP&MATCH, XLOOKUP, and similar two-condition lookup formulas in Excel using openpyxl. Use this skill whenever the user needs to populate data cells using lookup functions that match on two criteria (e.g., series code AND year), especially when pulling cross-sheet data with dynamic row/column matching.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 93
- Forks
- 8
01 前言 Front Matter
- name
- excel-lookup-formulas
- description
- How to write INDEX&MATCH, VLOOKUP&MATCH, XLOOKUP, and similar two-condition lookup formulas in Excel using openpyxl. Use this skill whenever the user needs to populate data cells using lookup functions that match on two criteria (e.g., series code AND year), especially when pulling cross-sheet data with dynamic row/column matching.
03 SKILL.md 正文
Excel Two-Condition Lookup Formulas
Overview
When populating a data table from a source sheet using two lookup conditions (e.g., a row identifier like series code AND a column identifier like year), use INDEX&MATCH with two MATCH calls.
Standard Pattern: INDEX&MATCH with Two Conditions
=INDEX(Data!$H$21:$M$40, MATCH($D12, Data!$B$21:$B$40, 0), MATCH(H$9, Data!$H$4:$M$4, 0))
How it works:
INDEX(range, row_num, col_num)— returns value at intersection- First
MATCHfinds the row: matches series code from Task column D against Data column B - Second
MATCHfinds the column: matches year from Task row 9 against Data row 4 headers $D12— dollar-locks the column (D), leaves row relative so it moves down when copiedH$9— dollar-locks the row (9), leaves column relative so it moves right when copied
Absolute vs Relative References
| Reference | Behavior when copied |
|---|---|
$D$12 |
Never moves |
$D12 |
Column locked, row moves down |
H$9 |
Row locked, column moves right |
H12 |
Both move |
- Use
$D12for the row lookup key (series code in column D) — column stays, row varies - Use
H$9for the column lookup key (year in row 9) — row stays, column varies - Use
$on both dimensions for the lookup arrays (source ranges)
Data Sheet References
When the source data has:
- Row headers (series codes) in column B, rows 21–40 →
Data!$B$21:$B$40 - Column headers (years) in row 4, columns H–M →
Data!$H$4:$M$4 - Data values in the body →
Data!$H$21:$M$40
The year headers in the source (Data sheet row 4) may include one extra year (e.g., 2018) not present in the Task sheet, so MATCH correctly selects the right column.
Writing Formulas with openpyxl
from openpyxl import load_workbook
wb = load_workbook('file.xlsx')
task = wb['Task']
# Fill a block of cells with the same formula pattern
for row in range(12, 18): # rows 12–17
for col in range(8, 13): # columns H–L (8–12)
col_letter = task.cell(row=row, column=col).column_letter
formula = f'=INDEX(Data!$H$21:$M$40,MATCH($D{row},Data!$B$21:$B$40,0),MATCH({col_letter}$9,Data!$H$4:$M$4,0))'
task.cell(row=row, column=col).value = formula
wb.save('file.xlsx')
Key Rules
- Always verify which row/column has the lookup keys before writing formulas
- Use
load_workbookwithoutdata_only=Trueto preserve existing formulas - Use
keep_vba=False(default) unless macros must be preserved - After saving, run
recalc.pyto evaluate formulas and check for errors - Never hardcode computed values — always write Excel formulas
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.