Nhờ chỉ code

Liên hệ QC

pooh_vi_na

Thành viên mới
Tham gia
8/5/08
Bài viết
6
Được thích
0
Nhờ các bạn chỉ giúp mình đoạn code để ẩn form.

Ví du: Mình tạo form xong tạo button "hide form", lúc đang nhập, click vào button này sẽ ẩn form đi
 
Nhờ các bạn chỉ giúp mình đoạn code để ẩn form.

Ví du: Mình tạo form xong tạo button "hide form", lúc đang nhập, click vào button này sẽ ẩn form đi
Không hiểu bạn lắm. Bạn tạo Form ra rồi thì bạn làm gì đó trên Form sau đó bạn thoát nó thì bạn cần thêm nút thoát trên Form đó luôn (ẩn là thế nào?).
 
Upvote 0
Mình muốn fỏm ẩn đi để kiểm tra số liệu đã nhập liệu trong excel, sau đó hiện form lên để nhập tiếp cho dòng khác.
 
Upvote 0
Option Explicit

Dim m_Row As Integer

Private Sub cboMonth_Change()
Select Case cboMonth.ListIndex + 1
Case 1
ColBlackCopy.Text = "P"
ColBlackPrint.Text = "Q"
colColorCopy.Text = "R"
colColorPrint.Text = "S"
ColFax.Text = "U"
ColScan = "V"


prColBlackCopy.Text = "H"
prColBlackPrint.Text = "I"
prcolColorCopy.Text = "J"
prcolColorPrint.Text = "K"
prColFax.Text = "M"
prColScan = "N"

Case 2
ColBlackCopy.Text = "X"
ColBlackPrint.Text = "Y"
colColorCopy.Text = "Z"
colColorPrint.Text = "AA"
ColFax.Text = "AC"
ColScan = "AD"

prColBlackCopy.Text = "P"
prColBlackPrint.Text = "Q"
prcolColorCopy.Text = "R"
prcolColorPrint.Text = "S"
prColFax.Text = "U"
prColScan = "V"
Case 3
ColBlackCopy.Text = "AF"
ColBlackPrint.Text = "AG"
colColorCopy.Text = "AH"
colColorPrint.Text = "AI"
ColFax.Text = "AK"
ColScan = "AL"

prColBlackCopy.Text = "X"
prColBlackPrint.Text = "Y"
prcolColorCopy.Text = "Z"
prcolColorPrint.Text = "AA"
prColFax.Text = "AC"
prColScan = "AD"
Case 4
ColBlackCopy.Text = "AN"
ColBlackPrint.Text = "AO"
colColorCopy.Text = "AP"
colColorPrint.Text = "AQ"
ColFax.Text = "AS"
ColScan = "AT"

prColBlackCopy.Text = "AF"
prColBlackPrint.Text = "AG"
prcolColorCopy.Text = "AH"
prcolColorPrint.Text = "AI"
prColFax.Text = "AK"
prColScan = "AL"
Case 5
ColBlackCopy.Text = "AV"
ColBlackPrint.Text = "AW"
colColorCopy.Text = "AX"
colColorPrint.Text = "AY"
ColFax.Text = "BA"
ColScan = "BB"

prColBlackCopy.Text = "AN"
prColBlackPrint.Text = "AO"
prcolColorCopy.Text = "AP"
prcolColorPrint.Text = "AQ"
prColFax.Text = "AS"
prColScan = "AT"
Case 6
ColBlackCopy.Text = "BD"
ColBlackPrint.Text = "BE"
colColorCopy.Text = "BF"
colColorPrint.Text = "BG"
ColFax.Text = "BI"
ColScan = "BJ"

prColBlackCopy.Text = "AV"
prColBlackPrint.Text = "AW"
prcolColorCopy.Text = "AX"
prcolColorPrint.Text = "AY"
prColFax.Text = "BA"
prColScan = "BB"
Case 7
ColBlackCopy.Text = "BL"
ColBlackPrint.Text = "BM"
colColorCopy.Text = "BN"
colColorPrint.Text = "BO"
ColFax.Text = "BQ"
ColScan = "BR"

prColBlackCopy.Text = "BD"
prColBlackPrint.Text = "BE"
prcolColorCopy.Text = "BF"
prcolColorPrint.Text = "BG"
prColFax.Text = "BI"
prColScan = "BJ"
Case 8
ColBlackCopy.Text = "BT"
ColBlackPrint.Text = "BU"
colColorCopy.Text = "BV"
colColorPrint.Text = "BW"
ColFax.Text = "BY"
ColScan = "BZ"

