Excel VBA如果声明只有可见的单元格

我正在尝试编写一个代码,在过滤字段上使用if语句,它将查找特定范围的单元格的第一个字母值,如果该字母落入特定范围,则要输出特定人员的首字母缩写。

我的代码是:

Sub NewFilter() Dim wbI As Workbook, wb0 As Workbook Dim wsI As Worksheet, ws0 As Worksheet Dim myRange As Range Dim outputRange As Range Dim i As Long, j As Long 'Establish my source of info worbooks and worksheet Set wbI = ThisWorkbook Set wsI = wbI.Sheets("Sheet1") 'Filters Original wb by criteria Selection.AutoFilter ActiveSheet.Range("A1").AutoFilter Field:=5, Criteria1:=Array( _ "Gur Insurance Plan 1", "Gur Insurance Plan 2", "Gur Insurance Plan 3", _ "Gur Insurance Plan 4", "Gur Insurance Pol No 1", "Gur Insurance Pol No 2", _ "Gur Insurance Pol No 3"), Operator:=xlFilterValues 'Establish and create new worbook where I will output my info Set wb0 = Workbooks.Add With wb0 'Save new workbook Set ws0 = wb0.Sheets("Sheet1") .SaveAs Filename:="New Soarian Test" 'Copy all visible cells in original workbook wsI.Cells.Copy 'Paste values and formatting rules into new workbook ws0.Range("A1").PasteSpecial Paste:=xlValues, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False ws0.Range("A1").PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False End With 'Add a new column into A called "Rep" ws0.Columns("A:A").InsertShift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove ws0.Range("A1").Value = "Rep" 'Filter Soarian Column by "Approved" Selection.AutoFilter ws0.Range("A1").AutoFilter Field:=7, Criteria1:=Array( _ "APPROVED"), Operator:=xlFilterValues 'Select and delete cells then unfilter all ws0.Range("A2:I1000").SpecialCells(xlCellTypeVisible).Select Application.DisplayAlerts = False Selection.Delete Application.DisplayAlerts = True ws0.AutoFilterMode = False 'Filter EncProv by Clincal Psych and Physcial Therapy ws0.Range("A1").AutoFilter Field:=3, Criteria1:=Array( _ "PHYSICAL THERAPY", "CLINIC PSYCH OUTPAT"), Operator:=xlFilterValues 'Select visible cells and delete them and then unfilter ws0.Range("A2:I1000").SpecialCells(xlCellTypeVisible).Select Application.DisplayAlerts = False Selection.Delete Application.DisplayAlerts = True ws0.AutoFilterMode = False 'Filter EncProv by NPL Endo ws0.Range("A1").AutoFilter Field:=3, Criteria1:=Array( _ "NPL ENDOCRINOLOGY"), Operator:=xlFilterValues ws0.Range("A1").Select Set myRange = Range("E2:E1000" & Cells(Rows.Count, "E").End (xlUp).Row).SpecialCells(xlCellTypeVisible) Set outputRange = Range("A2:A1000" & Cells(Rows.Count, "E").End(xlUp).Row).SpecialCells(xlCellTypeVisible) For i = 1 To myRange.Rows.Count For j = 1 To myRange.Columns.Count If Left(myRange.Cells(i, j).Value, 1) >= "A" And Left(myRange.Cells(i, j).Value, 1) <= "K" Then outputRange.Cells(i, j).Value = "MCJ" End If Next j Next i End Sub 

我大部分评论它更容易阅读,但我只关心代码的最后一部分,我设置我的范围。 我希望它只跟随应用filter后显示的单元格的if语句。 它的作品大部分是正确input人的姓名首字母,但是A列中仍然存在单元格,即使信件抬头在A和K之间,也没有正确填充。任何想法?

(我是VBA的新手,并意识到我的一些策略并不像我们的那样高效)

非常感谢你!