Convert strings with VBA in Microsoft Excel

As is well known, table cells can contain strings or numbers. Numbers can also be formatted as dates. You can convert the cell contents into variables of the appropriate data type if you can recognize their type or their formatting. You can use the IsNumeric () and IsDate () functions for recognition, and the CDbl () and CDate () functions for conversion.

An example:

Sub Umwandeln()
Dim x As Double
Dim d As Date
Dim s As String
Dim i As Integer
ThisWorkbook.Worksheets("Tabelle1").Activate
Fori=1To4
If IsNumeric(Cells(i, 2).Value) Then
x = CDbl(Cells(i, 2).Value)
Cells(i, 3).Value = "Zahl"
ElseIf IsDate(Cells(i, 2).Value) Then
d = CDate(Cells(i, 2).Value)
Cells(i, 3).Value = "Datum"
Else
s = Cells(i, 2).Value
Cells(i, 3).Value = "String"
End If
Next i
End Sub

If the IsNumeric () function determines that the cell content is a number, it is converted into a double variable with the CDbl () function. If the IsDate () function is used to determine that the date is specified, the cell content is converted into a Date type variable with the CDate () function. Otherwise the cell content is assigned to a string variable. The application can then continue to work with the variables of the correct data type.

Was the explanation to "Convert strings with VBA in Microsoft Excel"Helpful? Rate now:

Weitere Erklärungen zu Skriptsprachen