prColBlackCopy.Text = "BL"
prColBlackPrint.Text = "BM"
prcolColorCopy.Text = "BN"
prcolColorPrint.Text = "BO"
prColFax.Text = "BQ"
prColScan = "BR"
Case 9
ColBlackCopy.Text = "CB"
ColBlackPrint.Text = "CC"
colColorCopy.Text = "CD"
colColorPrint.Text = "CE"
ColFax.Text = "CG"
ColScan = "CH"

prColBlackCopy.Text = "BT"
prColBlackPrint.Text = "BU"
prcolColorCopy.Text = "BV"
prcolColorPrint.Text = "BW"
prColFax.Text = "BY"
prColScan = "BZ"
Case 10
ColBlackCopy.Text = "CJ"
ColBlackPrint.Text = "CK"
colColorCopy.Text = "CL"
colColorPrint.Text = "CM"
ColFax.Text = "CO"
ColScan = "CP"

prColBlackCopy.Text = "CB"
prColBlackPrint.Text = "CC"
prcolColorCopy.Text = "CD"
prcolColorPrint.Text = "CE"
prColFax.Text = "CG"
prColScan = "CH"
Case 11
ColBlackCopy.Text = "CR"
ColBlackPrint.Text = "CS"
colColorCopy.Text = "CT"
colColorPrint.Text = "CU"
ColFax.Text = "CW"
ColScan = "CX"

prColBlackCopy.Text = "CJ"
prColBlackPrint.Text = "CK"
prcolColorCopy.Text = "CL"
prcolColorPrint.Text = "CM"
prColFax.Text = "CO"
prColScan = "CP"
Case 12
ColBlackCopy.Text = "CZ"
ColBlackPrint.Text = "DA"
colColorCopy.Text = "DB"
colColorPrint.Text = "DC"
ColFax.Text = "DE"
ColScan = "DF"

prColBlackCopy.Text = "CR"
prColBlackPrint.Text = "CS"
prcolColorCopy.Text = "CT"
prcolColorPrint.Text = "CU"
prColFax.Text = "CW"
prColScan = "CX"
End Select
End Sub

Private Sub cmdHide_Click()

End Sub

Private Sub ColBlackCopy_Change()

End Sub

Private Sub colColorCopy_Change()

End Sub

Private Sub colColorPrint_Change()

End Sub
Private Sub ColFax_Change()

End Sub

Private Sub colMACNo_Change()

End Sub

Private Sub ColScan_Change()

End Sub

Private Sub Label16_Click()

End Sub

Private Sub Label18_Click()

End Sub

Private Sub Label23_Click()

End Sub

Private Sub prColBlackCopy_Change()

End Sub
Private Sub prColBlackPrint_Change()

End Sub

Private Sub txtBKCopy_Change()
UpdateRow
End Sub

Private Sub UpdateRow()
If m_Row >= 0 Then
ActiveSheet.Cells(m_Row, ColBlackCopy.Text).value = ConvertNumber(txtBKCopy.Text)
ActiveSheet.Cells(m_Row, ColBlackPrint.Text).value = ConvertNumber(txtBKPrint.Text)
ActiveSheet.Cells(m_Row, colColorCopy.Text).value = ConvertNumber(txtColorCopy.Text)
ActiveSheet.Cells(m_Row, colColorPrint.Text).value = ConvertNumber(txtColorPrint.Text)
ActiveSheet.Cells(m_Row, ColFax.Text).value = ConvertNumber(txtFax.Text)
ActiveSheet.Cells(m_Row, ColScan.Text).value = ConvertNumber(txtScan.Text)
txtTotal.Text = Format(CalculateTotal, "#,##0")
Else
txtTotal.Text = 0
End If

CalculateDiff
End Sub


Private Sub txtBKCopy_Enter()
If Trim(txtBKCopy.Text) <> "" Then
txtBKCopy.Text = ConvertNumber(txtBKCopy.Text)
End If
End Sub

Private Sub txtBKCopy_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtBKCopy.Text) <> "" Then
txtBKCopy.Text = Format(ConvertNumber(txtBKCopy.Text), "#,###")
End If
End Sub

Private Sub txtBKCopy_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub txtBKPrint_Change()
UpdateRow
End Sub
Private Sub txtBKPrint_Enter()
If Trim(txtBKPrint.Text) <> "" Then
txtBKPrint.Text = ConvertNumber(txtBKPrint.Text)
End If
End Sub

Private Sub txtBKPrint_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtBKPrint.Text) <> "" Then
txtBKPrint.Text = Format(ConvertNumber(txtBKPrint.Text), "#,###")
End If
End Sub

Private Sub txtBKPrint_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub txtColorCopy_Change()
UpdateRow
End Sub

Private Sub txtColorCopy_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub txtColorPrint_Change()
UpdateRow
End Sub

Private Sub txtColorPrint_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Private Sub txtFax_Change()
UpdateRow
End Sub

Private Sub txtFax_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub
Private Sub txtScan_Change()
UpdateRow
End Sub

