Hỏi về tối ưu code vba (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

nhk007dn

Thành viên chính thức
Tham gia
12/11/12
Bài viết
74
Được thích
7
Em làm 1 user form để nhập số liệu vào các ô cụ thể. Tuy nhiên khi chạy code thấy hơi chậm. với lại nhìn code thấy dài quá. e nghĩ có thể rút ngắn lại hơn nhiều và có thể làm cho nhanh hơn. Nhờ các a trên diễn đàn có thể giúp e tối ưu lại code này để e có thể học hỏi thêm. e cảm ơn.

Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
            Me.txt6 = Range("AK4")
            Me.txt8 = Range("AL4")
            Me.txt10 = Range("AM4")
            Me.txt12 = Range("AN4")
            Me.txt14 = Range("AO4")
            Me.txt16 = Range("AP4")
            Me.txt18 = Range("AQ4")
            Me.txt20 = Range("AR4")
            Me.txt22 = Range("AS4")
            Me.txt25 = Range("AT4")
            Me.txt28 = Range("AU4")
            Me.txt32 = Range("AV4")
            Me.txt36 = Range("AW4")
            Me.txt40 = Range("AX4")
            txt6.Value = Format(txt6.Value, "#,##0.000")
            txt8.Value = Format(txt8.Value, "#,##0.000")
            txt10.Value = Format(txt10.Value, "#,##0.000")
            txt12.Value = Format(txt12.Value, "#,##0.000")
            txt14.Value = Format(txt14.Value, "#,##0.000")
            txt16.Value = Format(txt16.Value, "#,##0.000")
            txt18.Value = Format(txt18.Value, "#,##0.000")
            txt20.Value = Format(txt20.Value, "#,##0.000")
            txt22.Value = Format(txt22.Value, "#,##0.000")
            txt25.Value = Format(txt25.Value, "#,##0.000")
            txt28.Value = Format(txt28.Value, "#,##0.000")
            txt32.Value = Format(txt32.Value, "#,##0.000")
            txt36.Value = Format(txt36.Value, "#,##0.000")
            txt40.Value = Format(txt40.Value, "#,##0.000")
End Sub


Private Sub cmdCapNhat_Click()
Application.ScreenUpdating = False
            Range("AK2") = txtChungLoai
            Range("AK4") = txt6
            Range("AL4") = txt8
            Range("AM4") = txt10
            Range("AN4") = txt12
            Range("AO4") = txt14
            Range("AP4") = txt16
            Range("AQ4") = txt18
            Range("AR4") = txt20
            Range("AS4") = txt22
            Range("AT4") = txt25
            Range("AU4") = txt28
            Range("AV4") = txt32
            Range("AW4") = txt36
            Range("AX4") = txt40
            Range("AK4:AX4").Select
            Selection.NumberFormat = "#,##0.000"
Application.ScreenUpdating = True
End
End Sub


Private Sub cmdThoat_Click()
End
End Sub
 
ngày mới may mắn!!!!!!!!!!!!!!!!
Bạn cần phải đặt tên textbox sao cho có thể dùng for next để duyêt nó.
Ví dụ bạn có TextBox1 đến TextBox14.
For i = 1 To 14
Me.Controls("TextBox" & i) = Format(Me.Controls("TextBox" & i), "#,##0.000")
Next

các cột trên sheet cũng cần phải sắp xếp có khoa học thì sẽ rút gọn được code.
 
Upvote 0
Bạn cần phải đặt tên textbox sao cho có thể dùng for next để duyêt nó.
Ví dụ bạn có TextBox1 đến TextBox14.
For i = 1 To 14
Me.Controls("TextBox" & i) = Format(Me.Controls("TextBox" & i), "#,##0.000")
Next

các cột trên sheet cũng cần phải sắp xếp có khoa học thì sẽ rút gọn được code.

các ô xếp liên tiếp từ AK4 đến AX4 không "lủng" ô nào chưa đủ khoa học sao bạn ? --=0--=0
 
Upvote 0
Cảm ơn các a đã góp ý.
e sửa lại thế này, tuy nhiên các a cho e hỏi đoạn e tô màu đỏ có thể rút gọn lại bằng cách dùng for next được ko?
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
[COLOR=#ff0000]            Me.txt1 = Range("AK4")[/COLOR]
[COLOR=#ff0000]            Me.txt2 = Range("AL4")[/COLOR]
[COLOR=#ff0000]            Me.txt3 = Range("AM4")[/COLOR]
[COLOR=#ff0000]            Me.txt4 = Range("AN4")[/COLOR]
[COLOR=#ff0000]            Me.txt5 = Range("AO4")[/COLOR]
[COLOR=#ff0000]            Me.txt6 = Range("AP4")[/COLOR]
[COLOR=#ff0000]            Me.txt7 = Range("AQ4")[/COLOR]
[COLOR=#ff0000]            Me.txt8 = Range("AR4")[/COLOR]
[COLOR=#ff0000]            Me.txt9 = Range("AS4")[/COLOR]
[COLOR=#ff0000]            Me.txt10 = Range("AT4")[/COLOR]
[COLOR=#ff0000]            Me.txt11 = Range("AU4")[/COLOR]
[COLOR=#ff0000]            Me.txt12 = Range("AV4")[/COLOR]
[COLOR=#ff0000]            Me.txt13 = Range("AW4")[/COLOR]
[COLOR=#ff0000]            Me.txt14 = Range("AX4")[/COLOR]
        For i = 1 To 14
            Me.Controls("txt" & i) = Format(Me.Controls("txt" & i), "#,##0.000")
        Next
End Sub


Private Sub cmdCapNhat_Click()
Application.ScreenUpdating = False
            Range("AK2") = txtChungLoai
[COLOR=#ff0000]            Range("AK4") = txt1[/COLOR]
[COLOR=#ff0000]            Range("AL4") = txt2[/COLOR]
[COLOR=#ff0000]            Range("AM4") = txt3[/COLOR]
[COLOR=#ff0000]            Range("AN4") = txt4[/COLOR]
[COLOR=#ff0000]            Range("AO4") = txt5[/COLOR]
[COLOR=#ff0000]            Range("AP4") = txt6[/COLOR]
[COLOR=#ff0000]            Range("AQ4") = txt7[/COLOR]
[COLOR=#ff0000]            Range("AR4") = txt8[/COLOR]
[COLOR=#ff0000]            Range("AS4") = txt9[/COLOR]
[COLOR=#ff0000]            Range("AT4") = txt10[/COLOR]
[COLOR=#ff0000]            Range("AU4") = txt11[/COLOR]
[COLOR=#ff0000]            Range("AV4") = txt12[/COLOR]
[COLOR=#ff0000]            Range("AW4") = txt13[/COLOR]
[COLOR=#ff0000]            Range("AX4") = txt14[/COLOR]
       Range("AK4:AX4").Select
    Selection.NumberFormat = "#,##0.000"
Application.ScreenUpdating = True
End
End Sub


Private Sub cmdThoat_Click()
End
End Sub
 
Upvote 0
Cảm ơn các a đã góp ý.
e sửa lại thế này, tuy nhiên các a cho e hỏi đoạn e tô màu đỏ có thể rút gọn lại bằng cách dùng for next được ko?
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
[COLOR=#ff0000]            Me.txt1 = Range("AK4")[/COLOR]
[COLOR=#ff0000]            Me.txt2 = Range("AL4")[/COLOR]
[COLOR=#ff0000]            Me.txt3 = Range("AM4")[/COLOR]
[COLOR=#ff0000]            Me.txt4 = Range("AN4")[/COLOR]
[COLOR=#ff0000]            Me.txt5 = Range("AO4")[/COLOR]
[COLOR=#ff0000]            Me.txt6 = Range("AP4")[/COLOR]
[COLOR=#ff0000]            Me.txt7 = Range("AQ4")[/COLOR]
[COLOR=#ff0000]            Me.txt8 = Range("AR4")[/COLOR]
[COLOR=#ff0000]            Me.txt9 = Range("AS4")[/COLOR]
[COLOR=#ff0000]            Me.txt10 = Range("AT4")[/COLOR]
[COLOR=#ff0000]            Me.txt11 = Range("AU4")[/COLOR]
[COLOR=#ff0000]            Me.txt12 = Range("AV4")[/COLOR]
[COLOR=#ff0000]            Me.txt13 = Range("AW4")[/COLOR]
[COLOR=#ff0000]            Me.txt14 = Range("AX4")[/COLOR]
        For i = 1 To 14
            Me.Controls("txt" & i) = Format(Me.Controls("txt" & i), "#,##0.000")
        Next
End Sub
Chổ màu đỏ bạn sửa lại thế này thử xem (mình chưa test thử, nhưng đoán là vậy).
Mã:
For i = 1 To 14
            Me.Controls("txt" & i) = Range("AK4").Offset(, i - 1)
Next
Cụ thể hơn là thế này.
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
        For i = 1 To 14
[COLOR=#ff0000][B]               Me.Controls("txt" & i) = Range("AK4").Offset(, i - 1)[/B][/COLOR]
            Me.Controls("txt" & i) = Format(Me.Controls("txt" & i), "#,##0.000")
        Next
End Sub
Hoặc muốn ngắn hơn nửa thì sửa lại thế này.
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
        For i = 1 To 14
            Me.Controls("txt" & i) = Format([B]Range("AK4").Offset(, i - 1)[/B], "#,##0.000")
        Next
End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Chổ màu đỏ bạn sửa lại thế này thử xem (mình chưa test thử, nhưng đoán là vậy).
Mã:
For i = 1 To 14
            Me.Controls("txt" & i) = Range("AK4").Offset(, i - 1)
Next
Cụ thể hơn là thế này.
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
        For i = 1 To 14
[COLOR=#ff0000][B]               Me.Controls("txt" & i) = Range("AK4").Offset(, i - 1)[/B][/COLOR]
            Me.Controls("txt" & i) = Format(Me.Controls("txt" & i), "#,##0.000")
        Next
End Sub
Hoặc muốn ngắn hơn nửa thì sửa lại thế này.
Mã:
Private Sub UserForm_Initialize()
            Me.txtChungLoai = Range("AK2")
        For i = 1 To 14
            Me.Controls("txt" & i) = Format([B]Range("AK4").Offset(, i - 1)[/B], "#,##0.000")
        Next
End Sub

Cảm ơn a!
e đã thử và thấy rất ổn a à.
a có thể cho e hỏi thêm phần sau (cmdCapNhat) có thể rút ngắn tương tự như phần này được ko?
 
Upvote 0
Cảm ơn a!
e đã thử và thấy rất ổn a à.
a có thể cho e hỏi thêm phần sau (cmdCapNhat) có thể rút ngắn tương tự như phần này được ko?
Thì bạn thực hiện tương tự thôi mà, cho bạn thêm một cách khác nửa là.
Mã:
Private Sub cmdCapNhat_Click()
Application.ScreenUpdating = False
            Range("AK2") = txtChungLoai
            Range("AK4").Resize(, 14) = Array(Txt1, Txt2, Txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14)
    Range("AK4:AX4").NumberFormat = "#,##0.000"
Application.ScreenUpdating = True
End
End Sub
 
Upvote 0
Thì bạn thực hiện tương tự thôi mà, cho bạn thêm một cách khác nửa là.
Mã:
Private Sub cmdCapNhat_Click()
Application.ScreenUpdating = False
            Range("AK2") = txtChungLoai
            Range("AK4").Resize(, 14) = Array(Txt1, Txt2, Txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14)
    Range("AK4:AX4").NumberFormat = "#,##0.000"
Application.ScreenUpdating = True
End
End Sub

Cảm ơn a!
e làm tương tự thế này, ko biết đúng hay sai mà thấy chạy được. Giờ có cách trên của a thấy gọn hơn rất nhiều.
Mã:
Private Sub cmdCapNhat_Click()
Application.ScreenUpdating = False
            Range("AK2") = txtChungLoai
        For i = 1 To 14
            Range("AK4").Offset(, i - 1) = Controls("txt" & i)
        Next


       Range("AK4:AX4").Select
    Selection.NumberFormat = "#,##0.000"
Application.ScreenUpdating = True
End
End Sub
 
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom