Take values from an excel spreadsheet and display in a pop-up

Asked

Viewed 2,124 times

3

I’m developing a spreadsheet and I’m having a hard time starting to work with the macros/vba. However I could not find a tutorial that showed me how to do what I need.

I have a table that says:

nome | data1 | data2

Maria | 01-01 | 02-01

Marco |  02-01 | 03-01

I wanted to create a button called "Today" that when clicked it through the function today() would check the table if any of the date columns contains today’s date and show the names in a pop-up. For example, assuming today is 02/01, when clicking the button would open a pop-up written: Maria and Marco.

Is this possible in Excel? Thank you very much.

  • Yes it is possible! You can use msgboxfor this! Just generate code logic in vba to make this check. Remembering dates with "01-01", using a dash - are not standard in Excel, but if you are using conditional formatting, ok! Have you done any code to generate this validation? Could you post?

  • @Evis I am using dates in excel standard, I put here just to exemplify. In the case I still could not do anything, I looked at some VBA tutorials but it is very complicated. I did not create the logic to generate this validation. Only in my head. The logic is: within an array of values (columns and excel rows) I will fetch today’s date. After taking the line reference that is dated today, I want to take the cell of the name of the person who has the corresponding date. And then display people’s names on msgbox.

2 answers

2


Good afternoon, my friend, I don’t know for what purpose you will use the method, but a simple way to solve what you are trying to do is to scroll through the two columns of dates you created and assign the names found to a string. Of course, depending on your business rule, this may vary. Follow example of the method:

Public Sub Pesquisa_Hoje()

Hoje = Format(Now, "DD/MM/YYYY")
Dim Achou As String
Dim Data As String

'Faz a busca na primeira coluna de datas
Fim = Worksheets("Planilha1").Range("B" & Rows.Count).End(xlUp).Row

For i = 2 To Fim
Data = Range("B" & i).Value

If Data = Hoje Then

    If Achou = "" Then
        Achou = Range("A" & i).Value
    Else
        Achou = Achou & ", " & Range("A" & i).Value
    End If

End If

Next

'Faz a busca novamente na segunda coluna de datas
Fim = Worksheets("Planilha1").Range("C" & Rows.Count).End(xlUp).Row

For i = 2 To Fim
Data = Range("C" & i).Value
If Data = Hoje Then

    If Achou = "" Then
        Achou = Range("A" & i).Value
    Else
        Achou = Achou & ", " & Range("A" & i).Value
    End If

End If

Next

'Mostra os nomes encontrados
If Achou <> "" Then

    MsgBox (Achou)

    Else

    MsgBox ("Nenhum nome encontrado!")

End If

End Sub
  • Thanks for the mood, Luiz. I tested it and it’s not finding any names. But come on, instead of DD/MM/YYYY I passed the date I wanted, right? Or should I insert in another corner? I am illiterate in vba. Running the algorithm is, because it is not found.

  • Another question: the button is in a spreadsheet but I am searching in another. The reference can be only: Worksheets("Outraplanilha") or needs some other character, such as !

  • Luiz Henrique, it worked! Thank you, only one problem. When I click on the button it won’t, but when I click on the VBA Play. Go. What might be going on?

  • 1

    Come on, first doubt, in your date columns I suggest putting the value in normal date format (Ex: 13/11/2017), and in the cell formatting the format you want to display (Ex: 13-11), why? The macro takes the value of the cell you are passing, so value will always be in the standard form according to the first example. In the other question, the example I sent, is working fully on the sheet "Planilha1", if you need to look for another sheet you can use the reference Worksheets("Outraplanilha") as exemplified.

  • That’s great! Now assign the macro to the button you created, it will probably work!

  • Great! With the dates worked, but then I assign to Macro and it does not find =(

  • 1

    Remembering that you must save the document as . xlsm and use by default on the button the code as follows: Private Sub Commandbutton1_click() call Search_today End Sub

  • I could do it right, it only works in the active spreadsheet but it’s worth it. A question: if I wanted to add more columns, I would have to copy the code?

  • Good afternoon, Yes, just copy the for part and add it to scroll through the new column you include.

Show 4 more comments

1

Code

There are several ways to match the values of an Excel file. (. Find, Match, Arrays, Dictionary, Collection, Autofilter, Loop, Excel Formula)

If the spreadsheet has a lot of data the methods that use arrays are faster, but the autofilter is also fast and easier to understand. Therefore, this will be used. The interaction between the VBA and the spreadsheet must be minimized and this reference hard-working.

Statements

Dim ws As Worksheet
'Worksheet de index 1 ou inserir o "NOME_entre_aspas"
Set ws = ThisWorkbook.Sheets(1)
Dim j As Long, i As Long, LastRow As Long
Dim VisibleRange As Range
Dim MyArray As Variant
ReDim MyArray(0 To 1000)
j = 0

Leading

Data = Format(Now, "dd-mm")
'Loop da Coluna 2 (B) até C(3)
With ws
    'Última Linha da coluna A
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 2 To 3
    'AutoFiltro
        .Range(.Cells(1, i), .Cells(1, i)).AutoFilter Field:=i, Criteria1:=Data
        'Cria range com as células visíveis após Filtrar
        On Error Resume Next
        Set VisibleRange = .Range(.Cells(2, 1), .Cells(LastRow, 1)).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not VisibleRange Is Nothing Then
        'Loop nas células visíveis após aplicação do AutoFiltro
        For Each Cell In VisibleRange
            MyArray(j) = Cell
            j = j + 1
        Next
        End If
        '"zera" o autofiltro, mostrando todos dados filtrados
        If .FilterMode Then
            .ShowAllData
        End If
    Next i
    'Redimensiona Array para quantidade de elementos correto
    If j > 0 Then
    ReDim Preserve MyArray(0 To j - 1)
    'Loop em cada elemento da Array para criar String
    For j = LBound(MyArray) To UBound(MyArray)
        txt = txt & MyArray(j) & vbCrLf
    Next j
    MsgBox txt
    Else
    MsgBox "Nenhuma data encontrada."
    End If
End With

Dice

Assuming the values as follows:

Dados

If you are from another, format in the spreadsheet or code.

Upshot

Resultado

  • Ta giving error in this part friend: . Range(.Cells(1, i), .Cells(1, i)). Autofilter Field:=i, Criteria1:=Data

  • @Joãoneto What are the columns used and the sheet name? The function Cells has the following syntax Cells(LINHAS , COLUNAS). Also check the date format. What error code?

  • There were no cells. Columns B and C and the name of the spreadsheet is Payments

  • Try to change from Set ws = ThisWorkbook.Sheets(1) for Set ws = ThisWorkbook.Sheets("Pagamentos"). If you do not filter, check how the dates are formatted, the code is configured to filter Data = Format(Now, "dd-mm")

  • All right, I’ll try it. The colleague code above tb worked. Let me ask you. Is there a way to increase msgbox capacity? Because I can’t fit everything in it... It’s a lot of records.

  • It has the ability to support 1024 characters. You have some options to get around the problem, one of them is to create a file. txt with this data, the other is to create a Userform to show them and the other is to insert them in a Spreadsheet. I suggest creating another question for this issue with the option you think best.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.