簡體   English   中英

VBA錯誤13類型不匹配

[英]VBA error 13 type missmatch

我在五個不同的工作簿上使用三個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)”和一個錯誤13類型。 任何想法如何解決這個問題?

2)有人為我提供了這一行:

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

有什么方法可以找到數據列表中最后填充的行的編號嗎?

  1. jbs被聲明為Date 當您收到錯誤消息時, .Cells(cRow,2)引用的單元格不包含日期或可以轉換為日期的浮點數。

假設你不關心什么樣的您正在尋找在第2列的值,則更換:

            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”列中查找最后使用的行,請使用: lngLastRow = .columns(2).Find(What:="*", LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
那應該擺脫循環錯誤,因為它將在應有的時候正確停止。


資料來源:

  1. Application.Columns屬性
  2. 范圍查找方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM