Advertisement
YasserKhalil2019

T4190_Fill Invoice Get Count SUM Unique Items

Oct 23rd, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.96 KB | None | 0 0
  1. https://excel-egy.com/forum/t4190
  2. ---------------------------------
  3.  
  4. Sub Fill_Invoice_Get_Count_SUM_Unique_Items()
  5. Dim a, e, x, ws As Worksheet, wTot As Double, dTot As Double, i As Long, j As Long, lr As Long
  6. Const sInvoice As String = "Invoice"
  7.  
  8. Application.ScreenUpdating = False
  9. Set ws = ThisWorkbook.Worksheets(1)
  10. a = ws.Range("A2:A" & ws.Cells(Rows.Count, 1).End(xlUp).Row).Value
  11.  
  12. With CreateObject("Scripting.Dictionary")
  13. .CompareMode = 1
  14.  
  15. For Each e In a
  16. If e <> "" Then
  17. For Each x In Split(e, "|")
  18. .item(Trim(x)) = .item(Trim(x)) + 1
  19. Next x
  20. End If
  21. Next e
  22.  
  23. a = Application.Transpose(Array(.Keys, .Items))
  24. ReDim Preserve a(1 To UBound(a, 1), 1 To 3)
  25.  
  26. For i = LBound(a) To UBound(a)
  27. a(i, 3) = Application.Round(Application.SumIf(ws.Columns(1), a(i, 1), ws.Columns(2)), 3)
  28. Next i
  29.  
  30. ReDim b(1 To UBound(a, 1) * 2, 1 To 4)
  31. i = 0
  32.  
  33. For j = LBound(b) To UBound(b) Step 2
  34. i = i + 1
  35. b(j, 1) = "*"
  36. b(j, 2) = a(i, 1) & " عدد " & a(i, 2) & " عميل"
  37. b(j, 3) = GetDecimal(a(i, 3))
  38. b(j, 4) = Int(a(i, 3))
  39. Next j
  40. End With
  41.  
  42. Application.DisplayAlerts = False
  43. If Evaluate("ISREF('" & sInvoice & "'!A1)") Then ThisWorkbook.Worksheets(sInvoice).Delete
  44. Application.DisplayAlerts = True
  45. ThisWorkbook.Worksheets("Template").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  46. ActiveSheet.Name = sInvoice
  47.  
  48. With ThisWorkbook.Worksheets(sInvoice)
  49. .Rows(14).Resize(UBound(b) - 2).Insert
  50. .Range("B13").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  51. .Range("B7").Value = "الكويت في : " & Format(Date, "yyyy/mm/dd")
  52. lr = .Cells(Rows.Count, 2).End(xlUp).Row - 1
  53.  
  54. With .Range("B" & lr + 1)
  55. .Offset(, 1).Formula = "=MOD(SUM(D13:D" & lr & "),1000)"
  56. .Offset(, 2).Formula = "=SUM(E13:E" & lr & ")+QUOTIENT(SUM(D13:D" & lr & "),1000)"
  57.  
  58. dTot = .Offset(, 1).Value: wTot = .Offset(, 2).Value
  59. .Value = "الإجمالي: مبلغ وقدره / " & Replace(Ar_WriteDownNumber(CStr(Int(wTot)), "دينار"), "و ", "و") & IIf(dTot = 0, " فقط لا غير", " & " & dTot & " فلس فقط لا غير")
  60. End With
  61. End With
  62. Application.ScreenUpdating = True
  63. End Sub
  64.  
  65. Function GetDecimal(ByVal num As Double) As String
  66. GetDecimal = Int((num - (Abs(num) / num) * Int(Abs(num))) * 1000)
  67. End Function
  68.  
  69. '----------------------------------------------------------------
  70.  
  71. Public Function Ar_WriteDownNumber(Number_Value As String, Optional Main_Currency As String, Optional Small_Currency As String, Optional Main_To_Small_Factor As Integer)
  72. Dim myNumber, myFractions, wordFraction, pr, hu, th, prTh, huTh, prMi, huMi, hu1, pr2, l, thu_Text As String, mil_Text As String
  73.  
  74. If Val(Main_To_Small_Factor) = 0 Then Main_To_Small_Factor = 100
  75.  
  76. If Small_Currency = "" Then
  77. If Main_To_Small_Factor = 100 Then
  78. Small_Currency = " جزء من مائة"
  79. Else
  80. Small_Currency = " جزء من ألف"
  81. End If
  82. End If
  83.  
  84. myNumber = Abs(Number_Value)
  85. myNumber = Int(myNumber)
  86.  
  87. If InStr(Number_Value, ".") > 0 Then
  88. myFractions = Mid(Number_Value, InStr(Number_Value, ".") + 1, 3)
  89. End If
  90.  
  91. l = Len(myNumber)
  92. pr = Right(myNumber, 2)
  93. Ar_WriteDownNumber = MyPrimary(pr)
  94.  
  95. If l > 2 Then
  96. hu = Right(Left(myNumber, l - 2), 1)
  97.  
  98. If Val(hu) <> 0 Then
  99. If Ar_WriteDownNumber <> 0 Then
  100. Ar_WriteDownNumber = MyHundreds(hu) & " و " & Ar_WriteDownNumber
  101. Else
  102. Ar_WriteDownNumber = MyHundreds(hu)
  103. End If
  104. End If
  105. Else
  106. GoTo 1
  107. End If
  108.  
  109. If l > 3 Then
  110. th = Right(Left(myNumber, l - 3), 2)
  111.  
  112. If Val(th) <> 0 Then
  113. thu_Text = ""
  114.  
  115. If Ar_WriteDownNumber <> 0 Then
  116. Ar_WriteDownNumber = MyThousand(th) & " و " & Ar_WriteDownNumber
  117. Else
  118. Ar_WriteDownNumber = MyThousand(th)
  119. End If
  120. Else
  121. thu_Text = " ألف"
  122. End If
  123. Else
  124. GoTo 1
  125. End If
  126.  
  127. If l > 5 Then
  128. huTh = Right(Left(myNumber, l - 5), 1)
  129.  
  130. If Val(huTh) <> 0 Then
  131. If Ar_WriteDownNumber <> 0 Then
  132. Ar_WriteDownNumber = MyHundreds(huTh) & thu_Text & " و " & Ar_WriteDownNumber
  133. Else
  134. Ar_WriteDownNumber = MyHundreds(huTh) & thu_Text
  135. End If
  136. End If
  137. Else
  138. GoTo 1
  139. End If
  140.  
  141. If l > 6 Then
  142. prTh = Right(Left(myNumber, l - 6), 2)
  143.  
  144. If Val(prTh) <> 0 Then
  145. mil_Text = ""
  146. If Ar_WriteDownNumber <> 0 Then
  147. Ar_WriteDownNumber = MillionPrimary(prTh) & " و " & Ar_WriteDownNumber
  148. Else
  149. Ar_WriteDownNumber = MillionPrimary(prTh) & mil_Text
  150. End If
  151. Else
  152. mil_Text = " مليون"
  153. End If
  154. Else
  155. GoTo 1
  156. End If
  157.  
  158. If l > 8 Then
  159. huMi = Right(Left(myNumber, l - 8), 1)
  160.  
  161. If Ar_WriteDownNumber <> 0 Then
  162. Ar_WriteDownNumber = MyHundreds(huMi) & mil_Text & " و " & Ar_WriteDownNumber
  163. Else
  164. Ar_WriteDownNumber = MyHundreds(huMi) & mil_Text
  165. End If
  166. End If
  167.  
  168. If l > 9 Then Ar_WriteDownNumber = myNumber
  169.  
  170. 1:
  171. If Len(Trim(Ar_WriteDownNumber)) > 0 Then
  172. Ar_WriteDownNumber = Ar_WriteDownNumber & " " & Main_Currency
  173. Else
  174. Ar_WriteDownNumber = ""
  175. End If
  176.  
  177. If Len(myFractions) < 2 Then myFractions = myFractions + "0"
  178. If Len(myFractions) < 3 Then myFractions = myFractions + "0"
  179. If Val(myFractions) = 0 Then Exit Function
  180.  
  181. If Main_To_Small_Factor = 100 Then
  182. pr2 = Left(myFractions, 2)
  183. Else
  184. pr2 = Mid(myFractions, 2, 2)
  185. End If
  186.  
  187. wordFraction = MyPrimary(pr2)
  188.  
  189. If Main_To_Small_Factor > 100 Then
  190. hu1 = Left(myFractions, 1)
  191.  
  192. If Val(hu1) <> 0 Then
  193. If wordFraction <> 0 Then
  194. wordFraction = MyHundreds(hu1) & " و " & wordFraction
  195. Else
  196. wordFraction = MyHundreds(hu1)
  197. End If
  198. End If
  199. Else
  200. GoTo 2
  201. End If
  202.  
  203. 2:
  204. If Main_Currency <> "" Then
  205. If Len(Trim(Ar_WriteDownNumber)) > 0 Then
  206.  
  207. Ar_WriteDownNumber = Ar_WriteDownNumber & " و " & wordFraction & " " & Small_Currency
  208. Else
  209. Ar_WriteDownNumber = wordFraction & " " & Small_Currency
  210. End If
  211. Else
  212. If Len(Trim(Ar_WriteDownNumber)) > 0 Then
  213.  
  214. If Main_To_Small_Factor = 100 Then
  215. Small_Currency = " جزء من مائة"
  216. Else
  217. Small_Currency = " جزء من ألف"
  218. End If
  219.  
  220. Ar_WriteDownNumber = Ar_WriteDownNumber & " فاصل " & wordFraction
  221. Else
  222. Ar_WriteDownNumber = wordFraction & " " & Small_Currency
  223. End If
  224. End If
  225. End Function
  226.  
  227. Private Function MyPrimary(j)
  228. Dim myText1, myText2, k
  229.  
  230. k = Right(j, 1): j = Val(j)
  231.  
  232. If j < 20 Then
  233. MyPrimary = Choose(j, "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة", "عشرة", "إحدى عشر", "اثنا عشر", "ثلاثة عشر", "أربعة عشر", "خمسة عشر", "ستة عشر", "سبعة عشر", "ثمانية عشر", "تسعة عشر")
  234. Else
  235. myText1 = Choose(Val(k), "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة")
  236. myText2 = Choose(Int((j - k) / 10) - 1, "عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون")
  237.  
  238. If Not IsNull(myText1) Then
  239. MyPrimary = myText1 & " و " & myText2
  240. Else
  241. MyPrimary = myText2
  242. End If
  243. End If
  244. End Function
  245.  
  246. Private Function MyHundreds(j)
  247. j = Val(j)
  248.  
  249. MyHundreds = Choose(j, "مائة", "مائتان", "ثلاثمائة", "أربعمائة", "خمسمائة", "ستمائة", "سبعمائة", "ثمانمائة", "تسعمائة")
  250. End Function
  251.  
  252. Private Function MyThousand(j)
  253. Dim myText1, myText2, k
  254.  
  255. k = Right(j, 1): j = Val(j)
  256.  
  257. If j < 20 Then
  258. MyThousand = Choose(j, "ألف", "ألفان", "ثلاثة آلاف", "أربعة آلاف", "خمسة آلاف", "ستة آلاف", "سبعة آلاف", "ثمانية آلاف", "تسعة آلاف", "عشرة آلاف", "إحدى عشر ألفاً", "اثنا عشر ألفاً", "ثلاثة عشر ألفاً", "أربعة عشر ألفاً", "خمسة عشر ألفاً", "ستة عشر ألفاً", "سبعة عشر ألفاً", "ثمانية عشر ألفاً", "تسعة عشر ألفاً")
  259. Else
  260. myText1 = Choose(k, "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة")
  261. myText2 = Choose((j - k) / 10 - 1, "عشرون", "ثلاثون", "أربعون", "خمسون", "ستون", "سبعون", "ثمانون", "تسعون")
  262.  
  263. If Not IsNull(myText1) Then
  264. MyThousand = myText1 & " و" & myText2 & " ألف"
  265. Else
  266. MyThousand = myText2 & " ألف"
  267. End If
  268. End If
  269. End Function
  270.  
  271. Private Function MillionPrimary(j)
  272. Dim myText1, myText2, k
  273.  
  274. k = Right(j, 1): j = Val(j)
  275.  
  276. If j < 20 Then
  277. MillionPrimary = Choose(j, "مليون", "مليونان", "ثلاثة ملايين", "أربعة ملايين", "خمسة ملايين", "ستة ملايين", "سبعة ملايين", "ثمانية ملايين", "تسعة ملايين", "عشرة ملايين", "إحدى عشر مليونأ", "اثنا عشر مليون", "ثلاثة عشر مليون", "أربعة عشر مليون", "خمسة عشر مليون", "ستة عشر مليون", "سبعة عشر مليون", "ثمانية عشر مليون", "تسعة عشر مليون")
  278. Else
  279. myText1 = Choose(Val(k), "واحد", "اثنان", "ثلاثة", "أربعة", "خمسة", "ستة", "سبعة", "ثمانية", "تسعة")
  280. myText2 = Choose(Int((j - k) / 10) - 1, "عشرون مليون", "ثلاثون مليون", "أربعون مليون", "خمسون مليون", "ستون مليون", "سبعون مليون", "ثمانون مليون", "تسعون مليون")
  281.  
  282. If Not IsNull(myText1) Then
  283. MillionPrimary = myText1 & " و " & myText2
  284. Else
  285. MillionPrimary = myText2
  286. End If
  287. End If
  288. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement