范围。突然更换超出范围

问题描述 投票:0回答:1

简单的 sub 用于清除给定范围内橙色格式的单元格中的内容。 它以前按预期工作,但现在清除整个工作表中具有匹配格式的单元格,而不仅仅是在定义的范围内。

我尝试将

ActiveSheet.Range(r.Address).Replace "", "", searchformat:=True
更改为
r.Replace "", "", searchformat:=True
,但这会产生相同的行为。

代码:

Sub ClearOrangeCellsRng(r As Range)
'by range
  Application.FindFormat.Clear
  Application.FindFormat.Interior.ColorIndex = 40
  ActiveSheet.Range(r.Address).Replace "", "", searchformat:=True
  Application.FindFormat.Clear
End Sub

无论其价值如何,子程序都是从以下位置调用的:

Sub BlockTemplate(n As Integer)

Dim strBlock As String
Dim nmTempl As String
Dim tblBuild, templData As ListObject
Dim BuildSht, templSht As Worksheet
Dim nTemplRows As Integer


Set BuildSht = ActiveSheet
Set templSht = ActiveWorkbook.Sheets("tblTemplates")

BuildSht.Unprotect

strBlock = "Build" & n & "Templ"
nmTempl = BuildSht.Range(strBlock).Value


Set tblBuild = BuildSht.ListObjects("BuildTbl" & n)
Set templData = templSht.ListObjects("tblTemplates")
 
With templData
    .AutoFilter.ShowAllData
    .Range.AutoFilter field:=1, Criteria1:=nmTempl
    
End With

nTemplRows = templData.DataBodyRange.Columns(1).SpecialCells(xlCellTypeVisible).Count
    
'Check if template table has more rows than build table, if so, add rows to build table
rowsdif = nTemplRows - (tblBuild.Range.Rows.Count - 1)
If rowsdif > 0 Then
    For i = 1 To rowsdif
        tblBuild.ListRows.Add
    Next
End If
        

ClearOrangeCellsRng (tblBuild.Range)

templData.ListColumns("Display Name").DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
tblBuild.ListColumns("Description").DataBodyRange(1).PasteSpecial xlPasteValues

templData.ListColumns("Qty").DataBodyRange.SpecialCells(xlCellTypeVisible).Copy
tblBuild.ListColumns("Qty per instance").DataBodyRange(1).PasteSpecial xlPasteValues


templData.AutoFilter.ShowAllData
BuildSht.Protect

End Sub
excel vba
1个回答
0
投票

我测试了 ClearOrangeCellsRng - 它根本不起作用!

所以,有必要改变它:

Sub ClearOrangeCellsRng(r As Range)
'by range
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
  Application.ReplaceFormat.Interior.ColorIndex = -4142
  Application.FindFormat.Interior.ColorIndex = 40
  ActiveSheet.Range(r.Address).Replace "", "", searchformat:=True
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
End Sub

现在它可以按预期工作,并且不会超出范围限制。

© www.soinside.com 2019 - 2024. All rights reserved.