Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2727|回复: 16

[已解决]去字母

[复制链接]
发表于 2013-6-17 18:55 | 显示全部楼层 |阅读模式
把所有字母去掉。
最佳答案
2013-6-17 22:03
不好意思,我这没测。
  1. Sub 方法3()

  2.     Dim i As Long, j As Long, k As Long
  3.     Dim arr(), result()

  4.     Application.ScreenUpdating = False
  5.     Application.DisplayAlerts = False
  6.     Application.EnableEvents = False
  7.     Application.Calculation = xlCalculationManual


  8.     If TypeName(Selection) <> "Range" Then
  9.         MsgBox "所选对象非单元格"
  10.         GoTo Quit
  11.     End If
  12.     Dim objRegExp As Object
  13.     Set objRegExp = CreateObject("VBScript.regExp")
  14.     With objRegExp
  15.         .Global = True
  16.         .Pattern = "[a-z,A-Z]"

  17.         For k = 1 To Selection.Areas.Count
  18.             '防止区域只有一个单元格,这样赋值就不存在数组一说了。
  19.             If Selection.Areas(k).Count = 1 Then
  20.                 Selection.Areas(k).Value = .Replace(Selection.Areas(k).Value, "")
  21.             Else
  22.                 arr = Selection.Areas(k).Value
  23.                 ReDim result(1 To UBound(arr), 1 To UBound(arr, 2))
  24.                 For i = LBound(arr) To UBound(arr)
  25.                     For j = LBound(arr, 2) To UBound(arr, 2)
  26.                         If .test(arr(i, j)) Then
  27.                             result(i, j) = .Replace(arr(i, j), "")
  28.                         Else
  29.                             result(i, j) = arr(i, j)
  30.                         End If
  31.                     Next
  32.                 Next
  33.                 Selection.Areas(k).Value = result
  34.             End If
  35.         Next
  36.     End With
  37.     MsgBox "OK"

  38. Quit:
  39.     Application.ScreenUpdating = True
  40.     Application.DisplayAlerts = True
  41.     Application.EnableEvents = True
  42.     Application.Calculation = xlCalculationAutomatic
  43. End Sub
复制代码

去字母.rar

1.82 KB, 下载次数: 4

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-6-17 19:02 | 显示全部楼层
  1. Function rp(rg) As String
  2.     Dim objRegExp As Object
  3.    
  4.     Set objRegExp = CreateObject("VBScript.regExp")
  5.     rp = rg
  6.     With objRegExp
  7.         .Global = True
  8.         .Pattern = "[a-z,A-Z]"
  9.             If .test(rg) Then
  10.                 rp = .Replace(rg, "")
  11.             End If
  12.     End With
  13.     Set objRegExp = Nothing
  14. End Function
复制代码
回复

使用道具 举报

发表于 2013-6-17 19:02 | 显示全部楼层
回复

使用道具 举报

发表于 2013-6-17 19:08 | 显示全部楼层
看看是否符合你的要求。。。

去字母.rar

8.63 KB, 下载次数: 1

回复

使用道具 举报

发表于 2013-6-17 19:08 | 显示全部楼层
  1. Sub 方法2()
  2.     Dim lLastRow&
  3.     Dim i As Long
  4.     Dim arr(), result()
  5.    
  6.     Application.ScreenUpdating = False
  7.     Application.DisplayAlerts = False
  8.     Application.EnableEvents = False
  9.     Application.Calculation = xlCalculationManual
  10.    
  11.    
  12.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  13.     arr = Range("a2:b" & lLastRow).Value
  14.     ReDim result(1 To UBound(arr), 1 To 1)

  15.     Dim objRegExp As Object
  16.     Set objRegExp = CreateObject("VBScript.regExp")
  17.     With objRegExp
  18.         .Global = True
  19.         .Pattern = "[a-z,A-Z]"
  20.         For i = LBound(arr) To UBound(arr)
  21.             If .test(arr(i, 1)) Then
  22.                 result(i, 1) = .Replace(arr(i, 1), "")
  23.             Else
  24.                 result(i, 1) = arr(i, 1)
  25.             End If
  26.         Next
  27.     End With
  28.     Range("f2").Resize(UBound(arr)).Value = result
  29.     Application.ScreenUpdating = True
  30.     Application.DisplayAlerts = True
  31.     Application.EnableEvents = True
  32.     Application.Calculation = xlCalculationAutomatic
  33.     MsgBox "OK"
  34. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-17 19:11 | 显示全部楼层
  1. Sub 方法2()
  2.     Dim lLastRow&
  3.     Dim i As Long
  4.     Dim arr(), result()
  5.    
  6.     '关闭屏幕更新,显示警告,事件,手动计算
  7.     Application.ScreenUpdating = False
  8.     Application.DisplayAlerts = False
  9.     Application.EnableEvents = False
  10.     Application.Calculation = xlCalculationManual
  11.    
  12.    
  13.     '取A列数据到数组
  14.     lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
  15.     arr = Range("a2:b" & lLastRow).Value
  16.     ReDim result(1 To UBound(arr), 1 To 1)

  17.     '正则处理
  18.     Dim objRegExp As Object
  19.     Set objRegExp = CreateObject("VBScript.regExp")
  20.     With objRegExp
  21.         .Global = True
  22.         '匹配规则为字母
  23.         .Pattern = "[a-z,A-Z]"
  24.         For i = LBound(arr) To UBound(arr)
  25.             If .test(arr(i, 1)) Then
  26.                 '替换字母
  27.                 result(i, 1) = .Replace(arr(i, 1), "")
  28.             Else
  29.                 result(i, 1) = arr(i, 1)
  30.             End If
  31.         Next
  32.     End With
  33.     Set objRegExp = Nothing
  34.    
  35.     '对之前关闭的属性进行恢复打开
  36.     Range("f2").Resize(UBound(arr)).Value = result
  37.     Application.ScreenUpdating = True
  38.     Application.DisplayAlerts = True
  39.     Application.EnableEvents = True
  40.     Application.Calculation = xlCalculationAutomatic
  41.     MsgBox "OK"
  42. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-6-17 19:31 | 显示全部楼层
