Code will not do whole file

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,775
Office Version
  1. 365
Platform
  1. Windows
I have been given this code that I am sure a lot of you can read and understand what it does. The problem is whatever the file size I use it on it only completes about 3/4s of it. If the file has 10 rows it only does about 7 of it or if it has 1000 rows it only completes about 700. Any ideas on why this could be? Thanks.

Code:
Sub Dazzawm()
Application.ScreenUpdating = False
Dim Rng     As Range
Dim Dn      As Range
Dim Twn     As String
Dim Q
Dim K
Dim ac      As Long
Dim c       As Long
Dim tot     As Long
    Set Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
    With CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
        For Each Dn In Rng
            Twn = Dn & Dn(, 2) & Dn(, 3)
                If Not .Exists(Twn) Then
                    .Add Twn, Array(Dn, Dn(, 2), Dn(, 3), Dn(, 4), Dn(, 5), Dn(, 6), Dn(, 7))
                Else
                    Q = .Item(Twn)
                        Q(3) = Application.Min(Q(3), Dn(, 4))
                        Q(4) = Application.Max(Q(4), Dn(, 5))
                    .Item(Twn) = Q
                End If
Next
For Each K In .Keys
    tot = tot + (.Item(K)(4) - .Item(K)(3))
Next K
ReDim ray(1 To tot + .Count, 1 To 7)
    For Each K In .Keys
      For ac = .Item(K)(3) To .Item(K)(4)
            c = c + 1
            ray(c, 1) = .Item(K)(0)
            ray(c, 2) = .Item(K)(1)
            ray(c, 3) = .Item(K)(2)
            ray(c, 4) = ac
            ray(c, 5) = .Item(K)(5)
            ray(c, 6) = .Item(K)(6)
        Next ac
    Next K
Range("H2").Resize(tot, 6) = ray
End With
Range("A1:D1").Select
    Selection.Copy
    Range("H1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("F1:G1").Select
    Selection.Copy
    Range("L1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Columns("A:G").Select
    Selection.Delete Shift:=xlToLeft
    Cells.Select
    Cells.EntireColumn.AutoFit
    Columns("C:C").Select
    Selection.NumberFormat = "0.0"
    Range("A1").Select
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
difficult to read exactly what is going on here, but in my experience, where code appears to work, but misses some of the worksheet, AND you are performing a delete somewhere in your code, it's for the following reason

you are working top to bottom, then at some point deleting a section. As you delete this, each section below shifts up. You then move on to the next row, missing one out where it shifted up into your "previous" row. This also happens working left to right and deleting columns

this is just a hunch...
 
Upvote 0

Forum statistics

Threads
1,222,103
Messages
6,163,946
Members
451,867
Latest member
csktwyr

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top