You are on page 1of 3
zor Excel VBA Programming Excel VBA Programming - Sting Exercise a Home & Learn Verdien geld terwijl u slaapt, heel gemakkelijk! hhipihwacnomeandleern orgia_sting_eercise hint riai20%4 ioe! VBA Programming - Sting Eercise An Excel VBA String Method Exercise To get some practice with Sting methods, we're going to work through a problem. Try to solve the problem yourself before going through the solution below. OK, here's the problem. Problem ‘Suppose you had a product code on a spreadsheet that looked lke this: PD-23-23-45 However, isin the wrong format. First, you're told to remove all the hyphens. Then, you" told to tun the letters PD in the product code to PDC. So your finished work should look ike this: Ppc232345 ‘The question is, how would you do this with Excel VBA code? IFyou get stuck solving the problem above, then here's the solution, >rekgette Veo30" Excel Lalnrgen mo Solution The first part of the problem, removing the hyphens, is fairly easy just use Replace: Dim ProductCode As String ProductCode = "PD-23.2345" ProductCode = Replace(ProductGod Inbetween the round brackets of Replace we have the text we want to search, which is the variable called ProductCode. After a comma, we have the character we want fo replace, the hyphen. Next, we rave the new character, whichis no tex at all This is done with two double quotes with no space between them, ‘The second part of the problem, adding the °C" after "PD", would be easy too, if VBA had an insert function. Butit doesn't Which makes this part of the problem a lle bit harder. ‘There are a few ways to insert the °C" in the correct place, Well'do it using the Left and Mid string functions. Wellluse Left to grab the first two characters, thon add the "C*. Welllthan use Mid to gat the numbers. To getthe first two characters and add the °C”, the code is this: Dim Letters As String Letters = Left(ProductCode, 2) & ‘Starting from the Ie of ProductCede, we grab two characters: Left(ProductCode, 2) ‘The "C" is added with concatenation: Left(ProductCode, 2) & ‘The new three letter code is then stored in the variable called Letters, To get the numbers, use Mid Dim Numbers As String Numbors = Mid(ProductCode, 3) Mid first needs the sting you'e searching, which is PreductCode for us. Next, we have 3 as the starting point to grab characters. This will grab the characters starting from the 3rd character inthe string. Because we haver't specified an end number, Mid willgrab the rest ofthe characters to the end hitpinwahomesndear.orgla_string_evercise Net rrar20%4 Beal VBA Programming - Sting Exercise of the string, The only thing oft to do is to join the two parts togothor: Dim NewCode As String NewCode = Letters & Numbers MsgBox NewCode This just uses concatenation to join the Letters variable fo the Numbers variable. The finalline uses a message boxto display the results. The whole ofthe code, though, looks lke this: Sub SteinaTeer |) Dem éaoBox Ney When you meet a problem lke the one above, the solution is usually to use one of the Left, Right, or Mid functions (or all of them) to chop the string into pieces and then join them back together again. In the next lesson, we'll move on and tackle programming loops, Enterprise Architect © = © spancystems aufoterprsedrchect ©All course material copyright Ken Carney hitpinwahomesndear.orgla_string_evercise Net Loops in Excel VBA > 28

You might also like