excel vba verificare se una cella contiene una data

Anonimo
2016-01-23T08:49:29+00:00

Salve a tutti,

mi servirebbe conoscere una procedura in VBA che verifichi se il contenuto di una cella è una data o un testo

Grazie

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2016-01-23T18:38:48+00:00

Non ho capito cosa vuoi fare.

Qui sotto come controllare la presenza di una data nelle celle A1:A10 del Foglio1.

Public Function f(ByVal vv As Variant) As Boolean

    On Error Resume Next

    Dim v As Variant

    f = False

    v = vv * 1

    If Err.Number = 0 Then

        f = IsDate(vv)

    End If

 End Function

Public Sub m()

    Dim sh As Worksheet

    Set sh = ThisWorkbook.Worksheets("Foglio1")

    Dim lng As Long

    With sh

        For lng = 1 To 10

            If f(.Range("A" & lng).Value) Then

                MsgBox "C'è una data"

            Else

                MsgBox "Non c'è una data"

            End If

        Next

    End With

    Set sh = Nothing

End Sub

GoTo è da evitare, assolutamente da evitare.

In grassetto le parti da modificare con le tue.

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento

2 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2016-01-23T11:43:47+00:00

    Ciao & grazie per  la risposta tempestiva (come al solito) 

    Io vorrei fare un qualcosa tipo quello che vedi qui sotto. Ma pare non funzionare.


    For i = 1 To 10

    If ActiveCell.Offset = IsDate(Value) Then GoTo linea1:

    ActiveCell.Offset(1, 0).Select

    Next

    linea1:

    End sub


    Ciao

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2016-01-23T09:29:02+00:00

    C'è la funzione IsDate:

    Public Sub m()

        With ActiveCell

            MsgBox IsDate(.Value)

        End With

    End Sub

    Ma, attenzione, se nella cella hai ad esempio 1/1 formattato come testo, ti restituisce comunque vero.

    Uno dei modi per evitarlo:

    Public Sub m()

        On Error Resume Next

        Dim v As Variant

        Dim bln As Boolean

        With ActiveCell

            v = .Value * 1

            If Err.Number = 0 Then

                bln = IsDate(.Value)

            End If

        End With

        MsgBox bln

    End Sub

    La risposta è stata utile?

    0 commenti Nessun commento