vba - 摆脱 VBA 代码中的 .Select

标签 vba excel

我想删除下面代码中的 sht2.Selectsht2.Range("B2").Select 。有办法做到这一点吗?

Sub Remaining()

Dim sht2 As Worksheet    
Dim cell As Range

Set sht2 = ThisWorkbook.Worksheets("Sheet2")

sht2.Select
sht2.Range("B2").Select    
With sht2
    For Each cell In .Range("B2", Cells(Rows.Count, "B").End(xlUp))
        If .Range("A:A").Find(What:=cell.Value2, LookAt:=xlWhole) Is Nothing Then
           Intersect(.UsedRange, cell.EntireRow).Offset(, 1).Copy Sheets("Sheet1").Cells(Rows.Count, "L").End(xlUp).Offset(1)
           cell.Interior.Color = vbYellow
       End If
    Next cell
End With

End Sub

最佳答案

Sub Remaining()

    Dim sht2 As Worksheet
    Dim cell As Range

    Set sht2 = ThisWorkbook.Worksheets("Sheet2")

    With sht2
        'A few fixes in the following line to make sure everything
        '   is referencing the correct sheet
        For Each cell In .Range(.Range("B2"), .Cells(.Rows.Count, "B").End(xlUp))
            If .Range("A:A").Find(What:=cell.Value2, LookAt:=xlWhole) Is Nothing Then
               Intersect(.UsedRange, cell.EntireRow).Offset(, 1).Copy _
                      Sheets("Sheet1").Cells(Rows.Count, "L").End(xlUp).Offset(1)
               cell.Interior.Color = vbYellow
           End If
        Next cell
    End With

End Sub

关于vba - 摆脱 VBA 代码中的 .Select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42166938/

相关文章:

vba - 从 For-Next 循环中前进到下一项

python - 在 Python 中循环 excel 工作表和行

vba - 使用数据透视表从 SAP 导出 Excel 文件

excel - VBA二进制文件-cnc程序

c# - 如何限制excel采用默认时间格式

excel - 在非事件工作表上写入多个条目的更好结构

sql-server - 如何在 ADODB 连接字符串中设置 "Application Name"

excel - 如何创建输出一系列单元格内容的 Excel 函数

VBA - 根据另一列中的连续日期查找一列中的更改条件值

vba - Outlook 约会 - 如何更改开始时间下拉列表中的项目