You are on page 1of 7

To bng chn d liu trong Excel

TO BNG CHN D LIU (DATA SELECTOR) Trong cc doanh nghip nh d liu thng c b tr trn Excel. Mt s ch doanh nghip khng quan tm n vic b tr cc d liu ny. Cng vic thit k v s dng d liu ny hon ton giao cho cc k ton vin. Mt s k ton vin khi thit k v b tr cc d liu li khng quan tm n vic s dng li cc d liu sau ny khi doanh nghip pht trin. Vic thit k cc bng lu tr, cc bng m cho khch hng, nhn vin bn hng, cc sn phm bn ra,...l cng vic rt quan trng ban u mc ni cc d liu vi nhau trong cng vic s l s liu nh: theo di cng n, theo di doanh s, phn tch theo cc yu cu...vv. Vn ti t ra y l khi mc ni cc d liu vi nhau bn s phi lm vic nhiu n vic la chn mt s m t cc bng m. Lm sao cng vic la chn ca bn c d dng? Liu Excel c cung cp cho ta cng c ny khng? Vng, Excel c cung cp cho ta mt s cng c nhng theo kinh nghim bn thn, ti ngh cch tt nht vn l t mnh thit k cho mnh mt bng chn. Cn cc s liu khc bn c th ly ra t cc hm chun ca Excel. y ti xin a ra mt v d nh v vic to bng chn, chn m sn phm t bng sn phm. V d trong sheet MaSanPham, ti c bng m sn phm nh sau: 1/t tn vng : Ti t tn cho vng A2:B570 c tn l MaSanPham. Bng cch nh du vng A2:B570 vo Insert / Name / Define. Trong hp thoi phn Names in Workbook bn g v MaSanPham , sau nhn nt OK. 2/ Phn 2 : Thit k form ngi dng chn la m sn phm.