Private Sub txtScan_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub txtMACNo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then 'enter
TimKhachHang txtMACNo.Text
SHowInfo
ShowPrevious
KeyCode = 0
End If
End Sub

Private Sub TimKhachHang(ByVal macno As String)
Dim r As Range
'chu y
Set r = ActiveSheet.Columns(colMACNo.Text).Find(What:=macno, After:=ActiveSheet.Cells(1, colMACNo.Text), LookIn:=xlValues, LookAt:= _
xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)

If Not r Is Nothing Then
m_Row = r.Row
Else
m_Row = -1
lblCustomerName.Caption = ""
MsgBox "Khong dung", vbOKOnly
txtMACNo.SetFocus
End If
End Sub

'Lay mot so thong tin hang tim duoc
Private Sub SHowInfo()
If m_Row >= 0 Then
lblCustomerName.Caption = ActiveSheet.Cells(m_Row, colCustomerName.Text).Text
txtBKCopy.Text = ActiveSheet.Cells(m_Row, ColBlackCopy.Text).Text
txtBKPrint.Text = ActiveSheet.Cells(m_Row, ColBlackPrint.Text).Text
txtColorCopy.Text = ActiveSheet.Cells(m_Row, colColorCopy.Text).Text
txtColorPrint.Text = ActiveSheet.Cells(m_Row, colColorPrint.Text).Text
txtFax.Text = ActiveSheet.Cells(m_Row, ColFax.Text).Text
txtScan.Text = ActiveSheet.Cells(m_Row, ColScan.Text).Text
txtTotal.Text = Format(CalculateTotal, "#,##0")

Else
lblCustomerName.Caption = ""

txtBKCopy.Text = ""
txtBKPrint.Text = ""
txtColorCopy.Text = ""
txtColorPrint.Text = ""
txtFax.Text = ""
txtScan.Text = ""
txtTotal.Text = ""
txtMACNo.Text = ""

End If
End Sub

Private Sub ShowPrevious()
If m_Row >= 0 And cboMonth.ListIndex > 0 Then 'chi hien thi thang 2 tro di
txtBKCopyPR.Text = ActiveSheet.Cells(m_Row, prColBlackCopy.Text).Text
txtBKPrintPR.Text = ActiveSheet.Cells(m_Row, prColBlackPrint.Text).Text
txtColorCopyPR.Text = ActiveSheet.Cells(m_Row, prcolColorCopy.Text).Text
txtColorPrintPR.Text = ActiveSheet.Cells(m_Row, prcolColorPrint.Text).Text
txtFaxPR.Text = ActiveSheet.Cells(m_Row, prColFax.Text).Text
txtScanPR.Text = ActiveSheet.Cells(m_Row, prColScan.Text).Text
txtTotalPR.Text = Format(CalculateTotalPR, "#,##0")
Else
txtBKCopyPR.Text = ""
txtBKPrintPR.Text = ""
txtColorCopyPR.Text = ""
txtColorPrintPR.Text = ""
txtFaxPR.Text = ""
txtScanPR.Text = ""
txtTotalPR.Text = ""
End If
End Sub

Private Sub CalculateDiff()
txtBKCopydiff.Text = Format(ConvertNumber(txtBKCopy.Text) - ConvertNumber(txtBKCopyPR.Text), "#,##0")
txtBKPrintdiff.Text = Format(ConvertNumber(txtBKPrint.Text) - ConvertNumber(txtBKPrintPR.Text), "#,##0")
txtColorCopyDiff.Text = Format(ConvertNumber(txtColorCopy.Text) - ConvertNumber(txtColorCopyPR.Text), "#,##0")
txtColorPrintDiff.Text = Format(ConvertNumber(txtColorPrint.Text) - ConvertNumber(txtColorPrintPR.Text), "#,##0")
txtFaxDiff.Text = Format(ConvertNumber(txtFax.Text) - ConvertNumber(txtFaxPR.Text), "#,##0")
txtScanDiff.Text = Format(ConvertNumber(txtScan.Text) - ConvertNumber(txtScanPR.Text), "#,##0")
txtTotalDiff.Text = Format(ConvertNumber(txtTotal.Text) - ConvertNumber(txtTotalPR.Text), "#,##0")
End Sub

Private Sub txtMACNo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
End Sub

Private Sub UserForm_Initialize()
m_Row = -1
InitMonth
cboMonth.ListIndex = Month(Now) - 1 'thang hien hanh, phai -1 do nó tinh tu 0
End Sub

Private Function CalculateTotal() As Double
CalculateTotal = ConvertNumber(txtBKCopy.Text) + ConvertNumber(txtBKPrint.Text) + ConvertNumber(txtColorCopy.Text) + ConvertNumber(txtColorPrint.Text)
End Function

