Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 90317

Mismatch error in VBA - problems with columns

$
0
0

I have a workbook where I want to find the differences of two sheets by looking at the company name and their corporate registration number and then type the differences on the third sheet.

I have tried the code in another workbook with only 143 rows, which works perfectly, but when I try it on the real workbook with 10,000 rows I get a "type mismatch error". Also if I use other columns than the CVR and Firm columns the code also works.

The CVR is numbers and Firms are strings (firm names). I get the

type mismatch error

on the line I marked **. Does somebody know why I get this error?

Sub ComCVR()
Dim CVR1()
Dim CVR2()
Dim Firm1()
Dim Firm2()
Dim n As Long, m As Long
Dim i As Double, j As Double
Dim intCurRow1 As Integer, intCurRow2 As Integer
Dim rng As Range, rng1 As Range

Set rng = ThisWorkbook.Sheets("Last month").Range("A11")
Set rng1 = ThisWorkbook.Sheets("Current month").Range("A11")

n = rng.CurrentRegion.Rows.Count
m = rng1.CurrentRegion.Rows.Count

ReDim CVR1(n)
ReDim Firm1(n)
ReDim CVR2(m)
ReDim Firm2(m)

ThisWorkbook.Sheets("CVR").Range("A1") = "Flyttet CVR"
ThisWorkbook.Sheets("CVR").Range("B1") = "Flyttet Firmanavn"
ThisWorkbook.Sheets("CVR").Range("A1:B1").Interior.ColorIndex = 3
ThisWorkbook.Sheets("CVR").Range("C1") = "Nye CVR"
ThisWorkbook.Sheets("CVR").Range("D1") = "Nye Firmanavn"
ThisWorkbook.Sheets("CVR").Range("C1:D1").Interior.ColorIndex = 4
ThisWorkbook.Sheets("CVR").Range("A1:D1").Font.Bold = True

' Inset data to arrays
For i = 0 To n
    CVR1(i) = ThisWorkbook.Sheets("Last month").Cells(12 + i, 5)
    Firm1(i) = ThisWorkbook.Sheets("Last month").Cells(12 + i, 4)
Next

For i = 0 To m
    CVR2(i) = ThisWorkbook.Sheets("Current month").Cells(12 + i, 5)
    Firm2(i) = ThisWorkbook.Sheets("Current month").Cells(12 + i, 4)
Next
intCurRow1 = 2
intCurRow2 = 2

'Old
For i = 0 To n
    For j = 0 To m
        If Firm1(i) = ThisWorkbook.Sheets("Current month").Cells(12 + j, 4) Then  '** Error raised here   
      Exit For
        End If

        If j = m Then
            ThisWorkbook.Sheets("CVR").Cells(intCurRow1, 1) = CVR1(i)
            ThisWorkbook.Sheets("CVR").Cells(intCurRow1, 2) = Firm1(i)
            intCurRow1 = intCurRow1 + 1
        End If
    Next j
Next i

'new
For i = 0 To m
    For j = 0 To n
        If Firm2(i) = ThisWorkbook.Sheets("Last month").Cells(12 + j, 4) Then
            Exit For
        End If

        If j = n Then
            ThisWorkbook.Sheets("CVR").Cells(intCurRow2, 3) = CVR2(i)
            ThisWorkbook.Sheets("CVR").Cells(intCurRow2, 4) = Firm2(i)
            intCurRow2 = intCurRow2 + 1
        End If
    Next j
Next i

Columns("A:B").Select
ActiveSheet.Range("$A:$B").RemoveDuplicates Columns:=1, Header:=xlNo
Application.DisplayAlerts = False

Columns("C:D").Select
ActiveSheet.Range("$C:$D").RemoveDuplicates Columns:=1, Header:=xlNo
Application.DisplayAlerts = False

End Sub

Viewing all articles
Browse latest Browse all 90317

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>