Tababarkaan wuxuu sharxi doonaa VBA Array, noocyo kala duwan oo hannaan ah, kala duwanaansho array ah, iyo habab habayn iyadoo la kaashanayo tusaalooyinka barnaamijka qiimaha hal xog. Waxa ay leedahay 1 ilaa 1 xidhiidh ie Halkii aad ka abuuri lahayd doorsoomayaal badan, waxaad abuuri kartaa hal doorsoome oo waxaad kaydin kartaa dhammaan noocyada qiimayaasha. Doorsoomiyahan waxa loo yaqaan ARRAY Waxaad baran doontaa waxa ay tahay VBA array, hal-cabbir, iyo arysyo laba-cabbir ah oo ay la socdaan noocyada kala duwan ee arrays sida Fixed and Dynamic. Waxaan sidoo kale fahmi doonaa habab kala duwanaansho oo loo isticmaalo VBA.
VBA Array
Arrays waa nooc gaar ah oo doorsoome oo kaydin kara qiimayaal badan oo isku nooc ah .
Tusaale ahaan, haddii aad haysato magacyada 100 shaqaale ah, ka dib halkii aad ka abuuri lahayd 100 doorsoome oo nooca xogta ah, waxa aad samayn kartaa hal doorsoome kala duwan oo ah nooca xargaha oo aad ku meelayso 100 qiime. Isku soo wada duuboo doorsoome isku mid ah.
Hal cabbir oo kala duwan
Array ka kooban dhammaan curiyeyaasha hal saf ama tiir keliya waxa loo yaqaannaa hal-beeg-beeg ah. Ku-qorista magacyada dhammaan ardayda fasalka hal tiir oo keliya ayaa tusaale u ah hal-beeg hal-beeg ah. Waxaa lagu dhawaaqay sida muuqataarray waxa lagu dhawaaqay sida hoos ka muuqata. >
>Dim ArrayName(FirstIndex To LastIndex, FirstIndex To LastIndex) Sida DataType . si loogu beddelo kala-duwanaanshaha array. Koodhkani waxa uu abuuri doonaa Mys[10]Sub Example() Dim Mys As Variant Mys = Application.Transpose(Range("A1:A10")) End Sub
Q #4) Waa maxay kala duwananshiyaha diyaarinta VBA?>Jawab: Qaabka kala duwani wuxuu aqbali doonaa dhammaan noocyada xogta noocyada kala duwan ee tusmihiisa tusaale ahaan waxaad ku kaydin kartaa noocyada kala duwan ee qiyamka hal array.
Tusaale: >
0>Dim arrayData(3) As VariantarrayData(0) = "Vikas Vipal"
arrayData(1) = 411234567890#
Siyaabaha wax loogu badalo shaxanka inta lagu jiro runtime iyo sidoo kale ilaalinta qiyamka iyadoo la isticmaalayo redim ilaalinta ayaa laga hadlay tusaalayaal. Ugu dambayntii, waxaanu barannay habab Array ah oo naga caawin doona fulinta hawlo dhawr ah.
hoose.Dim arrayname(hoos ilaa UpperBound) Sida DataType
Waxaa jira siyaabo badan oo lagu dhawaaqo array. Hoos waxaa ku qoran dhawr tusaale.
Tusaale:
#1) Dim MyArrayExample> Wuxuu abuuraa array leh goobta 0,1,2,3 taasoo aqbali doonta qiyamka isdhexgalka.
>#2 ilaa 3 oo abuuraa array leh goobta 0,1,2,3 taas oo aqbali doonta qiyamka String.
#3) Dim MyArray2(13 ilaa 15) Sida labanlaab ah Wuxuu abuuraa array ka bilaabmaya 13 i.e. 13, 14, iyo 15, oo aqbala labanlaab qiimayaal. Waxaan soo sheegnay xudduudaha hoose sida 13, markaa array waxay bilaabi doontaa qoondaynta qiyamka goobta 13 halkii ay ka ahaan lahayd 0.
Aynu abuurno kood fudud oo aynu fahanno dhammaan 3da hab ee ku dhawaaqida array.
Fiiro gaar ah: Si aad u qorto code VB Fur Microsoft Excel (noocyada la taageeray waa Excel 2007, 2010, 2013, 2016, 2019). U gudub Tabka Horumarinta -> Visual Basic (Beddel ahaan isticmaal gaaban Alt+F11). Tafatiraha VB, dhagsii Geli -> Module oo dheji koodka hoose
> Tixgeli habka hoose ee muujinaya noocyada kala duwan ee caddaynta>Private Sub arrayExample1() Dim firstQuarter(0 To 2) As String ‘creates array with index 0,1,2 firstQuarter(0) = "Jan" firstQuarter(1) = "Feb" firstQuarter(2) = "Mar" MsgBox "First Quarter in calendar " & " " & firstQuarter(0) & " " & firstQuarter(1) & " " & firstQuarter(2) End Sub Private Sub arrayExample2() Dim secondQuarter(2) As String ‘creates array with index 0,1,2 secondQuarter(0) = "April" secondQuarter(1) = "May" secondQuarter(2) = "June" MsgBox "Second Quarter in calendar " & " " & secondQuarter(0) & " " & secondQuarter(1) & " " & secondQuarter(2) End Sub Private Sub arrayExample3() Dim thirdQuarter(13 To 15) As String ‘creates array with index 13,14,15 thirdQuarter(13) = "July" thirdQuarter(14) = "Aug" thirdQuarter(15) = "Sep" MsgBox "Third Quarter in calendar " & " " & thirdQuarter(13) & " " & thirdQuarter(14) & " " & thirdQuarter(15) End Sub
F5 ku dhufo ama taabo badhanka run ee aaladda Si loo fuliyo koodka.
>![](/wp-content/uploads/guides/4rcibbktya-1.png)
Isbeddel joogto ah Vs Array Variable
Waxaynu hadda ognahay sida ay u shaqayso arraray hal-cabbir ah. Haddaba aan qaadanno daqiiqad si aan u fahanno sababta ay arradyadu aad muhiim ugu yihiinprogramming languages.
Ka soo qaad inaad u baahan tahay inaad gasho mushaharka 5 shaqaale ah. Si taas loo gaaro iyadoo la adeegsanayo doorsoome caadi ah, waxaad u baahan tahay inaad abuurto doorsoome 5 ah.
Public Sub RegularVariable() Dim shet As Worksheet Set shet = ThisWorkbook.Worksheets("Sheet1") ' Declare variable for each student Dim Emp1 As String Dim Emp2 As String Dim Emp3 As String Dim Emp4 As String Dim Emp5 As String ' Read student marks from cell Emp1 = shet.Range("A" & 2).Value Emp2 = shet.Range("A" & 3).Value Emp3 = shet.Range("A" & 4).Value Emp4 = shet.Range("A" & 5).Value Emp5 = shet.Range("A" & 6).Value ' Print student marks Debug.Print "Emp Name" Debug.Print Emp1 Debug.Print Emp2 Debug.Print Emp3 Debug.Print Emp4 Debug.Print Emp5 End Sub
Hadda aan dhisno kood isku mid ah annaga oo adeegsanayna doorsoome Array.
Option Explicit Public Sub ArrayVarible() Dim shet As Worksheet Set shet = ThisWorkbook.Worksheets("Sheet1") Dim Employee(1 To 6) As String Dim i As Integer For i = 1 To 6 Employee(i) = shet.Range("A" & i).Value Debug.Print Employee(i) Next i End Sub
Halkan, Waxaan hadda isticmaalnay hal doorsoome kala duwan oo kaydin doona dhammaan magacyada shaqaalaha. Ka soo qaad inaad u baahan tahay inaad ku darto 100 magacyo shaqaale oo kale ah markaa waxaad u baahan tahay inaad bedesho cabbirka arayga oo aadan samayn doorsoome cusub.
la fahmi karo lana akhrin karo.Array Laba-cabbir ah
> 2-cabbir ah waxa uu leeyahay 2 tusmooyin - tusmada koowaad waxa ay meteli doontaa safafka, tusmada 2aadna waxa ay meteli doontaa tiirka. Waxay leedahay safaf badan iyo tiirar waxaana badanaa lagu matalaa qaab miis.>To LastIndex, FirstIndex To LastIndex) Sida DataType. >>
Ka fiirso tusaale kaydinta buundooyinka 2 arday oo lagu helay 3 maado. Markaa waxaanu samayn doonaa array 2-cabbir ah oo qaadanaya 2 saf iyo 3 tiir.
Waxa aanu ka bilaabi doonaa safafka 1 ilaa safka 2 iyo tiirka 1 ilaa tiirka 3.
Sub Twodim() Dim totalMarks(1 To 2, 1 To 3) As Integer totalMarks(1, 1) = 23 totalMarks(2, 1) = 34 totalMarks(1, 2) = 33 totalMarks(2, 2) = 55 totalMarks(1, 3) = 45 totalMarks(2, 3) = 44 Msgbox “Total Marks in Row 2 and column 2 is “ &totalMarks(2,2) Msgbox “Total Marks in Row 1 and column 3 is “ &totalMarks(1,3) End Sub
F5 ku dhufo ama Riix badhanka orodka ee aaladda si aad ufuliso koodka
Safka 2aad iyo tiirka 2 >
Safka 1 iyo Tiirka 3 >
>![](/wp-content/uploads/guides/4rcibbktya-3.png)
Arrays go'an ayaa badanaa la isticmaalaa marka aad hubiso cabbirka cabbirka. Tusaale ahaan, Tirada maalmaha todobaadka, waxaad samayn kartaa array leh xidhid hoose oo 0 ah iyo xidhidh sare 6 oo hubso inaadan waligaa beddeli doonin cabbirkiisa.
> 9> Arrays DynamicArrays Firfircooni waxa ay noo ogolaataa in aanu wax ka bedelno shaxanka wakhtiga orodka Kuwani waa faa'iidooyin marka aadan hubin cabbirka seexanka. Ka soo qaad gelitaanka kulliyadda, waxaa laga yaabaa inaadan hubin inta arday ee dhab ahaantii heli doonta gelitaanka, markaa ma go'aamin kartid cabbirka wakhtiga naqshadaynta ama wakhtiga caddaynta.
Ku dhawaaqida array Dynamic waxay la mid tahay Static diyaarsan qawl madhan.
Shaqalaha Dim REDIM , waxaan u baahanahay in aan ogaano in xuduudka hoose aan la bedeli karin, kaliya waxaan bedeli karnaa xadka sare ee array." beddelo cabbirka array-ga inta lagu jiro runtime, markaa waxaan isticmaali karnaa bayaanka ReDim mar kasta oo aan u baahanno inaan kordhinno isku-xidhka isku xidhka. Aan isku dayno inaan kordhino cabbirka diyaarinta hal mar oo kale oo aan ku darno mid cusubMagaca ardayga.
Sub RedimExample() Dim dynArray() As String Dim curdate As Date curdate = Now Dim size As Integer ReDim dynArray(2) dynArray(0) = "John" dynArray(1) = "Tom" dynArray(2) = "Tonny" MsgBox "Students Enrolled untill " & curdate & " are " & dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2) ReDim dynArray(3) ‘ Redim will reinitialise the array and destroy the old values dynArray(3) = "John" MsgBox "Students Enrolled untill " & curdate & " are " & dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2) & " , " & dynArray(3) End Sub
>
Waxaad arki lahayd in natiijadu aanay ka soo bixin magacyada ardaydii hore loogu daray, waxay siinaysaa wax aan waxba tarayn. Taasi waa sababta oo ah bayaanka Redim wuxuu abuuri doonaa hannaan cusub oo leh cabbir cusub wuxuuna burburin doonaa qiyamkii hore.
Kaydinta ReDim
oo markaa badinaysa cabbirka shaxankaAynu dib u qorno koodka sare annagoo adeegsanayna ReDim Preserve
Sub preserveExample() Dim dynArray() As String Dim curdate As Date curdate = Now Dim size As Integer ReDim dynArray(2) dynArray(0) = "John" dynArray(1) = "Tom" dynArray(2) = "Tonny" MsgBox "Students Enrolled untill " & curdate & " are " & dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2) ReDim preserve dynArray(3) ‘ Redim preserve will retain the old values dynArray(3) = "John" MsgBox "Students Enrolled untill " & curdate & " are " & dynArray(0) & ", " & dynArray(1) & ", " & dynArray(2) & " , " & dynArray(3) End Sub
> >
Variant Array
Ilaa hadda waxaan aragnay array aqbalaya isla nooca qiyamka. Hadda aynu ku dhawaaqno array sida kala duwan oo aynu u kaydinno noocyada kala duwan ee xogta sida String, Date, Long, Integer hal array.
Tusaale:
Sub arrayVariant() Dim arrayData(3) As Variant arrayData(0) = "Vikram Vikrant" arrayData(1) = 411234567890# arrayData(2) = 38 arrayData(3) = "06-09-1972" MsgBox "Details of person " & arrayData(0) & " is " & " Phone No " & arrayData(1) & " ,Id " & arrayData(2) & " ,DOB " & arrayData(3) End Sub
19>
Hababka Array VBA
Waxaa jira dhowr habab oo ka mid ah diyaarinta VBA-da kuwaas oo naga caawin doona inaan qabanno hawlo kala duwan, sida hoos ku xusan. > 3>
Sl. Maya | Magaca | Syntax | Sharaxaad |
---|---|---|---|
1 | Array2 | >Array> Tirtir > > Tirtir arrayname Waxa loo adeegsaday dib-u-soo-celinta cabbirka cabbirka ee go'an oo u xoreeya xusuusta Dynamicarray. | |
3 | IsArray | IsArray doorsoomuhu waa habayn> Soo celisa rukunka ugu hooseeya ee habaynta . | > Kala qaybi (muujin, [ xad, [ xad, [ barbar dhig ]]]) > 26>Waxay u qaybisaa xadhigga dhawr xargo-hoosaad oo soo celisa array ku salaysan eber. 7 | >>Ku biir >>>Ku biir > | Ku biir . | >
8 | >> Shaandhayso > | Sandhayso (sourcearray, match, [ ka dar, [ barbar dhig ]]) | Filter-ku waxa uu noo ogolaanayaa in aanu ka baadho kuwa la cayimay oo ka mid ah shaxda. | >
Aynu mid walba si faahfaahsan uga wada hadalno tusaale.
# 1 Marka aad rabto in aad beddesho doorsoomayaasha kala duwanaanshiyaha caadiga ah una beddelo array, waxaan u baahannahay in aan isticmaalno ARRAY sida ku cad tusaalaha hoose.
. Qiimayaashan waxa loo qoondeeyay sidii qayb ka mid ah shaxda.Sub variantArray() Dim varData As Variant varData = Array("Mon Bel", "+61 112334123", 567, "06-09-1972") MsgBox "Details of person " & varData(0) & " is " & " Phone No " & varData(1) & " ,Id " & varData(2) & " ,DOB " & varData(3) End Sub
Waa inaad aqoonsataa doorsoomiyaha habaysan adigoo isticmaalaya index,sidaa darteed tusaalaha kore, qiyamka waxaa dib loogu soo ceshay sida varData(0) varData(2) varData(3)
#2) Tirtir
Shaqadani waxay tirtiri doontaa dhammaan qiimayaasha la geliyo qaabaynta cabbirka go'an oo u xorayn doonta booska xusuusta ee hannaan firfircoon.
Syntax: Tirtir arrayname
>Erase waxa uu leeyahay hab-dhaqan kala duwan noocyada xogta sida la bixiyay Hoos.
>- >
- Tiro go'an: Dhammaan qiimayaasha dib ayaa loo dejiyay eber 2>Dhammaan qiyamka waxa dib loogu dejiyay dhererka eber. >
- > Array fir fircoon: Waxay xoraynaysaa xusuusta ay adeegsatay hannaanku.
Tusaale :
Sub eraseExample() Dim NumArray(3) As Integer Dim decArray(2) As Double Dim strArray(2) As String NumArray(0) = 12345 decArray(1) = 34.5 strArray(1) = "Erase Function" Dim DynaArray() ReDim DynaArray(3) MsgBox " Values before Erase " & (NumArray(0)) & "," & (decArray(1)) & " , " & (strArray(1)) Erase NumArray Erase decArray Erase strArray Erase DynaArray ' Free the memory ' All values are erased. MsgBox " Values after Erase " & NumArray(0) & "," & decArray(1) & " , " & strArray(1) End Sub
Natiijooyinka ka hor inta aanad isticmaalin shaqada Tirtirka
>>
Syntax : IsArray (variablename)
Tusaale:
Sub isArrayTest() Dim arr1, arr2 As Variant arr1 = Array("Jan", "Feb", "Mar") arr2 = "12345" MsgBox ("Is arr1 an Array : " & IsArray(arr1)) MsgBox ("Is arr2 an Array : " & IsArray(arr2)) End
Natiijadii ka timid Msgbox-kii ugu horreeyay
# 4 [Dimension] )
ArrayName waa magaca shaxanka
Qayb-ku-beeggu waa qiimaha is-dhex-galka ikhtiyaariga ah, haddii shaxdu leedahay cabbirro badan, markaa waxaad cayimi kartaacabbirka aad rabto in aad go'aamiso Lbound.
Tusaale:
Sub lboundTest() Dim Result1, Result2, Result3 Dim ArrayValue(1 To 10, 5 To 15, 10 To 20) ' Declare array variables. Dim Arraywithoutlbound(10) Result1 = LBound(ArrayValue, 1) ' Returns 1. Result2 = LBound(ArrayValue, 3) ' Returns 10. Result3 = LBound(Arraywithoutlbound) MsgBox "Lowest subscript in first array " & Result1 & " lowest subscript in 3rd array " & Result2 & " Lowest subscript in Arraywithoutlbound " & Result3 End Sub
#5) Ubound
Waxay soo celisa rukunka sare ee shaxanka lagu tilmaamay dood ahaan shaqada Ubound.
Syntax: UBound( ArrayName, [Dimension] )
ArrayName waa magaca array.
Cabirku waa qiimaha isugaynta ikhtiyaariga ah, haddii shaxdu leedahay cabbiro badan, markaa waxaad qeexi kartaa cabbirka aad rabto inaad go'aamiso Ubound.
>> Tusaale:
Sub UboundTest() Dim Result1, Result2, Result3 Dim ArrayValue(1 To 10, 5 To 15, 10 To 20) ' Declare array variables. Dim ArraywithoutUbound(10) Result1 = UBound(ArrayValue, 1) Result2 = UBound(ArrayValue, 3) Result3 = UBound(ArraywithoutUbound) MsgBox "Lowest subscript in first array " & Result1 & " lowest subscript in 3rd array " & Result2 & " Lowest subscript in Arraywithoutlbound " & Result3 End Sub
3>> #6 1>Syntax: Kala qaybsanaan (muujin, [ delimiter, [ xad, [ barbar dhig]]])
- Muujinta: Kani waa xadhigga oo dhan ee loo isticmaali doono soo saar xadhig-hoosaadyo >
- > Xad-beeye: Iyada oo la adeegsanayo xadaynta la cayimay, xadhig-hoosaadyada ayaa la soo saari doonaa. Haddii aan tan la xusin, booska waxaa loo tixgelinayaa inuu yahay xad-dhaafiye.
- Limit: Tirada xargaha hoosaadka ee la soo celinayo. >
- >Is barbar dhig: Kadib Substring-ka ayaa la soo saaray, waxaad isticmaali kartaa isbarbardhigga kala duwan si aad u tijaabiso natiijada >
Tusaale: Tusaalaha hoose, waxaan u isticmaaleynaa xaddidaad sida - iyo xaddid sida 3.
Hadaba shaqada kala qaybsanaantu waxay u kala saari doontaa xadhigga oo dhan oo u kala qaybin doona xudduudaha ku salaysan xadeeyaha. Laakiin waxaan sidoo kale soo sheegnay xadka sida 3 sidaas darteed xargaha-hoosaadka lama sameyn doono xadka ka dib 3. Sidaas awgeed xaddidaadda ugu dambeysa -waa la boodi doonaa.
Sub splitExample() Dim MyString As String Dim Result() As String Dim DisplayText As String MyString = "This is the example for-VBA-Split-Function" Result = Split(MyString, "-",3) MsgBox Result(0) & vbNewLine & Result(1) & vbNewLine & Result(2) & vbNewLine & Result(3) End Sub
#7) Ku biir
Tani waa uun soo noqoshada kala qaybsanaanta, Join wuxuu abuuri doonaa hal xadhig isagoo isku daraya dhawr xadhig hoosaad.
Syntax: Ku biir 3>
Xiddeeye: Xidheeyaha la cayimay ayaa lagu dari doonaa xadhig kasta ka bacdi inta lagu biirayo ku biiray iyo \ la dhex dhigo kelmad kasta, sidaan soo sheegnay \ oo ah xad-dhaafiye
# 8 ka raadi ciyaar cayiman oo ka mid ah shaxanka. Iyada oo ku saleysan shuruudaha shaandhaynta, qaybta hoose ee xayndaabka xargaha waa la soo celin doonaa Tusaale: >
Sub filterExample() Dim Mystring As Variant Mystring = Array("Software Testing", "Testing help", "Software help") filterString = Filter(Mystring, "help") MsgBox "Found " & UBound(Mystring) - LBound(Mystring) + 1 & " words matching the criteria " End Sub
Tusaalahani waxa uu ka raadin doonaa ereyga "caawin" dhammaan xarkaha habaynta iyada oo la isticmaalayo shaqada filter.
Su'aalaha Inta Badan La Isweydiiyo
Q #1) Sida loo helo dhererka diyaarinta VBA? > array, waxaan isticmaalnaa shaqada Ubound. Shaqadani waxay ina siin doontaa rukunka sare ee array cayiman.
Q #2) Sida loogu dhawaaqo array gudaha VBA? >
>Jawaab: Hal- qaabaynta cabbirka waxa lagu dhawaaqay sida hoos ku cad. >
>Dim arrayname (hoose ilaa UpperBound) Sida DataType
>Tusaale: Dim Myarray (0 ilaa 2) Isku-darka 3>
Laba-cabbir