SoiBien
Thành viên tiêu biểu

- Tham gia
- 3/9/07
- Bài viết
- 734
- Được thích
- 609
có người bạn nhờ mình viết giùm cái function mà khi họ nhập liệu trong cột diễn giải xong thì nó tự gán công thức cho ô bên cạnh
đây là code, các bạn chép nó vào code của Thisworkbook nhé (trong file làm dự tóan của bạn).
Do bạn mình muốn áp dụng trên tất cả các sheets (???) nên mình applied nó vào workbook.
mình kèm luôn file ví dụ
đây là code, các bạn chép nó vào code của Thisworkbook nhé (trong file làm dự tóan của bạn).
Do bạn mình muốn áp dụng trên tất cả các sheets (???) nên mình applied nó vào workbook.
Mã:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Workbook_SheetChange([COLOR=darkblue]ByVal[/COLOR] Sh [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Object[/COLOR], [COLOR=darkblue]ByVal[/COLOR] Target [COLOR=darkblue]As[/COLOR] Range)
chuoichapnhan = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", ".", "(", ")")
[COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] Application.Intersect(Sh.Range("C:C"), Target) [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
Application.EnableEvents = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] cell [COLOR=darkblue]In[/COLOR] Target
[COLOR=darkblue]If[/COLOR] cell.Value = "" [COLOR=darkblue]Then[/COLOR]
cell.Offset(0, 1).Formula = ""
[COLOR=darkblue]Else[/COLOR]
[COLOR=darkblue]If[/COLOR] IsNumeric(cell.Value) = [COLOR=darkblue]False[/COLOR] [COLOR=darkblue]Then[/COLOR]
fmlstr = "="
kytu2 = ""
kytu = ""
bieuthuc = cell.Value
[COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] Len(bieuthuc)
[COLOR=darkblue]For[/COLOR] j = 0 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](chuoichapnhan)
kytu = Mid(bieuthuc, i, 1)
[COLOR=darkblue]If[/COLOR] kytu = chuoichapnhan(j) [COLOR=darkblue]Then[/COLOR]
[COLOR=darkblue]If[/COLOR] kytu = "/" [COLOR=darkblue]Then[/COLOR]
kytu2 = kytu
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
[COLOR=darkblue]Else[/COLOR]
[COLOR=darkblue]If[/COLOR] j <= 9 [COLOR=darkblue]Then[/COLOR] [COLOR=green]' kytu la so[/COLOR]
fmlstr = fmlstr & kytu2 & kytu
[COLOR=darkblue]Else[/COLOR] [COLOR=green]' kytu la cong thuc hoac ()[/COLOR]
fmlstr = fmlstr & kytu
kytu2 = ""
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
MsgBox fmlstr
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] j
[COLOR=darkblue]Next[/COLOR] i
[COLOR=darkblue]If[/COLOR] fmlstr <> "=" [COLOR=darkblue]Then[/COLOR]
cell.Offset(0, 1).Formula = fmlstr
[COLOR=darkblue]Else[/COLOR]
cell.Offset(0, 1).Formula = ""
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
Application.EnableEvents = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
ErrHandler:
Application.EnableEvents = [COLOR=darkblue]True[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
File đính kèm
Lần chỉnh sửa cuối: