Results 1 to 6 of 6

Thread: [RESOLVED]Tricky Excel?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Resolved [RESOLVED]Tricky Excel?

    Hi

    A new problem has arisen in my macro (surprise!)...

    What I have is;

    VB Code:
    1. Case "Credit"
    2.           gSheet.Rows(j).EntireRow.Copy tSheet.Rows(j)
    3.           For l = 5 To 350
    4.           Select Case gSheet.Cells(l, 1).Value
    5.           Case "W3900"
    6.             For m = l To gSheet.Cells(l, 2).End(xlDown).Row
    7.             Select Case gSheet.Cells(m, 2).Value
    8.             Case gSheet.Cells(j, 2).Value
    9.               gSheet.Activate
    10.               gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
    11.               tSheet.Activate
    12.               tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
    13.               Application.CutCopyMode = False
    14.             End Select
    15.             Next m
    16.           Case "Z3988"
    17.             For m = l To gSheet.Cells(l, 2).End(xlDown).Row
    18.             Select Case gSheet.Cells(m, 2).Value
    19.             Case gSheet.Cells(j, 2).Value
    20.               gSheet.Activate
    21.               gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
    22.               tSheet.Activate
    23.               tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
    24.               Application.CutCopyMode = False
    25.             End Select
    26.             Next m
    27.           Case "N3394"
    28.             For m = l To gSheet.Cells(l, 2).End(xlDown).Row
    29.             Select Case gSheet.Cells(m, 2).Value
    30.             Case gSheet.Cells(j, 2).Value
    31.               gSheet.Activate
    32.               gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
    33.               tSheet.Activate
    34.               tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlSubtract
    35.               Application.CutCopyMode = False
    36.             End Select
    37.             Next m
    38.           Case "3472Hen"
    39.             For m = l To gSheet.Cells(l, 2).End(xlDown).Row
    40.             Select Case gSheet.Cells(m, 2).Value
    41.             Case gSheet.Cells(j, 2).Value
    42.               gSheet.Activate
    43.               gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
    44.               tSheet.Activate
    45.               tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlSubtract
    46.               Application.CutCopyMode = False
    47.             End Select
    48.             Next m
    49.           End Select
    50.           Next l

    This worked fine for me for a while, until I discovered a flaw! The nested cases can only perform their operations if the initial case is positive - this is however not always the case.

    Can I in any way bypass this, so the nested cells will perform their operations in the same destination as they would have if the initial case had a match?

    regards nick
    Last edited by direktoren; May 30th, 2006 at 05:38 AM. Reason: RESOLVED

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Tricky Excel?

    You have two select statements. One outter and one inner. The outter match causes the inner select to be evaluated. If there is a match in the coded nested cases then it will fire.

    I think your loop inside the initial select may be bad logic as if you want to the outter case to be evaluated again then the loop needs to go outsite of the outter or mail select.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Tricky Excel?

    Well, what I have is actually;

    VB Code:
    1. For i = 5 To 350
    2.   Select Case gSheet.Cells(i, 1).Value
    3.     Case "Z3798"
    4.     For j = i To gSheet.Cells(i, 2).End(xlDown).Row
    5.       Select Case gSheet.Cells(j, 2).Value  
    6.         Case "Credit"
    7.           gSheet.Rows(j).EntireRow.Copy tSheet.Rows(j)
    8.           For l = 5 To 350
    9.           Select Case gSheet.Cells(l, 1).Value
    10.           Case "W3900"
    11.             For m = l To gSheet.Cells(l, 2).End(xlDown).Row
    12.             Select Case gSheet.Cells(m, 2).Value
    13.             Case gSheet.Cells(j, 2).Value
    14.               gSheet.Activate
    15.               gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
    16.               tSheet.Activate
    17.               tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
    18.               Application.CutCopyMode = False
    19.             End Select
    20.             Next m
    21.          End Select
    22.          Next l

    So I have got 2 outer cases - and yes, I do realize that nothing will fire unless I have a match on my outer cases.
    But what I'm wondering is whether there is an easier way to solve my problem, than applying fixed values to all destination ranges instead of variable ones (This code is repeated for lots and lots of reg no. so I would like an alternative!). With fixed values my problem would be somewhat solved, however it will take LOTS of time to implement it!

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Tricky Excel?

    It may be easier to use a Cells.Find method passing varing criteria in a loop.

    There is a detailed explaination in the help file on its arguments.

    expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    http://msdn.microsoft.com/library/de...HV05201251.asp
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Tricky Excel?

    Okay - So i went long...

    I decided to skip the variable row names and apply fixed row names in stead (still bearing in mind that a minor change in organization will force me to apply new row numbers once again).

    Heres what I've got;
    VB Code:
    1. For i = 5 To 350
    2.   Select Case gSheet.Cells(i, 1).Value
    3.     Case "Z3798 BANKAKT DANSKE BANK"
    4.     For j = i To gSheet.Cells(i, 2).End(xlDown).Row
    5.       Select Case gSheet.Cells(j, 2).Value
    6.       Case "Kundegrupper"
    7.           gSheet.Rows(j).EntireRow.Copy tSheet.Rows(82)
    8.       Case "Finansielle kunder"
    9.           gSheet.Rows(j).EntireRow.Copy tSheet.Rows(83)
    10.       Case "Kreditinstitutter"
    11.           gSheet.Rows(j).EntireRow.Copy tSheet.Rows(84)
    12.       '... and so on
    13.  
    14.       ' I then use pastespecial to add other values into these rows;
    15.  
    16.     Case "W3900 RESS. OG STABSOMRÅDE"
    17.     For j = i To gSheet.Cells(i, 2).End(xlDown).Row
    18.       Select Case gSheet.Cells(j, 2).Value
    19.       Case "Kundegrupper"
    20.           gSheet.Activate
    21.           gSheet.Rows(j).EntireRow.Copy
    22.           tSheet.Activate
    23.           tSheet.Rows(82).PasteSpecial Paste:=xlValues, operation:=xlAdd
    24.           Application.CutCopyMode = False
    25.       Case "Finansielle kunder"
    26.           gSheet.Activate
    27.           gSheet.Rows(j).EntireRow.Copy
    28.           tSheet.Activate
    29.           tSheet.Rows(83).PasteSpecial Paste:=xlValues, operation:=xlAdd
    30.           Application.CutCopyMode = False
    31.       Case "Kreditinstitutter"
    32.           gSheet.Activate
    33.           gSheet.Rows(j).EntireRow.Copy
    34.           tSheet.Activate
    35.           tSheet.Rows(84).PasteSpecial Paste:=xlValues, operation:=xlAdd
    36.           Application.CutCopyMode = False

    My problem is, and I'm starting to find the typical for Excel VBA, that it doesn't add all of the rows - some of them it will add, while others it won't!

    What's happening?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: Tricky Excel?

    Solved it - forgot for a moment that i was using Case instead for an If statement, so it didn't loop through the columns - fix it now though...

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