VBA algorithm for sorting

%3CLINGO-SUB%20id%3D%22lingo-sub-2535985%22%20slang%3D%22en-US%22%3EVBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2535985%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%20I%20need%20help%20with%20my%20task%20in%20the%20school%2C%20I%20should%20write%20my%20own%20bubble%20for%20sorting%20in%20alphabetic%20order%20and%20not%20use%20build-in%20functions.%20I%20know%20how%20to%20do%20it%20for%20numbers%20but%20I%20have%20no%20idea%20how%20to%20do%20it%20for%20letters.%20I%20am%20talking%20about%20this%20code%20specifically%3A%3C%2FP%3E%3CDIV%3E%3CSTRONG%3ERange(%22A1%3AF20%22).Sort%20Key1%3A%3DRange(%22C2%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3D%20_%3C%2FSTRONG%3E%3CBR%20%2F%3E%3CSTRONG%3ExlGuess%2C%20OrderCustom%3A%3D1%2C%20MatchCase%3A%3DFalse%2C%20Orientation%3A%3DxlTopToBottom%3C%2FSTRONG%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3BThanks%20a%20lot%20for%20any%20help%20or%20hints.%20Have%20a%20good%20day%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2535985%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2536010%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2536010%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1100652%22%20target%3D%22_blank%22%3E%40Chyma%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20bubble%20sort%20algorithm%20for%20text%20values%20is%20exactly%20the%20same%20as%20that%20for%20numeric%20values.%20The%20only%20difference%20is%20the%20data%20type%20of%20the%20variable%20that%20you%20use%20to%20store%20a%20value%20temporarily%20while%20swapping%20two%20values.%20That%20variable%20should%20be%20of%20type%20String%20instead%20of%20Long%20(or%20Double%2C%20or%20whatever%20you%20were%20using).%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2536057%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2536057%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1100652%22%20target%3D%22_blank%22%3E%40Chyma%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20say%20that%20a%20text%20sort%20presents%20some%20additional%20problems%2C%20besides%20just%20changing%20the%20data%20type%20of%20variables.%26nbsp%3B%20(Which%20is%20actually%20optional%2C%20if%20you%20choose%20to%20declare%20variables%20as%20type%20Variant%2C%20explicitly%20or%20by%20default.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%2C%20how%20to%20treat%20upper%20and%20lower%20cases.%26nbsp%3B%20(Hint%3A%20See%20the%20Option%20Compare%20statement.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESecond%2C%20how%20to%20treat%20spaces%20(and%20other%20whitespace%20characters%20like%20tab%20and%20nonbreaking%20spaces%3F)%2C%26nbsp%3B%20if%20you%20are%20sorting%20phrases.%26nbsp%3B%20(Hint%3A%20See%20the%20Replace%20function%20to%20remove%20whitespace%20characters%2C%20if%20desired.)%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESimilarly%2C%20how%20to%20treat%20special%20characters%2C%20notably%20commas%2C%20semicolons%2C%20colons%2C%20dashes%2C%20etc.%26nbsp%3B%20For%20example%2C%20sort%20%22A-c-b%22%20and%20%22Abc%22%2C%20noting%20that%20%22-%22%20%26lt%3B%20%22b%22%20is%20True.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBottom%20line%3A%26nbsp%3B%20Provide%20an%20example%20with%20some%20representative%20data%20and%20the%20sorted%20result%20that%20you%20want.%26nbsp%3B%20Or....%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ERe%3A%20%60%60I%20am%20talking%20about%20this%20code%20specifically%3A%20Range(%22A1%3AF20%22).Sort%20Key1%3A%3DRange(%22C2%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3DxlGuess%2C%20OrderCustom%3A%3D1%2C%20MatchCase%3A%3DFalse%2C%20%5B....%5D%60%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20mean%20that%20you%20want%20the%20result%20of%20your%20sort%20to%20be%20the%20same%20as%20the%20result%20of%20the%20range.Sort%20method%20with%20those%20options%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(I'm%20surprised%20that%20you%20want%20Header%3A%3DxlGuess%20instead%20of%20xlNo.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFinally%2C%20if%20you%20are%20looking%20for%20anything%20more%20specific%2C%20show%20us%20your%20implementation%20of%20a%20%3CU%3E%3CEM%3Enumeric%3C%2FEM%3E%3C%2FU%3E%20%22bubble%20sort%22%20--%20or%20at%20least%20the%20algorithm.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20bubble%20sort%20%3CEM%3Eper%20se%3C%2FEM%3E%20is%20inefficient.%26nbsp%3B%20Given%20the%20choice%2C%20I%20would%20use%20a%20selection%20sort.%26nbsp%3B%20But%20it%20depends%20on%20the%20specifics%20of%20the%20assignment.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2536061%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2536061%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F146717%22%20target%3D%22_blank%22%3E%40Joe%20User%3C%2FA%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20was%20the%20task%3A%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E***Grading%20journal***%3C%2FP%3E%3CP%3E(Name%2C%20last%20name%2C%20grade1%2C%20grade2%20grade3)%3C%2FP%3E%3CP%3EI%20want%20You%20to%20write%20me%20a%20project%20that%20is%20the%20grading%20journal.%20The%20program%20will%20read%20the%20given%20file%20%22student.txt%22%20and%20put%20it%20in%20the%20table%2Farray.%20There%20will%20be%203%20buttons%3A%201.%20sorting%20button%20(any%20known%20for%20labratory)%3CBR%20%2F%3E2.%20adding%20new%20student%20and%20his%20grades%3CBR%20%2F%3E3.%20writing%20the%20table%20to%20new%20file%20%22sorted%20Student.txt%3CBR%20%2F%3E%3CBR%20%2F%3E-%20reading%20list%20from%20the%20.txt%20file%3CBR%20%2F%3E-%20putting%20them%20in%20table%3CBR%20%2F%3E-%20sort%20the%20table%20by%20last%20name%3CBR%20%2F%3E-%20button%20to%20add%20new%20name%20and%20grade%3CBR%20%2F%3E-%20when%20finnished%20working%20with%20excel%20write%20it%20down%20to%20.txt%20again%3C%2FP%3E%3CP%3E%3CBR%20%2F%3Ethis%20is%20my%20solution%20of%20the%20step%20sort%20the%20table%20by%20last%20name%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CEM%3ESub%20Button4_Click()%20Range(%22A1%3AF20%22).Sort%20Key1%3A%3DRange(%22C2%22)%2C%20Order1%3A%3DxlAscending%2C%20Header%3A%3D%20_%20xlGuess%2C%20OrderCustom%3A%3D1%2C%20MatchCase%3A%3DFalse%2C%20Orientation%3A%3DxlTopToBottom%20End%20Sub%3C%2FEM%3E%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Ebut%20teacher%20wants%20from%20me%20to%20use%20not%20build-in%20function%20and%20instead%20of%20that%20to%20do%20my%20own%20bubble%20sort%20by%20changing%20latters%20to%20ASCII%20but%26nbsp%3B%20I%20dont%20really%20know%20how%20to%20match%20particular%20letter%20to%20the%20given%20number%20from%20ASCII%20table%2C%20and%20mainly%20if%20i%20will%20sort%20it%20by%20the%20last%20names%20it%20will%20mix%20all%20table%2C(%20wrong%20last%20name%20will%20match%20wrong%20first%20name)%20i%20have%20to%20lock%20somehow%20whole%20row%20so%20it%20will%20be%20sorted%20with%20first%20name%20and%20grades%20and%20so%20on.%20So%20here%20i%20struggle%20%2C%20the%20rest%20i%20know%20how%20to%20do%20more%20or%20less%2C%20so%20if%20you%20would%20have%20any%20idea%20i%20would%20really%20appreciate%20that%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2536073%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2536073%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1100652%22%20target%3D%22_blank%22%3E%40Chyma%3C%2FA%3E%26nbsp%3B%20wrote%3A%20%60%60i%20have%20to%20lock%20somehow%20whole%20row%20so%20it%20will%20be%20sorted%20with%20first%20name%20and%20grades%20and%20so%20on%60%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20organization%20of%20your%20data%20is%20not%20clear%20to%20me.%26nbsp%3B%20I%20suggest%20(again)%20that%20you%20provide%20a%20concrete%20example%20that%20demonstrates%20what%20does%20wrong%20when%20using%20the%20range.Sort%20method.%26nbsp%3B%20Ideally%2C%20attach%20an%20Excel%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E(Edit%3A%20Oh%2C%20I%20see%20that%20you%20sent%20it%20to%20me%20in%20PM%20because%20you%20do%20not%20want%20to%20make%20it%20public.%26nbsp%3B%20I%20will%20look%20to%20see%20if%20that%20changes%20anything%20that%20I%20wrote%20below.%26nbsp%3B%20I%20do%20not%20think%20so.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20seems%20that%20the%20only%20problem%20that%20you%20need%20help%20with%20is%2C%20for%20example%3A%26nbsp%3B%20having%20decided%20that%20C5%20%26lt%3B%20C2%2C%20you%20want%20to%20exchange%20%3CU%3E%3CEM%3Eall%20of%3C%2FEM%3E%3C%2FU%3E%20row%202%20and%20row%205%2C%20at%20least%20columns%20A%20through%20F.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOne%20way%20to%20do%20that%20(but%20not%20necessarily%20the%20best%20way)%20is%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20i%20As%20Long%2C%20j%20As%20Long%3CBR%20%2F%3EDim%20ti%20As%20Variant%2C%20tj%20As%20Variant%3CBR%20%2F%3EDim%20ri%20As%20Range%2C%20rj%20As%20Range%3CBR%20%2F%3Ei%20%3D%202%3A%20j%20%3D%205%3CBR%20%2F%3ESet%20ri%20%3D%20Range(%22a%22%20%26amp%3B%20i%20%26amp%3B%20%22%3Af%22%20%26amp%3B%20i)%3CBR%20%2F%3ESet%20rj%20%3D%20Range(%22a%22%20%26amp%3B%20j%20%26amp%3B%20%22%3Af%22%20%26amp%3B%20j)%3CBR%20%2F%3Eti%20%3D%20ri%3CBR%20%2F%3Etj%20%3D%20rj%3CBR%20%2F%3Eri%20%3D%20tj%3CBR%20%2F%3Erj%20%3D%20ti%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2536984%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20algorithm%20for%20sorting%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2536984%22%20slang%3D%22en-US%22%3E%3CP%3E(Sorry%2C%20I%20overlooked%20the%20following%20question.%26nbsp%3B%20And%20arrgghh!%26nbsp%3B%20I%20lost%20the%20first%20version%20of%20this%20response%20that%20I%20had%20written%20many%20hours%20ago.%26nbsp%3B%20This%20will%20have%20to%20be%20brief.)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1100652%22%20target%3D%22_blank%22%3E%40Chyma%3C%2FA%3E%26nbsp%3B%20wrote%3A%20%60%60do%20my%20own%20bubble%20sort%20by%20changing%20latters%20to%20ASCII%20but%20I%20dont%20really%20know%20how%20to%20match%20particular%20letter%20to%20the%20given%20number%20from%20ASCII%20table%60%60%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20should%20not%20have%20to%20%22change%20letters%20to%20ASCII%22.%26nbsp%3B%20Since%20you%20use%20LINE%20INPUT%23%20to%20read%20the%20data%20from%20a%20text%20file%2C%20it%20should%20already%20be%20ASCII%20text.%26nbsp%3B%20But%20beware%3A%26nbsp%3B%20some%20of%20the%20data%20might%20be%20stored%20as%20numbers%2C%20not%20text%2C%20if%20Excel%20can%20interpret%20the%20text%20as%20a%20numeric%20form.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESince%20you%20wrote%20previously%20that%20you%20%60%60know%20how%20to%20do%20it%20%5Bsort%5D%20for%20numbers%60%60%2C%20comparing%20text%20is%20not%20much%20different.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20should%20enter%20the%20%3CFONT%20color%3D%22%23FF0000%22%3EOption%20Compare%20Text%3C%2FFONT%3E%20statement%20near%20the%20beginning%20of%20the%20VBA%20module%2C%20in%20order%20to%20sort%20like%20your%20range.Sort%20method.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnd%20for%20efficiency%2C%20you%20should%20copy%20the%20data%20(at%20least%20the%20sort%20column)%20into%20a%20VBA%20array%2C%20especially%20if%20you%20insist%20on%20using%20a%20bubble%20sort.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20that%20end%2C%20you%20should%20not%20assume%20that%20the%20data%20has%20(up%20to)%2020%20rows%2C%20as%20your%20range.Sort%20method%20does%2C%20unless%20that%20is%20part%20of%20the%20assignment.%26nbsp%3B%20Instead%2C%20you%20might%20write%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20v%20As%20Variant%2C%20n%20As%20Long%3C%2FP%3E%3CP%3Ev%20%3D%20%3CFONT%20color%3D%22%23FF0000%22%3ERange(%22c1%22%2C%20Cells(1%2C%20%22c%22).End(xlDown))%3C%2FFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'%20for%20the%20sort%20column%20C%3C%2FP%3E%3CP%3Eor%3C%2FP%3E%3CP%3Ev%20%3D%20%3CFONT%20color%3D%22%23FF0000%22%3ERange(%22a1%22%2C%20Cells(1%2C%20%22f%22).End(xlDown))%3C%2FFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'%20for%20the%20entire%20table%20in%20columns%20A%3AF%3C%2FP%3E%3CP%3En%20%3D%20UBound(v%2C%201)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20assumes%20that%20column%20F%20is%20fully-populated.%26nbsp%3B%20Alternatively%2C%20you%20might%20determine%20the%20last%20row%20number%20as%20follows%2C%20assuming%20column%20C%20has%20the%20last%20names%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20lastRow%20As%20Long%3C%2FP%3E%3CP%3ElastRow%20%3D%20%3CFONT%20color%3D%22%23FF0000%22%3ECells(1%2C%20%22c%22).End(xlDown).Row%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20the%20condition%20to%20determine%20a%20possible%20swap%20is%20the%20same%20for%20text%20as%20it%20is%20for%20numbers%2C%20namely%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20%3CFONT%20color%3D%22%23FF0000%22%3Ev(j%2C1)%20%26gt%3B%20v(i%2C1)%3C%2FFONT%3E%20Then%3C%2FP%3E%3CP%3E%5B....%5D%3C%2FP%3E%3CP%3EEnd%20If%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you%20swap%20Excel%20rows%20as%20I%20demonstrated%20in%20a%20previous%20response%2C%20you%20should%20also%20swap%20the%20VBA%20array%20row.%26nbsp%3B%20If%20you%20have%20only%20the%20sort%20column%20in%20%22v%22%2C%20you%20might%20write%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDim%20t%20As%20Variant%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3Et%20%3D%20v(i%2C1)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3Ev(i%2C1)%20%3D%20v(j%2C1)%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3Ev(j%2C1)%20%3D%20t%20%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlternatively%2C%20if%20you%20copy%20the%20entire%20table%20into%20%22v%22%2C%20you%20can%20swap%20the%20entire%20VBA%20row%20as%20follows.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20k%20%3D%201%20to%206%26nbsp%3B%26nbsp%3B%26nbsp%3B%20'%206%20corresponds%20to%20column%20F%3C%2FP%3E%3CP%3Et%20%3D%20v(i%2Ck)%3C%2FP%3E%3CP%3Ev(i%2Ck)%20%3D%20v(j%2Ck)%3C%2FP%3E%3CP%3Ev(j%2Ck)%20%3D%20t%3C%2FP%3E%3CP%3ENext%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello, I need help with my task in the school, I should write my own bubble for sorting in alphabetic order and not use build-in functions. I know how to do it for numbers but I have no idea how to do it for letters. I am talking about this code specifically:

Range("A1:F20").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

 Thanks a lot for any help or hints. Have a good day 

3 Replies

@Chyma 

The bubble sort algorithm for text values is exactly the same as that for numeric values. The only difference is the data type of the variable that you use to store a value temporarily while swapping two values. That variable should be of type String instead of Long (or Double, or whatever you were using).

@Chyma 

I would say that a text sort presents some additional problems, besides just changing the data type of variables.  (Which is actually optional, if you choose to declare variables as type Variant, explicitly or by default.  But I do not recommend that.)

 

First, how to treat upper and lower cases.  (Hint: See the Option Compare statement.)

 

Second, how to treat spaces (and other whitespace characters like tab and nonbreaking spaces?),  if you are sorting phrases.  (Hint: See the Replace function to remove whitespace characters, if desired.) 

 

Similarly, how to treat special characters, notably commas, semicolons, colons, dashes, etc.  For example, sort "A-c-b" and "Abc", noting that "-" < "b" is True.

 

Bottom line:  Provide an example with some representative data and the sorted result that you want.  Or....

 

Re: ``I am talking about this code specifically: Range("A1:F20").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, [....]``

 

Do you mean that you want the result of your sort to be the same as the result of the range.Sort method with those options?

 

(I'm surprised that you want Header:=xlGuess instead of xlNo.)

 

Finally, if you are looking for anything more specific, show us your implementation of a numeric "bubble sort" -- or at least the algorithm.

 

The bubble sort per se is inefficient.  Given the choice, I would use an exchange sort.  But it depends on the specifics of the assignment.

 

@Joe User  

This was the task:


***Grading journal***

(Name, last name, grade1, grade2 grade3)

I want You to write me a project that is the grading journal. The program will read the given file "student.txt" and put it in the table/array. There will be 3 buttons: 1. sorting button (any known for labratory)
2. adding new student and his grades
3. writing the table to new file "sorted Student.txt

- reading list from the .txt file
- putting them in table
- sort the table by last name
- button to add new name and grade
- when finnished working with excel write it down to .txt again


this is my solution of the step sort the table by last name:

Sub Button4_Click() Range("A1:F20").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom End Sub

but teacher wants from me to use not build-in function and instead of that to do my own bubble sort by changing latters to ASCII but  I dont really know how to match particular letter to the given number from ASCII table, and mainly if i will sort it by the last names it will mix all table,( wrong last name will match wrong first name) i have to lock somehow whole row so it will be sorted with first name and grades and so on. So here i struggle , the rest i know how to do more or less, so if you would have any idea i would really appreciate that