T ca s Excel bn nhn t hp phm Alt + F11. Ca s VBE xut hin, sau bn chn Insert/UserForm . Ta t tn form l frmDataSelector, thuc tnh Caption l Data Selector. Trn form ta t cc i tng gm c: - 1 TextBox c tn l TxtCode - 1 nt lnh c tn l cmdOK - 1 nt lnh c tn l cmdCancel - 1 ListView c tn l LVDataSelector. (i tng ListView trn thanh Toolbox, nu khng c bn phi vo Tools/Additional Controls v chn 3/ Phn 3: Copy on m sau vo Module. on m trong Module, bn khng cn sa ch no c. Sau bn chn Insert/Module t tn Module l DataSelector v g vo cc hm v th tc sau: Code: Option Explicit Function RangeNameExists(Nname) As Boolean ' Kiem tra xem Ten bang co ton tai hay khong? ' Neu ton tai thi tra ve TRUE Dim n As Name RangeNameExists = False For Each n In ActiveWorkbook.Names If UCase(n.Name) = UCase(Nname) Then RangeNameExists = True Exit Function End If Next n End Function' Day la ham de xuat cac du lieu tu Bang da duoc dat ten ' sang mot mang

Code: Function TableToArray(ByVal TableName As String) Dim arr Dim vRange As Range Dim i As Long, j As Long, m As Long, n As Long If Not RangeNameExists(TableName) Then Exit Function 'Neu khong ton tai thi thoat On Error Resume Next Set vRange = Range(TableName) i = vRange.Rows.count j = vRange.Columns.count ReDim arr(1 To i, 1 To j) For m = 1 To i For n = 1 To j arr(m, n) = vRange(m, n).Value Next n Next m TableToArray = arr Set vRange = Nothing End Function'Chuyen tu mang sang ListView va dinh dang ListView Code: Sub ArrayToListview(ByVal VlistView As Listview, ByVal InputArray) Dim m As Long Dim i As Integer, j As Integer Dim bHang As Long, bCot As Long, bHeader As Integer Dim it As ListItem Dim anItem If Not IsObject(VlistView) Then Exit Sub On Error Resume Next 'Dem so hang va so cot trong InputArray bHang = UBound(InputArray, 1) bCot = UBound(InputArray, 2) 'Dinh dang ListView

VlistView.View = lvwReport VlistView.FullRowSelect = True VlistView.MultiSelect = False VlistView.Gridlines = True VlistView.LabelEdit = lvwManual VlistView.HideColumnHeaders = True bHeader = VlistView.ColumnHeaders.count Select Case bHeader 'Xac dinh so cot cua ListView Case Is < bCot For i = bHeader + 1 To bCot VlistView.ColumnHeaders.Add i Next i Case Is = bCot 'Khong lam gi ca Case Is > bCot 'Khong lam gi ca End Select 'Dien cac gia tri tu Inputarray vao Listview For i = 1 To bHang For j = 1 To bCot anItem = InputArray(i, j) If j = 1 Then Set it = VlistView.ListItems.Add() it.Text = anItem Else it.SubItems(j - 1) = anItem End If Next j Next i 'Dat do rong cac cot For i = 1 To bCot VlistView.ColumnHeaders(i).Width = 150 Next i Set it = Nothing Exit Sub Tbloi: MsgBox "Xin loi, khong the dua mang vao Listview " , vbCritical, "Th?b?

End Sub'Dua tu bang vao mang, sau do dua tu mang vao Listview 'Va di nhien ban cung co the chuyen tu bang vao ListView Code: Sub NhapDuLieu() On Error Resume Next Call DataSelector("MaVatTu") End Sub Sub DataSelector(Tenbang As String) Dim bMang1 On Error Resume Next bMang1 = TableToArray(Tenbang) Call ArrayToListview(frmDataSelector.LVDataSelector, bMang1) frmDataSelector.Show End Sub(Ch cn thay th l frmDataselector v LVDataselector, nu bn dng tn khc) 4/Phn 4 : on Code ca Form : on Code ny c nhng ch cn sa, ti s a ra c th, bn hy sa cho ph hp on m ca frmDataSelector nh sau: Code: Private Sub cmdCancel_Click() Unload Me 'Thoat End Sub(CmdCancel s c thay th nu bn dng tn khc cho nt Button ) Code: Private Sub cmdOK_Click() Dim bGiatrichon On Error Resume Next bGiatrichon = LVDataSelector.SelectedItem.Text

ActiveCell.Value = bGiatrichon 'Dat gia tri ban chon vao o hien tai End Sub(CmdOK s c thay th nu bn dng tn khc cho nt Button, LVDataselector cng c thay th nu bn dng tn khc ) 'Muc dich cua ham sau nham cuon danh sach trong Listview den ma tuong tu 'trong danh sach khi nguoi su dung go vao Textbox txtCode cac ky tu dau tien cua ma. Code: Private Sub txtCode_Change() Dim it As ListItem On Error Resume Next btim = Me.txtCode.Text Set it = Me.LVDataSelector.FindItem(btim, lvwText, , lvwPartial) bindex = it.Index Me.LVDataSelector.ListItems.Item(bindex).Selected = True Me.LVDataSelector.ListItems.Item(bindex).EnsureVis ible Set it = Nothing End Sub(TxtCode v LVDataselector l ch cn thay th) Gi s by gi ti mun ly m sn phm t bng MaSanPham, trong sheet MaSanPham, khi ti nhn chut phi ct 1 ca Sheet2 th on m trong Sheet2 nh sau: 5/Phn 5 : on m ca Sheet Code: Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As _ Boolean) If Target.Column = 1 Then Cancel = True Call NhapDuLieu End If End Subon Code ny bn cn lu : Target.Column = 1, hay mt s no khc tu bn mun bng chn hin ra ct no khi

Click chut phi By gi bn hy tr v ca s Excel, chn mt bt k ct 1 ca Sheet2, bn nhn chut phi th frmDataSelector hin ra , bn hy th g vo cc k t u tin ca m sn phm bn cn...v bn s thy n hot ng nh th no. Bn c th ci tin on m trong module DataSelector bng vic st nhp hm TableToArray v th tc ArrayToListview thnh mt. Chc cc bn thnh cng.