Zmienianie wartości z tekstowej na numeryczną

Często się zdarza, że chcemy wyszukać pionowo jakieś wartości, ale jest zwracany błąd #N/A. Znalazłem w internecie jak obejść ten problem.

Chodzi o taką właśnie sytuację

Konwertowanie na liczbę w Excel

Gdy będziemy chcieli tą wartość wyszukać za pomocą funkcji wyszukaj pionowo, a wartością szukaną będzie liczbą, funkcja prawdopodobnie zwróci nam błąd.
A to rozwiązanie problemu ze strony https://www.thoughtco.com/convert-text-to-number-in-excel-3424223.

Private Sub ChangeValuesToNumbers()

'https://www.thoughtco.com/convert-text-to-number-in-excel-3424223

Dim xCell
For Each xCell In Selection
   xCell.Value = xCell.Value
Next xCell

End Sub

Cały kod będzie wyglądać tak (wykorzystano też kod z komentarzy):

Dim RowValue, ColValue as Long

Public Sub KolejnyWO_button()

WoCellsRangeSelect
ChangeValuesToNumbers

End Sub

'---------------------------

Private Sub CellsRangeSelect()

' http://www.rondebruin.nl/win/s9/win005.htm

RowValue = 2
ColValue  = 2

ThisWorkbook.Sheets("SheetName").Select
With ThisWorkbook.Sheets("SheetName")
    lastRow = .Cells(.Rows.Count, "ColIndex").End(xlUp).Row
    Range(Cells(RowValue, ColValue), Cells(lastRow, ColValue)).Select
End With

End Sub

'---------------------------

Private Sub ChangeValuesToNumbers()

'https://www.thoughtco.com/convert-text-to-number-in-excel-3424223

Dim xCell

Application.ErrorCheckingOptions.NumberAsText = True

For Each xCell In Selection
    If xCell.Errors.Item(xlNumberAsText).Value = True Then
        xCell.Value = xCell.Value
    End If
Next xCell

End Sub

 

3 myśli w temacie “Zmienianie wartości z tekstowej na numeryczną

  1. Jest też możliwość pozywania/ukrywania informacji o konwercji tekstu na liczbę. Ustawienie wartości na FALSE spowoduje, że nie pojawi się zielony trójkąt przy wartości liczbowej będącej tekstem.

    https://docs.microsoft.com/en-us/office/vba/api/excel.errorcheckingoptions.numberastext
    Sub CheckNumberAsText()

    ’ Simulate an error by referencing a number stored as text.
    Application.ErrorCheckingOptions.NumberAsText = True
    Range(„A1”).Value = „’1”

    End Sub

  2. Aby wykluczyć prawidłowe liczby z konwercji z tekstu można zastosować poniższy kod:

    Sub testNumber()
    https://docs.microsoft.com/pl-pl/office/vba/api/excel.errorcheckingoptions

    Application.ErrorCheckingOptions.NumberAsText = True

    With ActiveSheet
    If .Cells(5, 2).Errors.Item(xlNumberAsText).Value = True Then
    Debug.Print .Cells(5, 2); ” nie jest liczbą”
    Else
    Debug.Print .Cells(5, 2); ” jest liczbą”
    End If

    If .Cells(7, 2).Errors.Item(xlNumberAsText).Value = True Then
    Debug.Print .Cells(7, 2); ” nie jest liczbą”
    Else
    Debug.Print .Cells(7, 2); ” jest liczbą”
    End If
    End With

    End Sub

    w .Cells(5, 2) jest prawidłowa liczba, a w .Cells(7, 2) jest liczba w postaci tekstu.

  3. Jest dodatkowa opcja. Nie trzeba zamieniać żadnych wartości. Używamy funkcji FIND i zwracamy numer wiersza jeżeli wartość została znaleziona.

    Sub Search_Item()

    https://stackoverflow.com/questions/32190029/excel-vba-find-row-number-of-matching-value

    Dim FoundCell As Range
    Dim item As Long

    item = 18298337

    Set FoundCell = ActiveSheet.Columns(„A:A”).Find(What:=item)
    If Not FoundCell Is Nothing Then
    Debug.Print item & ” found in row: ” & FoundCell.Row
    Else
    Debug.Print item & ” not found”
    End If

    End Sub

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.