2016-07-19 2 views
0

文字列から数値を取り除くことができません。 Excelでは、数字を含むことができる多くの文字列フィールドがあります。私は数字だけを気にし、残りの文字は捨てられます。番号は、設定された場所ではなく、どの位置にあってもかまいません。例えば文字列から数値を取り除くことができない

: 「2つのCATCH流域」または「CATCH流域×2」

私はこのSO答えに私のコードをベースにしていますが、私はそれが仕事を得るように見えることはできません。エラーメッセージは、 'アプリケーション定義またはオブジェクト定義のエラー'です。

Option Explicit 

Function onlyDigits(s As String) As String 
' Variables needed (remember to use "option explicit"). ' 
Dim retval As String ' This is the return string.  ' 
Dim i As Integer  ' Counter for character position. ' 

' Initialise return string to empty      ' 
retval = "" 

' For every character in input string, copy digits to  ' 
' return string.          ' 
For i = 1 To Len(s) 
    If Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Then 
     retval = retval + Mid(s, i, 1) 
    End If 
Next 

' Then return the return string.       ' 
onlyDigits = retval 
End Function 

Public Sub CommandButton1_Click() 

Application.ScreenUpdating = False 

' ----------------------------------------------------------------------------------------- 
' Will strip numbers from descriptions for basins, guy wires, water meters/valves & pull box 
' ----------------------------------------------------------------------------------------- 

Dim counter As Integer 'Index for the While Loop 
Dim fCode As String 'Variable for column E, feature code 
Dim fDesc As String 'Variable for column F, the descriptor 


Do While Cells(counter, 1).Value <> "" 'While the first column has any data, keep looping 
fCode = Cells(counter, 5).Value 'Populate feature code variable from column E 

If (fCode = "XCB") Or (fCode = "XGW") Or (fCode = "XWV") Or (fCode = "XWM") Then 
    fDesc = Cells(counter, 6).Value 
    Cells(counter, 6).Value = onlyDigits(fDesc) 
Else 
    'do nothing 
End If 


counter = counter + 1 
Loop 'Finishes checking for numbers within specific descriptors 

誰かが正しい方向に向いていますか?大変感謝しています!

ここ

答えて

2

Do While Cells(counter, 1).Value

counterゼロであるが、範囲のインデックスは、したがって1でエラーを開始します。

+0

おっと、私はそれを試みたと思った!それは私の面で大きな監督です。ありがとうアレックスK、それは今完璧に動作します! –

関連する問題