VBA错误13types错配

我正在用五个不同的工作簿上的三个500K +工作表和我的方式来提取我需要的数据我想出了以下代码:

Sub Macro3() Dim lngFirstRow As Long, lngLastRow As Long, cRow As Long, lngNextDestRow As Long Dim jbs As Date Dim shSrc As Worksheet, shDest As Worksheet Set shDest = ActiveWorkbook.Sheets("Sheeet1") '''Feuille de destination (sheetDestination) lngNextDestRow = 2 For Each shSrc In ThisWorkbook.Worksheets Nom = shSrc.Name If Nom <> "Sheeet2" Then With shSrc lngFirstRow = 2 lngLastRow = .Cells.Find(What:="*", after:=.Cells.Cells(1), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row For cRow = lngFirstRow To lngLastRow Step 1 jbs = .Cells(cRow, 2) If jbs <> .Cells(cRow - 1, 2).Value Then .Range("B" & cRow).Copy Destination:=shDest.Range("A" & lngNextDestRow) .Range("D" & cRow).Copy Destination:=shDest.Range("B" & lngNextDestRow) .Range("D" & cRow + 1).Copy Destination:=shDest.Range("C" & lngNextDestRow) .Range("E" & cRow).Copy Destination:=shDest.Range("D" & lngNextDestRow) .Range("E" & cRow + 1).Copy Destination:=shDest.Range("E" & lngNextDestRow) .Range("F" & cRow).Copy Destination:=shDest.Range("F" & lngNextDestRow) .Range("F" & cRow + 1).Copy Destination:=shDest.Range("G" & lngNextDestRow) lngNextDestRow = lngNextDestRow + 1 End If Next cRow End With End If Next shSrc End Sub 

这就是我所需要的。 我只是一点一点地修改它,使五个工作簿的处理速度更快。 在这里,我在同一个工作簿中提取新工作表中的数据。

1)它似乎工作,但整个过程完成后,我不断得到“jbs = .Cells(cRow,2)”突出显示和错误13types。 任何想法如何解决这个问题?

2)有人向我提供这一行:

 lngLastRow = .Cells.Find(What:="*", after:=.Cells.Cells(1), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row 

有没有办法find数据列表中最后一个填充行的数量?

  1. jbs被声明为一个Date 。 当你得到你的错误时.Cells(cRow,2)指的是一个单元格,它不包含可以转换为date的date或浮点数。

假设你不关心你在第2列中看到什么的值,replace:

  jbs = .Cells(cRow, 2) If jbs <> .Cells(cRow - 1, 2).Value Then 

附:

  If .Cells(cRow, 2) <> .Cells(cRow - 1, 2) Then 

这消除了声明jbs和完全打字。

  1. lngLastRow = .UsedRange.Rows.Count

在工作表上查找上次使用的单元格的行

您使用的lngLastRow的代码将返回表单上最后一个使用的单元格的一行。 不一定是最后一次在列“B”中使用的单元格,您将在代码中稍后进行比较,例如: If jbs <> .Cells(cRow - 1, 2).Value Then...

查找列“B”中最后使用的单元格的行

如果你想用这个方法在列“B”中find最后使用的行,使用: lngLastRow = .columns(2).Find(What:="*", LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
这应该摆脱循环错误,因为它会适当地停止时,应该。


来源:

  1. Application.Columns属性
  2. Range.Find方法