无法在Excel VB中找到包含内容的最后一行

时间:2017-11-09 21:50:56

标签: excel vba excel-vba

所以我需要找到两个不同列的最后一行。 VBA代码适用于第一列,但随后返回:

  

运行时错误'1004':
  应用程序定义或对象定义的错误。

相关变量在此处建立:

Set wsSource(0) = .Worksheets("CDF")
lStart = 6
Set wsTarget(1) = .Worksheets("Claim Edit Analysis")
lColumn = wsSource(0).Rows(1).Find(What:=lIDNumber, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False).Column
lColumnTwo = wsSource(0).Rows(1).Find(What:=lIDNumber2, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False).Column
lBillCol = wsSource(0).Rows(1).Find(What:=lBillNumber, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False).Column
lRow = 6
lCount = 0

然后我打电话给公共子

Call CDFExtract(lStart, wsSource(0), rnSource, wsTarget(Target), rnTarget, lColumn, lColumnTwo, lBillCol, lRow, lCount)

Public Sub进来,这就是出现错误的地方:

Public Sub CDFExtract(ByVal lStart As Double, ByRef wsSource As Worksheet, ByVal rnSource As Range, ByRef wsTarget As Worksheet, ByVal rnTarget As Range, ByVal lColumn As Double, ByVal lColumnTwo As Double, ByVal lBillCol As Double, ByVal lRow As Double, ByRef lCount As Double)

Dim iRowBrief As Long
Dim iRowBriefTwo As Long

    With wsSource
      iRowBrief = .Cells(Rows.Count, lColumn).End(xlUp).Row
      iRowBriefTwo = .Cells(Rows.Count, lColumnTwo).End(xlUp).Row
   End With

wsSource =源工作表

iRowBrief =存储第一列最后一行的变量

lColumn =找到最后一行的第一列

iRowBrief2 =第二列试图存储的最后一行的变量

lColumnTwo =需要找到最后一行的第二列

正如您所看到的,代码完全相同。为什么我收到此错误?

1 个答案:

答案 0 :(得分:0)

你需要在rows.count前加点吗?

With wsSource

        iRowBrief = .Cells(.Rows.Count, lColumn).End(xlUp).Row
        iRowBriefTwo = .Cells(.Rows.Count, lColumnTwo).End(xlUp).Row

End With