YUNOVA · Excel for work
Module 26: Automation/VBA
Formulas vs Power Query vs VBA decision
Key points
- Formulas: जहाँ value बदलते ही result तुरंत बदलना चाहिए—calculations, lookups, KPIs। सबसे transparent तरीका।
- Power Query: हर महीने आने वाली files को import, clean और combine करना—बिना code, एक Refresh में। Raw data नहीं बदलता।
- VBA macros actions के लिए: files बनाना/save करना, formatting, button से कई steps। File .xlsm रखें, security ध्यान रखें।
- नियम: सबसे सरल tool जो काम करे। Data cleaning के लिए VBA से पहले Power Query देखें—maintain करना आसान है।
Record/secure/inspect/clean VBA macros
Key points
- Developer tab: File > Options > Customize Ribbon > Developer tick। फिर Developer > Record Macro से steps record करें।
- Record से पहले "Use Relative References" तय करें: off हो तो macro हमेशा उन्हीं cells (जैसे A1) पर चलेगा।
- Alt+F11 से VBA Editor में code देखें। Recorder के .Select/Selection हटाकर सीधा लिखें: Range("A1").Font.Bold = True।
- Macro के बाद Ctrl+Z काम नहीं करता—पहले save करें। Email/internet की file के macros सिर्फ भरोसेमंद source से चलाएँ।
- Macro का ढाँचा: Sub RunReport() … End Sub। Cell में value: Range("B2").Value = 100; row-column से: Cells(2, 3) = C2।
- शर्त: If Range("C2").Value > 1000 Then … Else … End If। Multi-line If में End If भूलने पर compile error आता है।
- आखिरी भरी row: lastRow = Cells(Rows.Count, "A").End(xlUp).Row; फिर For i = 2 To lastRow … Next i से हर row पर काम।
- Button: Developer > Insert > Form Controls > Button बनाकर macro assign करें; या shape पर right-click > Assign Macro।