英文:
Cleaning non-numericals from a column of cells
问题
以下是翻译好的部分:
"我的最终目标是创建一个宏,用于清理电话号码行,但距离我上次进行VBA编程已经有一段时间,我觉得自己已经忘了一些东西。
第一步应该是进入一个单元格并清除其中的非数字数据,为此我编写了以下代码。它应该通过选择列中具有数据的第一个单元格开始工作,然后它应该只是进入每个单元格,从字符串中清除非数字数据,然后移动到列中的下一个单元格并重复此过程,直到遇到空行。
它正在循环遍历行,但没有清理数据。
在宏和函数之间,这应该有效,我认为。我不确定我做错了什么,但我担心这是一些非常简单的东西。
Sub Phonetest1()
ActiveCell.Range("A1").Select
Do
RemoveAllNonNums (A1)
ActiveCell.Offset(1, 0).Range("A1").Select
Loop Until Application.CountA(ActiveCell.EntireRow) = 0
End Sub
Function RemoveAllNonNums(myCell As String)
Dim myChar As String
Dim x As Integer
Dim i As String
i = ""
For x = 1 To Len(myCell)
myChar = Mid(myCell, x, 1)
If Asc(myChar) >= 48 And _
Asc(myChar) <= 57 Then
i = i & myChar
End If
Next
RemoveAllNonNums = Val(i)
End Function
希望这可以帮助您检查您的代码问题。
英文:
My ultimate goal is to make a macro that will clean up a row of telephone numbers, but it's been a while since I had to do any VBA programing, and I think I've forgotten something.
The first step should be to go to a cell and clean out any non-numerical data, for that I built the following code. It should work by selecting the first cell with data in the column, than it should just go to each cell, clean out the non-numerical data from the string and then move to the next cell in the column and repeat, until it comes across a blank row.
It's cycling through the rows, but it isn't cleaning the data.
Between the macro and the function this SHOULD work, I think. I'm not sure what I've done wrong, but I fear it's something very simple.
Sub Phonetest1()
ActiveCell.Range("A1").Select
Do
RemoveAllNonNums (A1)
ActiveCell.Offset(1, 0).Range("A1").Select
Loop Until Application.CountA(ActiveCell.EntireRow) = 0
End Sub
Function RemoveAllNonNums(myCell As String)
Dim myChar As String
Dim x As Integer
Dim i As String
i = ""
For x = 1 To Len(myCell)
myChar = Mid(myCell, x, 1)
If Asc(myChar) >= 48 And _
Asc(myChar) <= 57 Then
i = i & myChar
End If
Next
RemoveAllNonNums = Val(i)
End Function
答案1
得分: 2
你需要将函数的返回值分配回单元格:
Sub Phonetest1()
Dim c As Range
Set c = ActiveCell
Do While Application.CountA(c.EntireRow) > 0
c.Value = RemoveAllNonNums(CStr(c.Value))
Set c = c.Offset(1) '下一行
Loop
End Sub
Function RemoveAllNonNums(myCell As String)
Dim myChar As String, x As Long, i As String
i = ""
For x = 1 To Len(myCell)
myChar = Mid(myCell, x, 1)
If Asc(myChar) >= 48 And Asc(myChar) <= 57 Then
i = i & myChar
End If
Next
RemoveAllNonNums = Val(i)
End Function
英文:
You need to assign the function's return value back to the cell:
Sub Phonetest1()
Dim c As Range
Set c = ActiveCell
Do While Application.CountA(c.EntireRow) > 0
c.Value = RemoveAllNonNums(CStr(c.Value))
Set c = c.Offset(1) 'next row
Loop
End Sub
Function RemoveAllNonNums(myCell As String)
Dim myChar As String, x As Long, i As String
i = ""
For x = 1 To Len(myCell)
myChar = Mid(myCell, x, 1)
If Asc(myChar) >= 48 And Asc(myChar) <= 57 Then
i = i & myChar
End If
Next
RemoveAllNonNums = Val(i)
End Function
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论