【问题标题】:Loop to find cell, then using that cell reference to clear a range循环查找单元格,然后使用该单元格引用清除范围
【发布时间】:2018-09-08 13:02:13
【问题描述】:

我正在尝试编写一些代码,它将在前 30 列和行中搜索单词 TotalArea。我希望将这些单词的位置存储在一个变量中,然后使用这些变量来清除与它们相关的范围,然后循环遍历所有工作表。

我尝试使用我在网上找到的数字到字母转换器来存储列号,我认为这就是我的问题所在。

这是我在网上找到的代码:

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

和我的代码:

Private Sub Clear_Click()
    Dim LastRowH As Integer
    Dim ClearContent As Boolean
    Dim ws As Worksheet
    Dim testrange As Range
    Dim Cell1 As Range
    Dim Celln As Range
    ClearContent = False
    For Each ws In ActiveWorkbook.Worksheets
        'FINDS RANGE
        For i = 1 To 30
            For j = 1 To 30
                If ActiveWorkbook.Sheets(ws).Range(Col_Letter(CLng(i)) & j).Value = "Total" Then
                    Cell1 = ws.Range(Col_Letter(CLng(i + 1)) & j)
                End If
                If ActiveWorkbook.Sheets(ws).Range(Col_Letter(CLng(i)) & j).Value = "Area" Then
                    Celln = ws.Range(Col_Letter(CLng(i + 1)) & j - 1)
                End If
            Next
        Next
        '...<more code here>...
        If ClearContent = True Then
            '...<more code here>...
            ws.Range(Cell1 & ":" & Celln).ClearContents
        End If
    Next ws
End Sub

当我运行代码时,我收到错误消息:

运行时错误“13”:类型不匹配

我尝试了其他几种方法,但无法正常工作。

任何帮助表示赞赏,在此先感谢:)

更新 我曾尝试将代码中的for循环替换为使用“Cells”函数,如下:

For i = 1 To 30
            For j = 1 To 30
            If Sheets(ws).Cells(j, i).Value = "Total" Then
                    Set Cell1 = ws.Cells(j - 1, i + 1)
                End If
                If Sheets(ws).Cells(j, i).Value = "Area" Then
                    Set Celln = ws.Cells(j, i + 1)
                End If
            Next
        Next

但我仍然收到类型不匹配

【问题讨论】:

  • 有几种方法可以将列号转换为字母。这是 one of my answers 的帖子,还有 31 个其他答案。 (我认为这是最简单的方法,但我有偏见!:-)
  • 你已经有了ij,所以你可以使用Cells(j,i)而不是Range(Col_Letter(CLng(i)) &amp; j)
  • 我尝试使用 Cells(j,i) 方法,但是当它不允许我将其分配给变量时
  • 另外@ashleedawg 我已经看过你在另一个线程上的建议,但我不确定如何将它应用到我自己的代码中
  • 看起来主要问题是您的范围对象和字符串变量不匹配。您将 Cell1Celln 定义为 Range 对象,但稍后尝试将它们用作字符串变量。决定你要走哪条路,如果你想把它们用作 Range 变量,记得在分配它们时使用Set 关键字,然后直接引用它们,比如Range(Cell1, Celln).ClearContents

标签: excel vba for-loop cell named-ranges


【解决方案1】:

您的类型不匹配是由于ActiveWorkbook.Sheets(ws).Range ws 是工作表,而不是索引或名称。 ws.range 将扫描该工作表的范围。其他修改很少见 cmets。

Function Col_Letter(lngCol As Long) As String
    Dim vArr
    vArr = Split(Cells(1, lngCol).Address(True, False), "$")
    Col_Letter = vArr(0)
End Function

Sub test()
    Dim LastRowH As Integer
    Dim ClearContent As Boolean
    Dim ws As Worksheet
    Dim testrange As Range
    Dim Cell1 As Range
    Dim Celln As Range
    ClearContent = False
    For Each ws In ActiveWorkbook.Worksheets
        'FINDS RANGE
        For i = 1 To 30
            For j = 1 To 30
                If ws.Range(Col_Letter(CLng(i)) & j).Value = "Total" Then
                    Set Cell1 = ws.Range(Col_Letter(CLng(i + 1)) & j)  ' Set This
                End If
                If ws.Range(Col_Letter(CLng(i)) & j).Value = "Area" Then
                    Set Celln = ws.Range(Col_Letter(CLng(i + 1)) & j - 1)  ' Set This
                End If
            Next
        Next
        '...<more code here>...
        'ClearContent = True    ' Me Testing
        If ClearContent = True Then
            '...<more code here>...
            Cell1.ClearContents
            Celln.ClearContents

            'ws.Range(Cell1 & ":" & Celln).ClearContents  ' don't think this will work properly
        End If
    Next ws
End Sub

【讨论】:

  • 我已将您的更改添加到我的代码中,但似乎我仍然收到类型不匹配错误,这行代码高亮显示:'If ws.Range(Col_Letter(CLng(i)) & j ).Value = "Total" 然后'
猜你喜欢
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
相关资源
最近更新 更多