Data form I
Bạn thường xuyên phải nhập liệu, có thể data form ver 2.0 có thể giúp cho bạn. Đây là phiên bản miễn phí, các bạn có thể download từ đây:
http://j-walk.com/ss/dataform/download.htm
Đây là đoạn code của module
Option Explicit
' NOTE: Non-English translation is done in Sheet1 of this workbook.
' Set the IsAddIn property to False, and enter the translations.
' Then set the IsAddIn property to True
' Set the LANGUAGE constant to correspond to the column
Public Const LANGUAGE As Long = 1
Public Const APPNAME As String = "JWalk Enhanced Data Form" 'displayed in title bars
'CriteriaData is a custom data type for the Criteria array
Type CriteriaData
FieldNumber As Long
Value As Variant
End Type
'UndoData is a custom data type used for undoing a Delete or a data entry
Type UndoData
RecNum As Long
Address As String
Contents As Variant
End Type
Public UndoArray() As UndoData
Public Criteria() As CriteriaData 'Stores the criteria when searching
Public DatabaseRange As Range 'The range that contains the worksheet database
Public Text(1 To 36) As String 'Stores the text items used in the Form
Sub ShowDataForm()
' This is the "entry" subroutine that is executed
' To execute this procedure from a VBA procedure in a different project, use:
' Application.Run "dataform.xla!ShowDataForm"
' Note: The ThisWorkbook code module contains code to create the new menu item on the Data menu
' Sheet protected?
If ActiveSheet.ProtectContents Then
MsgBox "This command cannot be used on a protected worksheet." & vbCrLf & "Use the Tools - Protection - Unprotect Sheet command and try again.", vbCritical, APPNAME
Exit Sub
End If
' Create a Range variable for the database
Set DatabaseRange = ActiveCell.CurrentRegion
' Check for single cell
If DatabaseRange.Count = 1 Then
MsgBox "Cannot detect a database at the active cell." & vbCrLf & vbCrLf & "Select any cell within the database and try again.", vbCritical, APPNAME
Exit Sub
End If
' Check for empty database
If DatabaseRange.Rows.Count = 1 And DatabaseRange.Columns.Count > 2 Then
If MsgBox("Cannot detect a database at the active cell." & vbCrLf & vbCrLf & "Do you want to use the current row as field names for a new database?", vbCritical + vbYesNo, APPNAME) = vbNo Then Exit Sub
DatabaseRange(2, 1).Select
ActiveCell.Value = "[New]"
Set DatabaseRange = ActiveCell.CurrentRegion
End If
' Display the main dialog box
' This causes the FormMain_Initialize subroutine to execute
' The remainder of the code is in the FormMain code module
FormMain.Show
End Sub
Đây là đoạn code của form main
Option Explicit
Public RowOffset As Long ' No. of rows database is offset from the top
Public ColumnOffset As Long ' No. of columns database is offset from the left
Public RecordCount As Long, FieldCount As Long
Public CurrentRecord As Long 'Stores the current record, displayed in the form
Public ScrollBarClicked As Boolean 'Flag that determines how ScrollBar1_Change was called
Public InsertedRow As Long 'Used to undo a row insert
Public InsertedRec As Long 'Used to undo a row insert
Public CriteriaEntered As Boolean 'True if criteria are entered in the Criteria page
Public CriteriaCount As Long 'number of criteria entered
Private Sub UserForm_Initialize()
'This sub is executed before FormMain is displayed
Dim i As Long, j As Long, k As Long
Dim CurrentFrame As Frame
Dim VerticalPosition As Long
Dim NewLabel As Control
Dim NewControl As Control
Dim Options As Range
Dim ctl As Control
Dim ModifiedName As String
Dim ComboBoxOptions As Variant
'Get the text items
Call TranslateText(LANGUAGE)
Me.Width = UserWidth() 'Function looks for DF_WIDTH name
Me.Height = UserHeight() 'Function looks for DF_HEIGHT name
'If DF_WIDTH and/or DF_HEIGHT range names exist, change the form dimensions
If Me.Width <> 270 Or Me.Height <> 240 Then '270 and 240 are the default dimensions
MultiPage1.Width = Me.Width - 20
MultiPage1.Height = Me.Height - 54
HelpButton.Top = Me.Height - 42
LabelRecNum.Top = Me.Height - 38
UndoButton.Top = Me.Height - 42
CloseButton.Top = Me.Height - 42
ScrollBar1.Top = MultiPage1.Height - 36
NewButton.Left = MultiPage1.Width - 60
InsertButton.Left = MultiPage1.Width - 60
DeleteButton.Left = MultiPage1.Width - 60
FindPreviousButton.Left = MultiPage1.Width - 60
FindNextButton.Left = MultiPage1.Width - 60
ClearCriteriaButton.Left = MultiPage1.Width - 60
TipsButton.Left = MultiPage1.Width - 60
CloseButton.Left = Me.Width - 60
UndoButton.Left = CloseButton.Left - 66
Frame1.Width = MultiPage1.Width - 74
Frame1.Height = MultiPage1.Height - 48
Frame2.Width = MultiPage1.Width - 74
Frame2.Height = MultiPage1.Height - 48
ScrollBar1.Width = Frame1.Width
End If
'Set the caption for the form
Me.Caption = APPNAME
'Determine the offsets (important if the database does not begin in A1)
RowOffset = DatabaseRange.Rows(1).Row
ColumnOffset = DatabaseRange.Columns(1).Column - 1
'Determine the size of the database
RecordCount = DatabaseRange.Rows.Count - 1 'First row is assumed to be labels
FieldCount = DatabaseRange.Columns.Count
'Set up the Labels and TextBoxes/ComboBoxes for the fields dynamically
For i = 1 To 2
'First time thru, do Page1 (Data entry, Frame1 on Page1 of the MultiPage control)
'Second time thru do Page2 (Criteria, Frame2 on Page2 of the MultiPage control)
If i = 1 Then Set CurrentFrame = Frame1 Else Set CurrentFrame = Frame2
VerticalPosition = 5 'controls the vertical placement of the controls
For j = 1 To FieldCount
'Add a label
Set NewLabel = CurrentFrame.Controls.Add("forms.label.1")
With NewLabel
.Top = VerticalPosition + 3
.Left = 4
.Width = 60
.TextAlign = 3
.WordWrap = False
.Caption = Application.WorksheetFunction.Clean(DatabaseRange.Cells(1, j))
.Height = 16
.Font.Size = 8
End With
'Is a Name defined with an array of choices?
ModifiedName = Application.Substitute(NewLabel.Caption, " ", "_")
If NameHasData(ModifiedName) Then
'Add a ComboBox
Set NewControl = CurrentFrame.Controls.Add("forms.combobox.1")
ComboBoxOptions = UNIQUEITEMS(Worksheets(Range(ModifiedName).Parent.Name).Range(ModifiedName))
If IsArray(ComboBoxOptions) Then NewControl.List = ComboBoxOptions
Else
'Add a TextBox
Set NewControl = CurrentFrame.Controls.Add("forms.textbox.1")
End If
With NewControl
.Top = VerticalPosition
.Left = 66
'Adjust the width, based on whether scroll bar appears in the frame
If FieldCount * 18 > Frame1.Height Then
.Width = Me.Frame1.Width - 85 'scroll bar present
Else
.Width = Me.Frame1.Width - 72
End If
.Height = 16
.Font.Size = 8
End With
VerticalPosition = VerticalPosition + 18 'increment
Next j
'Determine how much to scroll the frame, based on the number of fields
CurrentFrame.ScrollHeight = (FieldCount * 18) + 5
Next i
'Display the record in the row of the active cell
CurrentRecord = ActiveCell.Row - DatabaseRange.Rows(1).Row
If CurrentRecord = 0 Then CurrentRecord = 1 'Title row is selected
'Make sure Page1 of the MultiPage control is displayed
MultiPage1.Value = 0
MultiPage1_Change ' Call the sub that's executed when the page is changed
'Set up the record scroll bar
'ScrollBar1 is always in sync with the CurrentRecord
With ScrollBar1
.Min = 1
.Max = RecordCount
.Value = CurrentRecord
End With
'Update the form
Call UpdateForm
'Set the focus to the first field
On Error Resume Next
With Frame1.Controls(1)
.SelStart = 0
.SelLength = Len(Frame1.Controls(1).Text)
.SetFocus
End With
ScrollBarClicked = True
Range(Cells(CurrentRecord + RowOffset, 1 + ColumnOffset), Cells(CurrentRecord + RowOffset, ColumnOffset + FieldCount)).Select
End Sub
Bạn thường xuyên phải nhập liệu, có thể data form ver 2.0 có thể giúp cho bạn. Đây là phiên bản miễn phí, các bạn có thể download từ đây:
http://j-walk.com/ss/dataform/download.htm
Đây là đoạn code của module
Option Explicit
' NOTE: Non-English translation is done in Sheet1 of this workbook.
' Set the IsAddIn property to False, and enter the translations.
' Then set the IsAddIn property to True
' Set the LANGUAGE constant to correspond to the column
Public Const LANGUAGE As Long = 1
Public Const APPNAME As String = "JWalk Enhanced Data Form" 'displayed in title bars
'CriteriaData is a custom data type for the Criteria array
Type CriteriaData
FieldNumber As Long
Value As Variant
End Type
'UndoData is a custom data type used for undoing a Delete or a data entry
Type UndoData
RecNum As Long
Address As String
Contents As Variant
End Type
Public UndoArray() As UndoData
Public Criteria() As CriteriaData 'Stores the criteria when searching
Public DatabaseRange As Range 'The range that contains the worksheet database
Public Text(1 To 36) As String 'Stores the text items used in the Form
Sub ShowDataForm()
' This is the "entry" subroutine that is executed
' To execute this procedure from a VBA procedure in a different project, use:
' Application.Run "dataform.xla!ShowDataForm"
' Note: The ThisWorkbook code module contains code to create the new menu item on the Data menu
' Sheet protected?
If ActiveSheet.ProtectContents Then
MsgBox "This command cannot be used on a protected worksheet." & vbCrLf & "Use the Tools - Protection - Unprotect Sheet command and try again.", vbCritical, APPNAME
Exit Sub
End If
' Create a Range variable for the database
Set DatabaseRange = ActiveCell.CurrentRegion
' Check for single cell
If DatabaseRange.Count = 1 Then
MsgBox "Cannot detect a database at the active cell." & vbCrLf & vbCrLf & "Select any cell within the database and try again.", vbCritical, APPNAME
Exit Sub
End If
' Check for empty database
If DatabaseRange.Rows.Count = 1 And DatabaseRange.Columns.Count > 2 Then
If MsgBox("Cannot detect a database at the active cell." & vbCrLf & vbCrLf & "Do you want to use the current row as field names for a new database?", vbCritical + vbYesNo, APPNAME) = vbNo Then Exit Sub
DatabaseRange(2, 1).Select
ActiveCell.Value = "[New]"
Set DatabaseRange = ActiveCell.CurrentRegion
End If
' Display the main dialog box
' This causes the FormMain_Initialize subroutine to execute
' The remainder of the code is in the FormMain code module
FormMain.Show
End Sub
Đây là đoạn code của form main
Option Explicit
Public RowOffset As Long ' No. of rows database is offset from the top
Public ColumnOffset As Long ' No. of columns database is offset from the left
Public RecordCount As Long, FieldCount As Long
Public CurrentRecord As Long 'Stores the current record, displayed in the form
Public ScrollBarClicked As Boolean 'Flag that determines how ScrollBar1_Change was called
Public InsertedRow As Long 'Used to undo a row insert
Public InsertedRec As Long 'Used to undo a row insert
Public CriteriaEntered As Boolean 'True if criteria are entered in the Criteria page
Public CriteriaCount As Long 'number of criteria entered
Private Sub UserForm_Initialize()
'This sub is executed before FormMain is displayed
Dim i As Long, j As Long, k As Long
Dim CurrentFrame As Frame
Dim VerticalPosition As Long
Dim NewLabel As Control
Dim NewControl As Control
Dim Options As Range
Dim ctl As Control
Dim ModifiedName As String
Dim ComboBoxOptions As Variant
'Get the text items
Call TranslateText(LANGUAGE)
Me.Width = UserWidth() 'Function looks for DF_WIDTH name
Me.Height = UserHeight() 'Function looks for DF_HEIGHT name
'If DF_WIDTH and/or DF_HEIGHT range names exist, change the form dimensions
If Me.Width <> 270 Or Me.Height <> 240 Then '270 and 240 are the default dimensions
MultiPage1.Width = Me.Width - 20
MultiPage1.Height = Me.Height - 54
HelpButton.Top = Me.Height - 42
LabelRecNum.Top = Me.Height - 38
UndoButton.Top = Me.Height - 42
CloseButton.Top = Me.Height - 42
ScrollBar1.Top = MultiPage1.Height - 36
NewButton.Left = MultiPage1.Width - 60
InsertButton.Left = MultiPage1.Width - 60
DeleteButton.Left = MultiPage1.Width - 60
FindPreviousButton.Left = MultiPage1.Width - 60
FindNextButton.Left = MultiPage1.Width - 60
ClearCriteriaButton.Left = MultiPage1.Width - 60
TipsButton.Left = MultiPage1.Width - 60
CloseButton.Left = Me.Width - 60
UndoButton.Left = CloseButton.Left - 66
Frame1.Width = MultiPage1.Width - 74
Frame1.Height = MultiPage1.Height - 48
Frame2.Width = MultiPage1.Width - 74
Frame2.Height = MultiPage1.Height - 48
ScrollBar1.Width = Frame1.Width
End If
'Set the caption for the form
Me.Caption = APPNAME
'Determine the offsets (important if the database does not begin in A1)
RowOffset = DatabaseRange.Rows(1).Row
ColumnOffset = DatabaseRange.Columns(1).Column - 1
'Determine the size of the database
RecordCount = DatabaseRange.Rows.Count - 1 'First row is assumed to be labels
FieldCount = DatabaseRange.Columns.Count
'Set up the Labels and TextBoxes/ComboBoxes for the fields dynamically
For i = 1 To 2
'First time thru, do Page1 (Data entry, Frame1 on Page1 of the MultiPage control)
'Second time thru do Page2 (Criteria, Frame2 on Page2 of the MultiPage control)
If i = 1 Then Set CurrentFrame = Frame1 Else Set CurrentFrame = Frame2
VerticalPosition = 5 'controls the vertical placement of the controls
For j = 1 To FieldCount
'Add a label
Set NewLabel = CurrentFrame.Controls.Add("forms.label.1")
With NewLabel
.Top = VerticalPosition + 3
.Left = 4
.Width = 60
.TextAlign = 3
.WordWrap = False
.Caption = Application.WorksheetFunction.Clean(DatabaseRange.Cells(1, j))
.Height = 16
.Font.Size = 8
End With
'Is a Name defined with an array of choices?
ModifiedName = Application.Substitute(NewLabel.Caption, " ", "_")
If NameHasData(ModifiedName) Then
'Add a ComboBox
Set NewControl = CurrentFrame.Controls.Add("forms.combobox.1")
ComboBoxOptions = UNIQUEITEMS(Worksheets(Range(ModifiedName).Parent.Name).Range(ModifiedName))
If IsArray(ComboBoxOptions) Then NewControl.List = ComboBoxOptions
Else
'Add a TextBox
Set NewControl = CurrentFrame.Controls.Add("forms.textbox.1")
End If
With NewControl
.Top = VerticalPosition
.Left = 66
'Adjust the width, based on whether scroll bar appears in the frame
If FieldCount * 18 > Frame1.Height Then
.Width = Me.Frame1.Width - 85 'scroll bar present
Else
.Width = Me.Frame1.Width - 72
End If
.Height = 16
.Font.Size = 8
End With
VerticalPosition = VerticalPosition + 18 'increment
Next j
'Determine how much to scroll the frame, based on the number of fields
CurrentFrame.ScrollHeight = (FieldCount * 18) + 5
Next i
'Display the record in the row of the active cell
CurrentRecord = ActiveCell.Row - DatabaseRange.Rows(1).Row
If CurrentRecord = 0 Then CurrentRecord = 1 'Title row is selected
'Make sure Page1 of the MultiPage control is displayed
MultiPage1.Value = 0
MultiPage1_Change ' Call the sub that's executed when the page is changed
'Set up the record scroll bar
'ScrollBar1 is always in sync with the CurrentRecord
With ScrollBar1
.Min = 1
.Max = RecordCount
.Value = CurrentRecord
End With
'Update the form
Call UpdateForm
'Set the focus to the first field
On Error Resume Next
With Frame1.Controls(1)
.SelStart = 0
.SelLength = Len(Frame1.Controls(1).Text)
.SetFocus
End With
ScrollBarClicked = True
Range(Cells(CurrentRecord + RowOffset, 1 + ColumnOffset), Cells(CurrentRecord + RowOffset, ColumnOffset + FieldCount)).Select
End Sub