Private Function CalculateTotalPR() As Double
CalculateTotalPR = ConvertNumber(txtBKCopyPR.Text) + ConvertNumber(txtBKPrintPR.Text) + ConvertNumber(txtColorCopyPR.Text) + ConvertNumber(txtColorPrintPR.Text)
End Function

Private Function ConvertNumber(value As String) As Double
If IsNumeric(value) Then
ConvertNumber = CDbl(value)
Else
ConvertNumber = 0
End If
End Function

Private Sub cmdclo_Click()
Unload Me
End Sub

Private Sub InitMonth()
cboMonth.AddItem "Jan"
cboMonth.AddItem "Feb"
cboMonth.AddItem "Mar"
cboMonth.AddItem "Apr"
cboMonth.AddItem "May"
cboMonth.AddItem "Jun"
cboMonth.AddItem "Jul"
cboMonth.AddItem "Aug"
cboMonth.AddItem "Sep"
cboMonth.AddItem "Oct"
cboMonth.AddItem "Nov"
cboMonth.AddItem "Dec"
End Sub
 
Upvote 0
1/Mình tham gia đoan code hàng khủng cboMonth_Change() nên viết gọn lại như sau:

Mã:
Private Sub cboMonth_Change()
ColBlackCopy.Text = Choose(cboMonth.ListIndex + 1, "P", "X", "AF", "AN", "AV", "BD", "BL", "BT", "CB", "CJ", "CR", "CZ")
ColBlackPrint.Text = Choose(cboMonth.ListIndex + 1, "Q", "Y", "AG", "AO", "AW", "BE", "BM", "BU", "CC", "CK", "CS", "DA")
colColorCopy.Text = Choose(cboMonth.ListIndex + 1, "R", "Z", "AH", "AP", "AX", "BF", "BN", "BV", "CD", "CL", "CT", "DB")
colColorPrint.Text = Choose(cboMonth.ListIndex + 1, "S", "AA", "AI", "AQ", "AY", "BG", "BO", "BW", "CE", "CM", "CU", "DC")
ColFax.Text = Choose(cboMonth.ListIndex + 1, "U", "AC", "AK", "AS", "BA", "BI", "BQ", "BY", "CG", "CO", "CW", "DE")
ColScan = Choose(cboMonth.ListIndex + 1, "V", "AD", "AL", "AT", "BB", "BJ", "BR", "BZ", "CH", "CP", "CX", "DF")

prColBlackCopy.Text = Choose(cboMonth.ListIndex + 1, "H", "P", "X", "AF", "AN", "AV", "BD", "BL", "BT", "CB", "CJ", "CR")
prColBlackPrint.Text = Choose(cboMonth.ListIndex + 1, "I", "Q", "Y", "AG", "AO", "AW", "BE", "BM", "BU", "CC", "CK", "CS")
prcolColorCopy.Text = Choose(cboMonth.ListIndex + 1, "J", "R", "Z", "AH", "AP", "AX", "BF", "BN", "BV", "CD", "CL", "CT")
prcolColorPrint.Text = Choose(cboMonth.ListIndex + 1, "K", "S", "AA", "AI", "AQ", "AY", "BG", "BO", "BW", "CE", "CM", "CU")
prColFax.Text = Choose(cboMonth.ListIndex + 1, "M", "U", "AC", "AK", "AS", "BA", "BI", "BQ", "BY", "CG", "CO", "CW")
prColScan = Choose(cboMonth.ListIndex + 1, "N", "V", "AD", "AL", "AT", "BB", "BJ", "BR", "BZ", "CH", "CP", "CX")
End Sub
2/Cái gì VBE đã có thì không nên làm UDF nữa vì khó mà "ăn" được của chính nó.
Cụ thể cái hàm ConvertNumber(txtScan.Text) hoàn toàn có thể thay thế bằng Format(txtScan.Text,"0") hay cẩn thận hơn Val(Format(txtScan.Text,"0"))

3/Câu hỏi bài 1 mình nghĩ bạn có ý đùa hay sao ấy vì dòng lệnh Me.Hide biết tạo Form là biết nó.
 
Lần chỉnh sửa cuối:
Upvote 0
Thanks Sealand nhiều, mình chỉ mới tập tành viết form nên có nhiều thứ chưa biết, mong các bạn chỉ dẫn.
 
Upvote 0
ah mình muốn hỏi thêm một vấn đề, form của mình sau khi nhập thông tin của một row, chuyển sang row khác thì phần dữ liệu được nhập không được xóa,
 
Upvote 0
Code của nút OK sau phần nhập dữ liệu thêm phần reset lại các ô điều khiển, các ô dạng Text gán ="" còn ô dạng số gán =0
 
Upvote 0
Web KT

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

Back
Top Bottom