hwc2ycy 发表于 2013-6-17 19:08

首先谢谢,但是如果数据有多个区域且区域不连续,我改用Selection属性,就是选中后执行,为什么不能,看看哪里改错了?
  1. Sub 方法2()
  2.     Dim lLastRow&
  3.     Dim i As Long
  4.     Dim arr(), result()
  5.    
  6.     Application.ScreenUpdating = False
  7.     Application.DisplayAlerts = False
  8.     Application.EnableEvents = False
  9.     Application.Calculation = xlCalculationManual
  10.    
  11.    
  12.     lLastRow = Cells(Rows.Count, Columns.Count).End(xlUp).Row
  13.     arr = Selection
  14.     ReDim result(1 To UBound(arr), 1 To Columns.Count)

  15.     Dim objRegExp As Object
  16.     Set objRegExp = CreateObject("VBScript.regExp")
  17.     With objRegExp
  18.         .Global = True
  19.         .Pattern = "[a-z,A-Z]"
  20.         For i = LBound(arr) To UBound(arr)
  21.             If .test(arr(i, Columns.Count)) Then
  22.                 result(i, Columns.Count) = .Replace(arr(i, Columns.Count), "")
  23.             Else
  24.                 result(i, Columns.Count) = arr(i, Columns.Count)
  25.             End If
  26.         Next
  27.     End With
  28.     Selection.Resize(UBound(arr)).Value = result
  29.     Application.ScreenUpdating = True
  30.     Application.DisplayAlerts = True
  31.     Application.EnableEvents = True
  32.     Application.Calculation = xlCalculationAutomatic
  33.     MsgBox "OK"
  34. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-17 21:28 | 显示全部楼层
  1. Sub 方法2()

  2.     Dim rg As Range
  3.     Dim objRegExp As Object

  4.     Application.ScreenUpdating = False
  5.     Application.DisplayAlerts = False
  6.     Application.EnableEvents = False
  7.     Application.Calculation = xlCalculationManual

  8.     If TypeName(Selection) > "Range" Then
  9.         MsgBox "所选对象非单元格"
  10.         GoTo Quit
  11.     End If

  12.     Set objRegExp = CreateObject("VBScript.regExp")
  13.     With objRegExp
  14.         .Global = True
  15.         .Pattern = "[a-z,A-Z]"
  16.         For Each rg In Selection
  17.             If .test(rg.Value) Then rg.Value = .Replace(rg.Value, "")
  18.         Next
  19.     End With
  20.     MsgBox "OK"
  21. Quit:
  22.     Application.ScreenUpdating = True
  23.     Application.DisplayAlerts = True
  24.     Application.EnableEvents = True
  25.     Application.Calculation = xlCalculationAutomatic

  26. End Sub
复制代码
回复

使用道具 举报

发表于 2013-6-17 21:29 | 显示全部楼层
如果是多区域,你想用数组的话,就得用Selection.areas来做处理。
多区域对象有,直接用SELECTION.VALUE只会是区域1赋值给数组。
回复

使用道具 举报

 楼主| 发表于 2013-6-17 21:30 | 显示全部楼层
hwc2ycy 发表于 2013-6-17 21:29
如果是多区域,你想用数组的话,就得用Selection.areas来做处理。
多区域对象有,直接用SELECTION.VALUE只 ...

怎么改才对?请明示?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 04:57 , Processed in 0.397547 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表