Option Explicit
Private Sub combobox1_Change() 'Choose the month
ListBox1.RowSource = ""
If ComboBox1.Value <> "" Then
Sheet2.Range("L2") = ComboBox1 'modulo2 filter advance
Call FiltroCbo
End If
Worksheets("sheet2").Range("L2") = Me.ComboBox1.Value
End Sub
Private Sub FilterData()
Dim Mês As String
Dim Desp2 As String
Dim myDB As Range 'range data in sheet1
On Error Resume Next
With Me
If .ComboBox1.ListIndex < 0 Then Exit Sub
'titulos na sheet1
Mês = .ComboBox1.Value
End With
With ActiveWorkbook.Sheets("sheet1")
Set myDB = .Range("A1:H1").Resize(.Cells(.Rows.Count, 1).End(xlUp).Row)
End With
With myDB
.AutoFilter ' remove filters
.AutoFilter Field:=3, Criteria1:=Mês 'filter data
.AutoFilter
End With
'Mes Seleccionado
Worksheets("sheet2").Range("L2") = Me.ComboBox1.Value
End Sub
Sub UpdateListbox(ListBox1 As MSForms.ListBox, myDB As Range, columnToList As Long)
Dim cell As Range, dataValues As Range
On Error Resume Next
If myDB.SpecialCells(xlCellTypeVisible).Count > myDB.Columns.Count Then
Set dataValues = myDB.Resize(myDB.Rows.Count + 1)
ListBox1.Clear 'we clear the listbox before adding new elements
For Each cell In dataValues.Columns(columnToList).SpecialCells(xlCellTypeVisible)
With Me.ListBox1
.AddItem cell.Value
.List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
.List(.ListCount - 1, 2) = cell.Offset(0, 2).Value
.List(.ListCount - 1, 3) = cell.Offset(0, 3).Value
.List(.ListCount - 1, 4) = cell.Offset(0, 4).Value
.List(.ListCount - 1, 5) = cell.Offset(0, 5).Value
.List(.ListCount - 1, 6) = cell.Offset(0, 6).Value
.List(.ListCount - 1, 7) = cell.Offset(0, 7).Value
End With
Next cell
Else
ListBox1.Clear 'if no match then clear listbox
End If
ListBox1.SetFocus
End Sub
Private Sub CommandButton2_Click() 'DELETE
Dim i As Integer
If ListBox1.Value = "" Then
MsgBox ("Please fill up, the Months or Value commands, and select them")
Else
If MsgBox("Are you sure you want to delete this row?", vbYesNo + vbQuestion, "Delete row") = vbYes Then
For i = 1 To Range("a10000").End(xlUp).Row
If Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then
Rows(i).Select
Selection.Delete
End If
Next i
End If
End If
ComboBox1.Value = ""
ListBox1.RowSource = ""
End Sub
Private Sub UserForm_Initialize()
On Error Resume Next
Dim Base As Range
Dim Nome As String
Dim Lh As Long
Dim dict, key
Dim lastRow As Long
Dim Sb As Double
lastRow = Application.WorksheetFunction.CountA(Sheets("sheet1").Range("C:C"))
Worksheets("sheet1").Range ("c2:c" & lastRow)
dict = Sheets("sheet1").Value
With CreateObject("scripting.dictionary")
.comparemode = 1 'vbtextcompare - case words doesn't matter:apple
For Each key In dict
If Not .exists(key) Then .Add key, Nothing
Next
If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
End With
With CreateObject("scripting.dictionary")
.comparemode = 1
For Each key In dict
If Not .exists(key) Then .Add key, Nothing
Next
End With
Me.ComboBox1.List = Array("JANEIRO", "FEVEREIRO", _
"MARÇO", "ABRIL", "MAIO", "JUNHO", "JULHO", "AGOSTO", _
"SETEMBRO", "OUTUBRO", "NOVEMBRO", "DEZEMBRO")
Lh = Sheet1.Range("A1").CurrentRegion.Rows.Count
Set Base = Sheet1.Range(Sheet1.Cells(2, 1), Sheet1.Cells(Lh, 7)) 'Sheet1.Range("A1").CurrentRegion
Nome = "'" & Sheet1.Name & "'!"
ListBox1.ColumnCount = 7
End Sub