是否可以简化这些 With 语句?

时间:2021-04-09 23:45:29

标签: excel vba

VBA 新手,但我觉得这可以简化,只是不确定如何,因为我还在学习。

我想知道是否可以简化这些 With 语句:

With outputSheet.Range("C5:C" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:E,3,0)"
    .Value = .Value
End With

With outputSheet.Range("E5:E" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:G,5,0)"
    .Value = .Value
End With

With outputSheet.Range("F5:F" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:H,6,0)"
    .Value = .Value
End With

我正在考虑将 .Range 删除,但它给出了“无效使用属性错误”,想知道是否有人可以解释为什么它会给出错误。像这样:

With outputSheet
    .Range ("C5:C" & .Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:E,3,0)"
    .Range ("E5:E" & .Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:G,5,0)"
    .Value = .Value
End With

感谢您的任何帮助/反馈!

3 个答案:

答案 0 :(得分:2)

原因

With outputSheet
    .Range ("C5:C" & .Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:E,3,0)"
    .Range ("E5:E" & .Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:G,5,0)"
    .Value = .Value
End With

抛出一个错误是您正在引用一个范围,但没有对它做任何事情。此外,工作表没有 Values 属性

这可以修复为

With outputSheet
    With .Range("C5:C" & .Range("A" & .Rows.Count).End(xlUp).Row)
        .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:E,3,0)"
        .Value = .Value
    End With
    With .Range("E5:E" & .Range("A" & .Rows.Count).End(xlUp).Row)
        .Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:G,5,0)"
        .Value = .Value
    End With
End With

答案 1 :(得分:0)

使用 With 语句相对容易。将您要使用的对象放在开头语句中,然后用前导句点替换 End With 之前代码中该对象的每个重复项。而不是

lastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, 1).End.xlUp).Row
' you can say
With Worksheets("Sheet1")
    lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
End With

但是,您的代码患有另一种疾病。您尝试将单元格范围寻址为 Range。然后你就会对语法感到困惑。如果您将单个单元格作为 Cells 集合(语法:Cells([Row], [Column])的成员,并且仅按范围名称来处理多个单元格的范围,那么它会更容易。然后,请记住,范围虽然以其名称命名和寻址(“A1:D4”是一个名称,因为它显示为字符串),但由其第一个和最后一个单元格定义。由于单元格由 2 个数字、行和列定义,因此您也可以不使用任何字符串表达式来定义范围,这使得编程变得更加容易。

您的代码的另一个问题是 VLOOKUP 的预期用途是首先检索一个值,然后将其替换为它检索到的值。为什么不简单地检索值并将其写入单元格。查找公式不应该出现在单元格中。但是,如果您考虑使用该方法,则不会使用它 3 次。相反,您会找到数据来自的行并从该行中选择 3 个值。这就是下面的代码所做的。

Sub Snippet()
    ' 216

    Dim Rng     As Range            ' Lookup range in dataSheet
    Dim Fnd     As Range            ' Cell where match was found
    Dim R       As Long             ' Loop counter: rows
    Dim LookUp  As Variant          ' return of VLOOKUP
    
    Set Rng = dataSheet.Columns("C:C")
    Application.ScreenUpdating = False
    
    With outputSheet
        ' Observe that both the 'Cells' and 'Rows.Count' are on
        ' the sheet specified by the 'With' statement.
        ' Therefore both require the leading period.
        For R = 5 To .Cells(.Rows.Count, "A").End(xlUp).Row
            Set Fnd = Rng.Find(.Cells(R, "A").Value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not Fnd Is Nothing Then
                .Cells(R, "C").Value = dataSheet.Cells(Fnd.Row, "E").Value
                .Cells(R, "E").Value = dataSheet.Cells(Fnd.Row, "G").Value
                .Cells(R, "F").Value = dataSheet.Cells(Fnd.Row, "H").Value
            End If
        Next R
    End With
    Application.ScreenUpdating = True
End Sub

我不确定从 dataSheet 收集正确的值并将它们分配给 outputSheet 上的正确单元格。然而,由于我用于寻址单元格的简化语法,您将能够立即进行任何必要的调整。

我只是看到您有 3 个以上的值可供选择。您可以轻松扩展我的系统。但是,请考虑创建一个循环而不是 10 条几乎相同的行来传输数据。

答案 2 :(得分:-1)

这看起来更“干净”吗?我假设 .Value =.Value 语句旨在用值替换公式。

Dim lastRow As Long
Dim r As Range

lastRow = outputSheet.Columns(1).Range("A" & Rows.Count).End(xlUp).Row

Set r = outputSheet.Range("C5:C" & lastRow)
r.Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:E,3,0)"
r.Copy
r.PasteSpecial xlPasteValues

Set r = outputSheet.Range("E5:E" & lastRowRow)
r.Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:G,5,0)"
r.Copy
r.PasteSpecial xlPasteValues

Set r = outputSheet.Range("F5:F" & lastRowRow)
r.Formula = "=VLOOKUP(A5,'" & dataSheet.Name & "'!C:H,6,0)"
r.Copy
r.PasteSpecial xlPasteValues