[英]Method 'Range' of object'_Worksheet' failed |Excel|VBA|
错误:对象“ _Worksheet”的方法“范围”失败
Dim Ws As Worksheet
For Each Ws In Sheets(Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))
Ws.Range("I9:AM68" & Rows.Count).Replace What:="AP", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="BL", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="MA", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="PA", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="PL", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="SL", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="UP", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="H1", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="H2", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="TR", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="WH", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="NJ", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="AL", Replacement:="", LookAt:=xlWhole
Ws.Range("I9:AM68" & Rows.Count).Replace What:="SUP", Replacement:="", LookAt:=xlWhole
Next Ws
如果在每个工作表中找到匹配项,则数据已清除=>在范围中有条件格式=> I到AM
这是我尝试不起作用的另一个代码
Dim Ws As Worksheet, lr As Long, c As Range
For Each Ws In Sheets(Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"))
lr = Ws.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row
For Each c In Ws.Range("I9:AM68" & lr)
Select Case UCase(c.Value)
Case "SUP", "AP", "AL"
c.Value = ""
c.Interior.ColorIndex = xlNone
End Select
Next
Next Ws
这是我的Excel的图像
[我的图片] [1]: https : //i.stack.imgur.com/yDLj4.png
下面的这对我来说很好,但不能将像元颜色设置为正常的像没有填充颜色的像元颜色
'what:=UCase("SUP"), Replacement:="", ReplaceFormat:=True
Dim Ws As Worksheet
For Each Ws In Sheets(Array("Sheet1", "Sheet2", "Sheet3"))
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("SUP"), Replacement:="", ReplaceFormat:=True
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("SUP"), Replacement:="", ReplaceFormat:=False
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("AP"), Replacement:="", ReplaceFormat:=True
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("AP"), Replacement:="", ReplaceFormat:=False
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("AL"), Replacement:="", ReplaceFormat:=True
Ws.Range("A4:K" & Rows.Count).Replace what:=UCase("AL"), Replacement:="", ReplaceFormat:=False
Next Ws
这是在上面的代码[输出] [1]的输出下面: https : //i.stack.imgur.com/H09ab.png
假设您尝试获取该范围内的最后一行,而不是实际的rows.count
,请参见下面的一种实现这些替换的方法。
Option Explicit
Sub doSomeReplacements()
Application.ScreenUpdating = False
Dim wb As Workbook: Set wb = ThisWorkbook 'set your workbook variable
Dim Ws As Worksheet
Dim shNames() As String: shNames = Split("Sheet1,Sheet2,Sheet3", ",") 'allocate sheet names to an array
Dim strReplaces() As String: strReplaces = Split("AP,BL,MA,PA,PL,SL,UP,H1,H2,TR,WH,NJ,AL,SUP", ",") 'allocate replacement strings to an array
Dim lastRow As Long, X As Long, Z As Long, R As Long, C As Long
For X = LBound(shNames) To UBound(shNames) 'Loop through the array of sheet names
On Error Resume Next
Set Ws = wb.Sheets(shNames(X))
On Error GoTo 0
If Not Ws Is Nothing Then 'If there is a worksheet to work with....
With Ws
'lastRow = .Cells(.Rows.Count, "I").End(xlUp).Row 'get last row at column "I"
lastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row 'alternative last row
For R = 1 To lastRow
For C = 1 To 11 '"A:K"
For Z = LBound(strReplaces) To UBound(strReplaces) 'Loop through the array of replacements and apply the replacement
With .Cells(R, C)
If .Value = strReplaces(Z) Then
.Value = ""
.Interior.ColorIndex = xlNone
End If
End With
Next Z
Next C
Next R
End With
End If
Set Ws = Nothing
Next X
Application.ScreenUpdating = True
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.