简体   繁体   中英

VBA error 13 type missmatch

I'm working with three 500K+ rows sheets on five different workbooks and on my way to extracting the data I need I came up with the following code:

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

This does what I need. I just modify it little by little to make the processing of the five workbooks faster. Here I extract the data in a new sheet on the same workbook.

1) It seems to work but after the whole process is done I keep getting " jbs = .Cells(cRow, 2) " highlighted and an error 13 type. Any idea how to fix this?

2) Someone provided me with this line:

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

Is there any way to find the number of the last filled row in a list of data?

  1. jbs is declared a Date . When you get your error, .Cells(cRow,2) is referring to a cell that does not contain a date or a floating point number that could be converted to a date.

Assuming that you don't care what kind of values you're looking at in column 2, replace:

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

With:

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

That eliminates declaring jbs and the typing altogether.

  1. lngLastRow = .UsedRange.Rows.Count

Find row of the last used cell on the sheet

The code for lngLastRow you are using, will return a row of the last used cell on the sheet. Not necessarily last used cell in column "B" which you are comparing later in your code, for example here : If jbs <> .Cells(cRow - 1, 2).Value Then...

Find row of the last used cell in column "B"

If you want to find last used row in column "B" with this method use : lngLastRow = .columns(2).Find(What:="*", LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
That should get rid of the loop error, because it will properly stop when it should.


Sources :

  1. Application.Columns Property
  2. Range.Find Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM