【问题标题】:How to remove modify a string to remove letters from a new worksheet如何删除修改字符串以从新工作表中删除字母
【发布时间】:2018-12-25 07:22:12
【问题描述】:

所以我一直在尝试创建一个宏来格式化打开一个新的工作文件,然后修改 1 列以从数字字符串中删除字母(例如,20180717a 变为 20180717)。 我尝试了几件事并想出了这段代码,但它似乎无法正常工作:

Sub Creating_progress_reports()

    Dim wb As Workbook
    Dim myfilename As String

    myfilename = "xxxx.xlsx"
    Set wb = Workbooks.Open(myfilename)

    Dim str1 As String
    Dim str2 As String
    Dim rngTemp As Range
    Dim rngCell As Range

    str1 = "a"
    str2 = "b"
    str3 = "c"
    str4 = "d"

    With Workbooks("xxxx.xlsx").Sheets("xxxx")
    Set rngTemp = Columns(6).CurrentRegion

    For Each rngCell In rngTemp

    If InStr(1, rngCell, str1) > 0 Then
        rngCell = Replace(rngCell.Value, str1, "")
    End If

    If InStr(1, rngCell, str2) > 0 Then
        rngCell = Replace(rngCell.Value, str2, "")
    End If

    If InStr(1, rngCell, str1) > 0 Then
        rngCell = Replace(rngCell.Value, str3, "")
    End If
        If InStr(1, rngCell, str1) > 0 Then
        rngCell = Replace(rngCell.Value, str4, "")
    End If
    Next rngCell
    End With
End Sub

错误是vba代码通过,但结果不存在。字符串中的字母还在。

【问题讨论】:

  • 它不能正常工作并不是对问题的真正解释;)你能说出错误发生在哪里吗?什么不工作?
  • 脚本通过,但结果不存在。字符串中的字母还在

标签: vba excel letters


【解决方案1】:

Columns(6).CurrentRegion 之前需要一个. 来引用Workbooks("xxxx.xlsx").Sheets("xxxx")

With Workbooks("xxxx.xlsx").Sheets("xxxx")
    Set rngTemp = .Columns(6).CurrentRegion

否则是指以下之一:

  • 代码所在的Sheet
  • ActiveSheet,如果代码不在Sheet 内;

【讨论】:

  • 谢谢。我试过了,但它似乎仍然无法正常工作
  • @Cunneryn - 我猜您在第 6 列中有一些空单元格,因此 CurrentRegion 不是最佳选择。尝试在Set rngTemp = .Col... 行之后写MsgBox rngTemp.Address,看看是否是这种情况。
  • 该列中肯定有空单元格是的。是否有其他功能更适合这种情况?
  • @Cunneryn - 还有很多其他方法。最好的情况是找到最低的单元格并根据它定义范围 - stackoverflow.com/questions/11926972/…rondebruin.nl/win/s9/win005.htm
  • 谢谢,我会试试这个方法,希望它的作品
猜你喜欢
  • 2021-06-11
  • 2020-03-20
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
相关资源
最近更新 更多