Results 1 to 4 of 4

Thread: [RESOLVED] Excel VBA - corrupted??

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Resolved [RESOLVED] Excel VBA - corrupted??

    Hi

    Just a quick question;

    Is it possible for a code in a module to get corrupted via immense copy/pasting?

    I have a module with a large sub - it's a fairly simple code, which is repeated through out the sub. Now I just discovered that it doesn't work! OR actually it does, but only the first instance - the next 20 instances doesn't function (no errors, they just don't do the trick!).
    I've printed out the codes and verified that they are completely identical (or at least to my eye).

    What happened? Did I kill it with copy/paste???
    Last edited by direktoren; May 17th, 2006 at 09:29 AM. Reason: RESOLVED

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Excel VBA - corrupted??

    Can you post the module? I'll take a look at it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Excel VBA - corrupted??

    Ok It's pretty long, so I'll just bring the two first instances...

    The first one is working, the second one isn't;

    VB Code:
    1. Sub KngrAdder1()
    2.  
    3. 'First instance - this one is working
    4.  
    5. With Worksheets("Grund").Range("A5:R400")
    6.     For i = 1 To .Rows.Count
    7.         If .Cells(i, 1) = "BFORR" Then
    8.         .Cells(i, 1).CurrentRegion.Copy
    9.         Worksheets("Beregninger").Range("A1").PasteSpecial
    10.         End If
    11.     Next i
    12. End With
    13. Application.CutCopyMode = False
    14.  
    15. With Worksheets("Beregninger").Range("A1:R20")
    16.     For i = 1 To .Rows.Count
    17.         If .Cells(i, 2) = "Merchant Bank Private" Then
    18.         .Cells(i, 2).EntireRow.Copy
    19.         Worksheets("Beregninger").Rows("21").PasteSpecial
    20.         End If
    21.     Next i
    22. End With
    23. Application.CutCopyMode = False
    24.  
    25. With Worksheets("Beregninger").Range("A1:R20")
    26.     For i = 1 To .Rows.Count
    27.         If .Cells(i, 2) = "04 Fonde/investeringsselskaber" Then
    28.         .Cells(i, 2).EntireRow.Copy
    29.         Worksheets("Beregninger").Rows("22").PasteSpecial
    30.         End If
    31.     Next i
    32. End With
    33. Application.CutCopyMode = False
    34.  
    35. With Worksheets("Beregninger").Range("A1:R20")
    36.     For i = 1 To .Rows.Count
    37.         If .Cells(i, 2) = "Mindre Erhverv" Then
    38.         .Cells(i, 2).EntireRow.Copy
    39.         Worksheets("Beregninger").Rows("23").PasteSpecial
    40.         End If
    41.     Next i
    42. End With
    43. Application.CutCopyMode = False
    44.  
    45. With Worksheets("Beregninger").Range("A1:R20")
    46.     For i = 1 To .Rows.Count
    47.         If .Cells(i, 2) = "Ukendt" Then
    48.         .Cells(i, 2).EntireRow.Copy
    49.         Worksheets("Beregninger").Rows("24").PasteSpecial
    50.         End If
    51.     Next i
    52. End With
    53. Application.CutCopyMode = False
    54.  
    55. 'Merchant bank
    56. Worksheets("Beregninger").Range("C26:R26").formula = "=Beregninger!C21-Beregninger!C22"
    57.  
    58. 'Mindre Erhverv
    59. Worksheets("Beregninger").Range("C27:R27").formula = "=Beregninger!C22+Beregninger!C23+Beregninger!C24"
    60.  
    61. With Worksheets("Beregninger").Range("A1:R50")
    62.     For i = 1 To .Rows.Count
    63.         If .Cells(i, 2) = "Kundegrupper" Then
    64.         .Cells(i, 2).EntireRow.Copy
    65.         Worksheets("Beregninger").Rows("31").PasteSpecial
    66.         End If
    67.         If .Cells(i, 2) = "Finansielle kunder" Then
    68.         .Cells(i, 2).EntireRow.Copy
    69.         Worksheets("Beregninger").Rows("32").PasteSpecial
    70.         End If
    71.         If .Cells(i, 2) = "Kreditinstitutter" Then
    72.         .Cells(i, 2).EntireRow.Copy
    73.         Worksheets("Beregninger").Rows("33").PasteSpecial
    74.         End If
    75.         If .Cells(i, 2) = "Mellemstore Erhverv" Then
    76.         .Cells(i, 2).EntireRow.Copy
    77.         Worksheets("Beregninger").Rows("34").PasteSpecial
    78.         End If
    79.         If .Cells(i, 2) = "Merchant Bank Private" Then
    80.         .Range("A21:B21").Copy
    81.         .Range("A35:B35").PasteSpecial
    82.         .Range("C35:R35").formula = "=$C$26:$R$26"
    83.         End If
    84.         If .Cells(i, 2) = "Mindre Erhverv" Then
    85.         .Range("A23:B23").Copy
    86.         .Range("A36:B36").PasteSpecial
    87.         .Range("C36:R36").formula = "=$C$27:$R$27"
    88.         End If
    89.         If .Cells(i, 2) = "Offentlige kunder" Then
    90.         .Cells(i, 2).EntireRow.Copy
    91.         Worksheets("Beregninger").Rows("37").PasteSpecial
    92.         End If
    93.         If .Cells(i, 2) = "Porteføljer" Then
    94.         .Cells(i, 2).EntireRow.Copy
    95.         Worksheets("Beregninger").Rows("38").PasteSpecial
    96.         End If
    97.         If .Cells(i, 2) = "Private" Then
    98.         .Cells(i, 2).EntireRow.Copy
    99.         Worksheets("Beregninger").Rows("39").PasteSpecial
    100.         End If
    101.         If .Cells(i, 2) = "Retssager" Then
    102.         .Cells(i, 2).EntireRow.Copy
    103.         Worksheets("Beregninger").Rows("40").PasteSpecial
    104.         End If
    105.         If .Cells(i, 2) = "Storkunder" Then
    106.         .Cells(i, 2).EntireRow.Copy
    107.         Worksheets("Beregninger").Rows("41").PasteSpecial
    108.         End If
    109.         Next i
    110. End With
    111. Application.CutCopyMode = False
    112.  
    113. Worksheets("Tilrettet").Rows("5:15").Value = Worksheets("Beregninger").Rows("31:41").Value
    114. Application.CutCopyMode = False
    115.  
    116. 'Second instance - the one not working!
    117.  
    118. With Worksheets("Grund").Range("A5:R400")
    119.     For i = 1 To .Rows.Count
    120.         If .Cells(i, 1) = "W3744 REALKREDIT DANMARK" Then
    121.         .Cells(i, 1).CurrentRegion.Copy
    122.         Worksheets("Beregninger").Range("A51").PasteSpecial
    123.         End If
    124.     Next i
    125. End With
    126. Application.CutCopyMode = False
    127.  
    128. With Worksheets("Beregninger").Range("A51:R70")
    129.     For i = 1 To .Rows.Count
    130.         If .Cells(i, 2) = "Merchant Bank Private" Then
    131.         .Cells(i, 2).EntireRow.Copy
    132.         Worksheets("Beregninger").Rows("71").PasteSpecial
    133.         End If
    134.     Next i
    135. End With
    136. Application.CutCopyMode = False
    137.  
    138. With Worksheets("Beregninger").Range("A51:R70")
    139.     For i = 1 To .Rows.Count
    140.         If .Cells(i, 2) = "04 Fonde/investeringsselskaber" Then
    141.         .Cells(i, 2).EntireRow.Copy
    142.         Worksheets("Beregninger").Rows("72").PasteSpecial
    143.         End If
    144.     Next i
    145. End With
    146. Application.CutCopyMode = False
    147.  
    148. With Worksheets("Beregninger").Range("A51:R70")
    149.     For i = 1 To .Rows.Count
    150.         If .Cells(i, 2) = "Mindre Erhverv" Then
    151.         .Cells(i, 2).EntireRow.Copy
    152.         Worksheets("Beregninger").Rows("73").PasteSpecial
    153.         End If
    154.     Next i
    155. End With
    156. Application.CutCopyMode = False
    157.  
    158. With Worksheets("Beregninger").Range("A51:R70")
    159.     For i = 1 To .Rows.Count
    160.         If .Cells(i, 2) = "Ukendt" Then
    161.         .Cells(i, 2).EntireRow.Copy
    162.         Worksheets("Beregninger").Rows("74").PasteSpecial
    163.         End If
    164.     Next i
    165. End With
    166. Application.CutCopyMode = False
    167.  
    168. 'Merchant bank
    169. Worksheets("Beregninger").Range("C76:R76").formula = "=Beregninger!C71-Beregninger!C72"
    170.  
    171. 'Mindre Erhverv
    172. Worksheets("Beregninger").Range("C77:R77").formula = "=Beregninger!C72+Beregninger!C73+Beregninger!C74"
    173.  
    174. With Worksheets("Beregninger").Range("A51:R100")
    175.     For i = 1 To .Rows.Count
    176.         If .Cells(i, 2) = "Kundegrupper" Then
    177.         .Cells(i, 2).EntireRow.Copy
    178.         Worksheets("Beregninger").Rows("81").PasteSpecial
    179.         End If
    180.         If .Cells(i, 2) = "Finansielle kunder" Then
    181.         .Cells(i, 2).EntireRow.Copy
    182.         Worksheets("Beregninger").Rows("82").PasteSpecial
    183.         End If
    184.         If .Cells(i, 2) = "Kreditinstitutter" Then
    185.         .Cells(i, 2).EntireRow.Copy
    186.         Worksheets("Beregninger").Rows("83").PasteSpecial
    187.         End If
    188.         If .Cells(i, 2) = "Mellemstore Erhverv" Then
    189.         .Cells(i, 2).EntireRow.Copy
    190.         Worksheets("Beregninger").Rows("84").PasteSpecial
    191.         End If
    192.         If .Cells(i, 2) = "Merchant Bank Private" Then
    193.         .Range("A71:B71").Copy
    194.         .Range("A85:B85").PasteSpecial
    195.         .Range("C85:R85").formula = "=$C$76:$R$76"
    196.         End If
    197.         If .Cells(i, 2) = "Mindre Erhverv" Then
    198.         .Range("A73:B73").Copy
    199.         .Range("A86:B86").PasteSpecial
    200.         .Range("C86:R86").formula = "=$C$77:$R$77"
    201.         End If
    202.         If .Cells(i, 2) = "Offentlige kunder" Then
    203.         .Cells(i, 2).EntireRow.Copy
    204.         Worksheets("Beregninger").Rows("87").PasteSpecial
    205.         End If
    206.         If .Cells(i, 2) = "Porteføljer" Then
    207.         .Cells(i, 2).EntireRow.Copy
    208.         Worksheets("Beregninger").Rows("88").PasteSpecial
    209.         End If
    210.         If .Cells(i, 2) = "Private" Then
    211.         .Cells(i, 2).EntireRow.Copy
    212.         Worksheets("Beregninger").Rows("89").PasteSpecial
    213.         End If
    214.         If .Cells(i, 2) = "Retssager" Then
    215.         .Cells(i, 2).EntireRow.Copy
    216.         Worksheets("Beregninger").Rows("90").PasteSpecial
    217.         End If
    218.         If .Cells(i, 2) = "Storkunder" Then
    219.         .Cells(i, 2).EntireRow.Copy
    220.         Worksheets("Beregninger").Rows("91").PasteSpecial
    221.         End If
    222.         Next i
    223. End With
    224. Application.CutCopyMode = False
    225.  
    226. Worksheets("Tilrettet").Rows("25:35").Value = Worksheets("Beregninger").Rows("81:91").Value
    227. Application.CutCopyMode = False

    The part that doesn't work in the second instance (and all of the following) is the following;

    VB Code:
    1. If .Cells(i, 2) = "Merchant Bank Private" Then
    2.         .Range("A71:B71").Copy
    3.         .Range("A85:B85").PasteSpecial
    4.         .Range("C85:R85").formula = "=$C$76:$R$76"
    5.         End If
    6.         If .Cells(i, 2) = "Mindre Erhverv" Then
    7.         .Range("A73:B73").Copy
    8.         .Range("A86:B86").PasteSpecial
    9.         .Range("C86:R86").formula = "=$C$77:$R$77"
    10.         End If

    And yes - it is a sloppy code, but its old!!

    Nick
    Last edited by direktoren; May 17th, 2006 at 01:30 AM. Reason: corrected

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Excel VBA - corrupted??

    I can't see anything obviously wrong with it, but I do have a couple of suggestions. First would be to replace all of the If...End if statements with a Select Case structure, i.e.:
    VB Code:
    1. With Worksheets("Beregninger").Range("A1:R50")
    2.     For i = 1 To .Rows.Count
    3.         Select Case .Cells(i, 2).Value
    4.         Case "Kundegrupper"
    5.             .Cells(i, 2).EntireRow.Copy
    6.             Worksheets("Beregninger").Rows("31").PasteSpecial
    7.         Case "Finansielle kunder"
    8.             .Cells(i, 2).EntireRow.Copy
    9.             Worksheets("Beregninger").Rows("32").PasteSpecial
    10.         Case "Kreditinstitutter"
    11.             .Cells(i, 2).EntireRow.Copy
    12.             Worksheets("Beregninger").Rows("33").PasteSpecial
    13.         Case "Mellemstore Erhverv"
    14.             .Cells(i, 2).EntireRow.Copy
    15.             Worksheets("Beregninger").Rows("34").PasteSpecial
    16.         Case "Merchant Bank Private"
    17.             .Range("A21:B21").Copy
    18.             .Range("A35:B35").PasteSpecial
    19.             .Range("C35:R35").Formula = "=$C$26:$R$26"
    20.         '...
    21.         End Select
    22.     Next i
    23. End With
    You can also speed things up a little bit by early loop exits. These sections are looking for a specific value, so when you find it, there is no reason to continue the loop:
    VB Code:
    1. With Worksheets("Grund").Range("A5:R400")
    2.     For i = 1 To .Rows.Count
    3.         If .Cells(i, 1) = "BFORR" Then
    4.             .Cells(i, 1).CurrentRegion.Copy
    5.             Worksheets("Beregninger").Range("A1").PasteSpecial
    6.             Exit For   'Add this
    7.         End If
    8.     Next i
    9. End With
    As far as not functioning goes, I would just step through it in the debugger and make sure that all the values are what you expect. It's a lot easier to nail down what is happening this way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width