I'm working on a workbook where I insert manually mixed English and Arabic text in the same cell "D" this text contain the section code and unit number in English in addition to some description in Arabic like this for example (NH7B-AV-06A-23 تم الانتهاء من العمل خلال 48 ساعه) in this text (NH7B) is the section code and (AV-06A-23) is the unit number... then in column "C" I have a formula to extract the "section code" (=LEFT(E7,SEARCH("-",E7)-1) and this is ok no problem at all... and in column "E" i have another formula to extract the "unit number (=MID(E70,SEARCH("-",E7)+1,FIND("@",E7)-SEARCH("-",E7)-1) ....by this way and in order to make the formula in "E" run, I have to add @ symbol to my text in column "D" manually and now i need it to be added automatically
One of my friends created a code for me to run it with a button after inserting text to D but it has one problem: it removes the formula in column "E" and extract all the english text from "D" to "E" not only the unit number....
I need your help please to fix this issue in the code or if you have any new suggestions will be appreciated
Sub englishATarabic()
Dim txt As String, L, r, count As Long
For r = 1 To ActiveSheet.Cells(ActiveSheet.Rows.count, "E").End(xlUp).Row
If IsNumeric(ActiveSheet.Range("B" & r).Value) And ActiveSheet.Range("B" & r).Value <> "" And ActiveSheet.Range("E" & r).Value <> "" Then
If InStr(ActiveSheet.Range("E" & r).Text, "@") < 1 Then
count = 0
For L = 1 To Len(ActiveSheet.Range("E" & r).Text)
Debug.Print L, AscW(Mid(ActiveSheet.Range("E" & r).Text, L, 1))
If AscW(Mid(ActiveSheet.Range("E" & r).Text, L, 1)) < 1000 Then count = count + 1 Else Exit For
Next
txt = Trim(Left(ActiveSheet.Range("E" & r).Text, count))
If InStrRev(txt, "-") = Len(txt) Then ActiveSheet.Range("D" & r).Value = Trim(Left(txt, Len(txt) - 1)) Else ActiveSheet.Range("D" & r).Value = txt
End If
End If
Next
End Sub
Your method (of determining the position of the first Arabic character) can be used without VBA in various ways. However, please note that your method will leave a trailing hyphen for the
Unitin your second example mentioned in your comment.To replicate your method, using formulas, assuming you have
365, you could use this function. Note that I had to add a specific step to remove that trailing hyphen (tUnit)(Note that in both the formula and the Power Query methods, we test for an Arabic character by looking for a character code greater than 255. In a more complex scenario, we could test specifically for the arabic character set, but, at present, this comprises
1519characters in11separate groupings, so would add considerable complexity to the formula. However, if applicable to your dataset, you could just test for the range0600-06FF. You'd need to investigate whether that is sufficient.)and fill down. The results
SPILLinto the adjacent columnsThis can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
Data => Get&Transform => from Table/Rangeorfrom within sheetHome => Advanced EditorApplied Stepsto understand the algorithmNote that it is easier to trim that trailing hyphen.
Column1can be deleted from the final table in PQ if that is preferableIf, for some reason, you prefer to use VBA, you can write a UDF that will do the entire splitting task, and then use that function on your worksheet. eg:
The function will return a three element array. Depending on your version of Excel, it may either
SPILLto the three columns, or you may need to array-enter it across the three columns, or use the INDEX function to return each element of the arrayExplanation of the Regex used
^([^-\s])[-\s]+([^\u0600-\u06FF]+)[-\s]+(.)
Options: Case insensitive; ^$ match at line breaks
^([^-\s]*)[^-\s]**-\s[-\s]++-\s([^\u0600-\u06FF]+)[^\u0600-\u06FF]++\u0600\u06FF[-\s]++-\s(.*).**Created with RegexBuddy