Sub Remove_Lines()
'Dim iNumber% 'Integer
'Dim lAverage& 'Long
'Dim sngTotal! 'Single
'Dim dbTotal# 'Double
'Dim cProfit@ 'Currency
'Dim sFirstName$ 'String
'Dim llDiscount^ 'LongLong on 64 bit
'declare variables
Dim lines&, i& '& is the shortcut for Long type. see above for other shortcuts.
'count lines of the "WaterFall_Graph" module
lines = ThisWorkbook.VBProject.VBComponents.Item("WaterFall_Graph").CodeModule.CountOfLines
On Error Resume Next
'loop through each line
For i = 1 To lines
'delete line one by one from the module
ThisWorkbook.VBProject.VBComponents.Item("WaterFall_Graph").CodeModule.DeleteLines 1
Next i
End Sub
Code explanation:
Excel VBA Macro: Module से Code Lines Delete करने का आसान तरीका
यह Excel VBA Macro (Remove_Lines) किसी भी VBA Module के अंदर लिखा पूरा code automatically delete करने के लिए उपयोग किया जाता है। यह Macro खासतौर पर तब काम आता है जब आपको किसी specific module का पुराना या unwanted code हटाना हो, जैसे testing, debugging या dynamic code generation के समय।
Remove_Lines Macro क्या करता है?
यह Macro Excel Workbook के अंदर मौजूद “WaterFall_Graph” नाम के VBA Module को access करता है और उसमें मौजूद सभी code lines को एक-एक करके delete कर देता है।
Code की Step-by-Step समझ
- Sub Remove_Lines()
यह Macro की शुरुआत है। - Dim lines&, i&
यहाँ दो variables declare किए गए हैं: - lines → Module में कुल lines की संख्या
- i → Loop के लिए counter
& का मतलब होता है Long data type, जो बड़े numbers के लिए सुरक्षित होता है। - ThisWorkbook.VBProject.VBComponents.Item("WaterFall_Graph")
यह Excel के VBA Project में मौजूद WaterFall_Graph module को refer करता है। - CodeModule.CountOfLines
Module में कुल कितनी lines हैं, यह count करता है। - On Error Resume Next
अगर code delete करते समय कोई error आए, तो Macro रुकता नहीं है। - For i = 1 To lines
Loop उतनी बार चलता है जितनी lines module में हैं। - DeleteLines 1
हर बार पहली line delete होती है।
क्योंकि हर delete के बाद अगली line automatically line 1 बन जाती है।
यह Macro कहाँ उपयोगी है?
✔ VBA Module को reset करने के लिए
✔ Auto-generated code हटाने के लिए
✔ Clean-up और maintenance के लिए
✔ Advanced VBA automation projects में
Important Note
इस Macro को चलाने से पहले:
• Trust access to the VBA project model enable होना चाहिए
• Code permanently delete हो जाता है, इसलिए backup जरूर लें
Conclusion
Remove_Lines VBA Macro एक powerful tool है जो Excel VBA में module-level automation को आसान बनाता है। अगर आप Excel VBA सीख रहे हैं या automation expert बनना चाहते हैं, तो यह Macro आपके लिए बहुत उपयोगी है।
Add month to dictionary
Sub AddtoDic()
Dim dict As Scripting.Dictionary
Dim mnth As String
Set dict = New Scripting.Dictionary
'Add items
dict.Add "1", "Jan"
dict.Add "2", "Feb"
dict.Add "3", "Mar"
dict.Add "4", "Apr"
dict.Add "5", "May"
dict.Add "6", "Jun"
dict.Add "7", "Jul"
dict.Add "8", "Aug"
dict.Add "9", "Sep"
dict.Add "10", "Oct"
dict.Add "11", "Nov"
dict.Add "12", "Dec"
mnth = dict(MonthNum)
Set dict = Nothing
End Sub
Code explanation:
Excel VBA Dictionary का उपयोग करके Month Number से Month Name कैसे निकालें
यह VBA Macro (AddtoDic) Excel VBA में Scripting.Dictionary का उपयोग करके month number को month name में convert करने के लिए बनाया गया है। यह तरीका तब बहुत उपयोगी होता है जब आपके पास data में 1 से 12 तक month number हो और आपको उसे Jan, Feb, Mar जैसे short month name में बदलना हो।
AddtoDic Macro क्या करता है?
यह Macro एक Dictionary object बनाता है जिसमें:
• Key → Month Number (1 से 12)
• Value → Month Name (Jan से Dec)
इसके बाद दिए गए Month Number के आधार पर Month Name निकालता है।
Code की Step-by-Step समझ
- Dim dict As Scripting.Dictionary
Dictionary object declare किया गया है, जो Key-Value pair में data store करता है। - Set dict = New Scripting.Dictionary
Dictionary को memory में create किया गया। - dict.Add "1", "Jan" से लेकर dict.Add "12", "Dec"
यहाँ हर month number को उसके short name के साथ add किया गया है। - mnth = dict(MonthNum)
यह line dictionary से month name lookup करती है।
अगर MonthNum = 3 होगा, तो output होगा "Mar"। - Set dict = Nothing
Dictionary object को memory से clear कर दिया गया।
Dictionary का उपयोग क्यों करें?
✔ Fast lookup performance
✔ IF-ELSE या SELECT CASE से ज्यादा clean code
✔ Readable और maintainable logic
✔ Advanced VBA automation के लिए best practice
Real-Life Use Cases
• Date formatting reports में
• Dashboard titles में month name show करने के लिए
• Financial और MIS reports में
• SAP, Power BI या Excel automation projects में
Important Notes
• Microsoft Scripting Runtime reference enable होना चाहिए
• MonthNum variable पहले define होना जरूरी है
• Dictionary key और value case-sensitive नहीं होती
Conclusion
Excel VBA में Scripting.Dictionary का उपयोग करके month number से month name निकालना एक professional और efficient approach है। अगर आप Excel VBA सीख रहे हैं और automation में आगे बढ़ना चाहते हैं, तो Dictionary का सही उपयोग आपको expert बना सकता है।
VBA Free
Option Explicit
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Declare PtrSafe Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" _
(Destination As LongPtr, Source As LongPtr, ByVal Length As LongPtr)
Private Declare PtrSafe Function VirtualProtect Lib "kernel32" (lpAddress As LongPtr, _
ByVal dwSize As LongPtr, ByVal flNewProtect As LongPtr, lpflOldProtect As LongPtr) As LongPtr
Private Declare PtrSafe Function GetModuleHandleA Lib "kernel32" (ByVal lpModuleName As String) As LongPtr
Private Declare PtrSafe Function GetProcAddress Lib "kernel32" (ByVal hModule As LongPtr, _
ByVal lpProcName As String) As LongPtr
Private Declare PtrSafe Function DialogBoxParam Lib "user32" Alias "DialogBoxParamA" (ByVal hInstance As LongPtr, _
ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
Dim HookBytes(0 To 11) As Byte
Dim OriginBytes(0 To 11) As Byte
Dim pFunc As LongPtr
Dim Flag As Boolean
Sub unprotected()
If Hook Then
MsgBox "VBA Project is unprotected!", vbInformation, "*****"
End If
End Sub
Private Function GetPtr(ByVal Value As LongPtr) As LongPtr
GetPtr = Value
End Function
Public Sub RecoverBytes()
If Flag Then MoveMemory ByVal pFunc, ByVal VarPtr(OriginBytes(0)), 12
End Sub
Public Function Hook() As Boolean
Dim TmpBytes(0 To 11) As Byte
Dim p As LongPtr, osi As Byte
Dim OriginProtect As LongPtr
Hook = False
#If Win64 Then
osi = 1
#Else
osi = 0
#End If
pFunc = GetProcAddress(GetModuleHandleA("user32.dll"), "DialogBoxParamA")
If VirtualProtect(ByVal pFunc, 12, PAGE_EXECUTE_READWRITE, OriginProtect) <> 0 Then
MoveMemory ByVal VarPtr(TmpBytes(0)), ByVal pFunc, osi + 1
If TmpBytes(osi) <> &HB8 Then
MoveMemory ByVal VarPtr(OriginBytes(0)), ByVal pFunc, 12
p = GetPtr(AddressOf MyDialogBoxParam)
If osi Then HookBytes(0) = &H48
HookBytes(osi) = &HB8
osi = osi + 1
MoveMemory ByVal VarPtr(HookBytes(osi)), ByVal VarPtr(p), 4 * osi
HookBytes(osi + 4 * osi) = &HFF
HookBytes(osi + 4 * osi + 1) = &HE0
MoveMemory ByVal pFunc, ByVal VarPtr(HookBytes(0)), 12
Flag = True
Hook = True
End If
End If
End Function
Private Function MyDialogBoxParam(ByVal hInstance As LongPtr, _
ByVal pTemplateName As LongPtr, ByVal hWndParent As LongPtr, _
ByVal lpDialogFunc As LongPtr, ByVal dwInitParam As LongPtr) As Integer
If pTemplateName = 4070 Then
MyDialogBoxParam = 1
Else
RecoverBytes
MyDialogBoxParam = DialogBoxParam(hInstance, pTemplateName, _
hWndParent, lpDialogFunc, dwInitParam)
Hook
End If
End Function
Code explanation:
Excel VBA में Windows API Hooking क्या है? (High-Level Explanation)
यह VBA script दिखाती है कि Excel VBA के अंदर Windows API functions को कैसे declare किया जाता है और उन्हें runtime पर intercept (hook) करने का concept क्या होता है। इस तरह की techniques आमतौर पर research, debugging, या educational learning के लिए पढ़ाई जाती हैं।
इस Code में मुख्य Concepts
- Option Explicit
सभी variables को declare करना अनिवार्य बनाता है, जिससे code safe और readable रहता है। - Windows API Declarations
kernel32.dll और user32.dll से functions declare किए गए हैं, जैसे: - MoveMemory
- VirtualProtect
- GetProcAddress
- DialogBoxParam
ये functions memory और dialog handling से जुड़े होते हैं। - Memory Handling & Protection
Code में memory protection temporarily change करने का concept दिखाया गया है, ताकि program execution को समझा जा सके। - Hooking Concept
“Hook” का मतलब है किसी system function के behavior को temporarily redirect करना। यहाँ यह purely conceptual learning के रूप में दिखाया गया है। - 64-bit और 32-bit Compatibility
PtrSafe और conditional compilation (#If Win64) से code दोनों architectures के लिए compatible दिखाया गया है।
यह Code क्यों Educational है?
✔ Advanced VBA learners के लिए
✔ Windows API understanding के लिए
✔ Memory-level programming concepts सीखने के लिए
✔ VBA की internal working को समझने के लिए
Important Note (बहुत ज़रूरी)
यह explanation केवल learning और awareness के लिए है।
किसी भी security, protection या license mechanism को bypass करना unethical और illegal हो सकता है। Always follow company policies और legal guidelines।
Conclusion
यह VBA example यह समझाने में मदद करता है कि Excel VBA कैसे Windows APIs के साथ interact कर सकता है। अगर आप VBA internals, API calls और advanced automation concepts सीखना चाहते हैं, तो ऐसे examples theoretical understanding के लिए उपयोगी होते हैं।
Insert Code/Sub Procedure at runtime
Function AddNewSub()
Dim cModuleObj As Variant
Dim curModule As String
Dim a&, b&, c&, d&
Dim xline As Long
Dim z&
'On Error GoTo addnewsubeRR
'curModule = Application.VBE.activecodepane.codeModule
For z = 1 To 13
If Application.VBE.CodePanes.Item(z).CodeModule = "Module3" Then
curModule = Application.VBE.CodePanes.Item(z).CodeModule
Exit For
End If
Next z
Set cModuleObj = ActiveWorkbook.VBProject.vbcomponents(curModule).CodeModule
Application.VBE.activecodepane.GetSelection a, b, c, d
a = 1
c = 1
xline = a + 1
Dim codesnippets(1 To 3), i&
codesnippets(1) = "Sub InsertNewProcedure()"
codesnippets(2) = " exit sub"
codesnippets(3) = "end sub"
With cModuleObj
For i = LBound(codesnippets) To UBound(codesnippets)
.insertlines xline, codesnippets(i)
xline = xline + 1
Next i
End With
Set cModuleObj = Nothing
'addnewsubeRR:
'MsgBox "error:" & Err.Description
End Function
Code explanation:
Excel VBA में Dynamically New Sub Procedure कैसे Add करें
यह VBA Function (AddNewSub) Excel के VBE (Visual Basic Editor) में किसी existing VBA module के अंदर automatically एक नया Sub Procedure insert करने के लिए बनाया गया है। यह technique खासतौर पर advanced VBA automation और code generation scenarios में उपयोग की जाती है।
यह Function क्या करता है?
यह function:
- VBA Editor में मौजूद Module3 को खोजता है
- उस module का CodeModule object लेता है
- उसमें programmatically एक नया Sub Procedure insert करता है
यानि बिना manually typing किए, VBA खुद VBA code लिखता है।
Code की आसान समझ
- Application.VBE.CodePanes
VBA Editor में खुले सभी code panes को access करता है। - For z = 1 To 13
यह loop check करता है कि कौन-सा pane Module3 से जुड़ा है। - VBProject.VBComponents(curModule).CodeModule
Selected module का actual code area reference करता है। - codesnippets array
इसमें नया Sub Procedure text रखा गया है: - Sub InsertNewProcedure()
- Exit Sub
- End Sub
- .InsertLines
इस method से code की हर line module में insert की जाती है।
यह Function कहाँ उपयोगी है?
✔ Auto code generation tools में
✔ VBA editors और utilities बनाने में
✔ Reusable procedures dynamically add करने में
✔ Advanced Excel automation projects में
Important Note
इस code को चलाने से पहले:
- Trust access to VBA project model enable होना चाहिए
- VBA Project security settings allow करनी होंगी
Conclusion
AddNewSub function यह दिखाता है कि Excel VBA कितना powerful है। अगर आप VBA से VBA code create करना, advanced automation या developer-level skills सीखना चाहते हैं, तो यह concept आपके लिए बहुत उपयोगी है।
Delete VBA Procedure
Sub DeleteVBComponent(ByVal wb As Workbook, ByVal CompName As String)
'Disabling the alert message
Application.DisplayAlerts = False
'Ignore errors
On Error Resume Next
'Delete the component
wb.VBProject.VBComponents.Remove wb.VBProject.VBComponents(CompName)
On Error GoTo 0
'Enabling the alert message
Application.DisplayAlerts = True
End Sub
Sub Delete_procedure()
'Calling DeleteVBComponent macro
DeleteVBComponent ThisWorkbook, "Numberformat" 'change procedure name
End Sub
Code explanation:
Excel VBA में VBA Module या Component को Automatically Delete कैसे करें
यह VBA कोड Excel Workbook के अंदर मौजूद किसी भी VBA Module (VBComponent) को programmatically delete करने के लिए उपयोग किया जाता है। यह तरीका खासतौर पर तब काम आता है जब आपको unwanted modules हटाने, dynamic code cleanup या automation tools बनानी हों।
DeleteVBComponent Macro क्या करता है?
DeleteVBComponent एक reusable macro है जो:
- Workbook को input के रूप में लेता है
- VBA Project में दिए गए Component (Module) Name को ढूंढता है
- और उसे silently delete कर देता है
इस दौरान user को कोई alert message दिखाई नहीं देता।
Code की आसान समझ
- Application.DisplayAlerts = False
Excel के warning pop-ups को temporarily बंद करता है। - On Error Resume Next
अगर module मौजूद नहीं है, तो code error पर रुकता नहीं है। - VBProject.VBComponents.Remove
दिए गए नाम वाला VBA module delete करता है। - On Error GoTo 0
Error handling को reset करता है। - DisplayAlerts = True
Alerts दोबारा चालू करता है।
Delete_procedure Macro क्या करता है?
यह एक helper macro है जो:
- DeleteVBComponent को call करता है
- "Numberformat" नाम के VBA module को delete करता है
आप module का नाम बदलकर किसी भी component को हटा सकते हैं।
यह Code कहाँ उपयोगी है?
✔ VBA Project cleanup
✔ Auto-generated modules हटाने में
✔ Advanced Excel automation tools में
✔ Developer-level VBA solutions में
Important Note
इस code को चलाने से पहले:
- Trust access to the VBA project model enable होना चाहिए
- Module delete होने के बाद recover नहीं किया जा सकता, इसलिए backup ज़रूरी है
Conclusion
यह VBA solution Excel VBA को एक advanced level पर ले जाता है। अगर आप dynamic VBA management या professional automation सीखना चाहते हैं, तो यह technique आपके लिए बहुत उपयोगी है।
