Theory
Piano roll ની પાછળ: અસલી code
પાછલા lesson માં Aryan એ એક macro record કર્યો. હવે એ એને ખોલે છે, અને શોધે છે એ જાદુ નથી, એ code છે, VBA (Visual Basic for Applications) નામની ભાષામાં એક અસલી program.
આ એક BCA student માટે એક આનંદદાયક ક્ષણ છે: programming જે તમે BCA104 માં શીખ્યા (variables, loops, if-statements) અહીં પણ મોજૂદ છે, Excel તરફ લક્ષિત. એક recorded macro બસ VBA છે જે Excel એ તમારા માટે લખ્યું.
થોડું પણ VBA સમજવું કઠોર recordings ને લવચીક, editable automation માં બદલી નાખે છે. અને તમે પહેલેથી એક programmer ની જેમ વિચારો છો. ચાલો editor ખોલીએ અને થોડું code વાંચીએ.
Theory
Excel ને એક robot ની જેમ command કરવો
VBA તમને Excel ને typed commands આપવા દે છે, એક robot ને સૂચના આપવાની જેમ: 'cell A1 પર જાઓ' (Range("A1")), 'ત્યાં 100 મૂકો' (.Value = 100), 'એને bold બનાવો' (.Font.Bold = True). Excel objects થી બન્યું છે (workbooks, sheets, cells), દરેકની properties સાથે જે તમે વાંચો કે set કરો છો. VBA એ objects પર હુકૂમત કરવાની ભાષા છે. એક macro બસ આવા commands ની એક યાદી છે જે ઉપરથી નીચે ચાલે છે, બરાબર એક C program ના statements ની જેમ.
Theory
એક macro ની શારીરિક રચના
VBA Editor ખોલવા Alt+F11 દબાવો. એક macro એક Sub procedure છે:
Sub FormatReport()
Range("A1").Value = "Monthly Report"
Range("A1").Font.Bold = True
Range("B2:B50").NumberFormat = "0.00"
End Sub
એને વાંચવું: Sub ... End Sub macro ને લપેટે છે (જેમ main() એક C program ને લપેટે છે). દરેક line એક object ને command કરે છે: Range("A1") લક્ષ્ય છે, .Value અને .Font.Bold set કરાતી properties છે. Cells(row, col) એક cell ને number થી લક્ષે છે, loops ની અંદર કામનું.
આ એ code છે જે recorder એ પેદા કર્યો. હવે Aryan એને edit કરી શકે છે.
Quiz
VBA માં, line Range("A1").Value = 100 શું કરે છે?
- Range object ની Value property set કરીને cell A1 ની value 100 set કરે છે
- A1 માં જે પણ છે એ વાંચે છે
- Cell A1 delete કરે છે
- A1 નામની એક નવી sheet બનાવે છે
Show the answer
Range object ની Value property set કરીને cell A1 ની value 100 set કરે છે
Range("A1") object છે (cell), .Value એની property છે, અને = 100 એ property ને set કરે છે, તો A1 100 બની જાય છે. આ object-dot-property pattern બધા VBA નો મૂળ છે: એક object લક્ષો, એની properties વાંચો કે set કરો. આ BCA104 C માં એક struct member ને access કરવા (student.marks) વાળો એ જ idea છે, objects જેની properties તમે નામથી સંબોધો છો.
Think first
તમારા C કૌશલ્ય transfer થાય છે
Aryan ઇચ્છે છે કે એનો macro A1 થી A10 સુધી cells ને bold કરે, ફક્ત A1 નહીં. C માં એ એક for loop વાપરત. VBA માં For...Next છે. મોટા ભાગે એ કેવી રીતે loop કરશે, અને એ તમને VBA વિશે શું કહે છે?
Show the answer
કંઈક આ રીતે:
For i = 1 To 10
Cells(i, 1).Font.Bold = True
Next i
Cells(i, 1) row i, column 1 ને લક્ષે છે, તો loop A1 થી A10 ને bold કરે છે. પાઠ: VBA માં BCA104 C જેવા એ જ programming constructs છે, variables (Dim), loops (For...Next), decisions (If...Then), બસ અલગ syntax. તમારું programming જ્ઞાન સીધું transfer થાય છે; તમે એક નવી બોલી શીખો છો, વિચારવાની એક નવી રીત નહીં. એ જ કારણ છે કે એક BCA student VBA ઝડપથી પકડે છે.
Watch out
Marks ક્યાં કપાય છે
એ ન જાણવું કે VBA macros ની પાછળની ભાષા છે, Alt+F11 થી ખૂલે છે, અને એક macro એક Sub...End Sub procedure છે. object model ચૂકવું: તમે Excel ને objects (Range, Cells) અને એમની properties (.Value, .Font.Bold) ના દ્વારા command કરો છો. અને એ ભૂલવું કે recorded macro editable VBA CHHE. આ exam માટે તમારે VBA નિષ્ણાત હોવાની જરૂર નથી, પણ તમારે Sub structure, Range/Cells objects, અને એ કે programming constructs transfer થાય છે, ઓળખવું પડશે.
Theory
Record, read, edit, repeat
VBA શીખવાની સૌથી ઝડપી રીત એક loop છે: એક નાની ક્રિયા record કરો, Excel એ જે code લખ્યું એને read કરો, એક વસ્તુ edit કરો, એને run કરો. બાકી જિજ્ઞાસા કરે છે, અને તમારો BCA104 પાયો એટલે logic પહેલેથી સમજાય છે. આગળ, આપણે code ને પાછળ છોડીએ છીએ બે data-power tools માટે જેમણે આધુનિક Excel બદલ્યું: Power Query (ગંદા data ને આપોઆપ સાફ અને જોડો) અને Power Pivot. આગળ: Power Query.
Summary
Key takeaways
- VBA (Visual Basic for Applications) Excel macros ની પાછળની programming ભાષા છે.
- Alt+F11 VBA Editor ખોલે છે; એક macro એક Sub procedure છે (Sub Name() ... End Sub).
- તમે Excel ને objects (Range, Cells) અને એમની properties (.Value, .Font.Bold) ના દ્વારા command કરો છો.
- Recorded macros વાંચી શકાય એવો, editable VBA code પેદા કરે છે.
- BCA104 ના programming constructs (variables, For loops, If) સીધા VBA માં transfer થાય છે.
- યાદ રાખવાની યુક્તિ: Excel objects ને એક robot ની જેમ command કરવો, લક્ષો પછી એક property set કરો.