|
-
May 29th, 2006, 01:57 AM
#1
Thread Starter
Lively Member
[RESOLVED]Tricky Excel?
Hi
A new problem has arisen in my macro (surprise!)...
What I have is;
VB Code:
Case "Credit"
gSheet.Rows(j).EntireRow.Copy tSheet.Rows(j)
For l = 5 To 350
Select Case gSheet.Cells(l, 1).Value
Case "W3900"
For m = l To gSheet.Cells(l, 2).End(xlDown).Row
Select Case gSheet.Cells(m, 2).Value
Case gSheet.Cells(j, 2).Value
gSheet.Activate
gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
tSheet.Activate
tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
Application.CutCopyMode = False
End Select
Next m
Case "Z3988"
For m = l To gSheet.Cells(l, 2).End(xlDown).Row
Select Case gSheet.Cells(m, 2).Value
Case gSheet.Cells(j, 2).Value
gSheet.Activate
gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
tSheet.Activate
tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
Application.CutCopyMode = False
End Select
Next m
Case "N3394"
For m = l To gSheet.Cells(l, 2).End(xlDown).Row
Select Case gSheet.Cells(m, 2).Value
Case gSheet.Cells(j, 2).Value
gSheet.Activate
gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
tSheet.Activate
tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlSubtract
Application.CutCopyMode = False
End Select
Next m
Case "3472Hen"
For m = l To gSheet.Cells(l, 2).End(xlDown).Row
Select Case gSheet.Cells(m, 2).Value
Case gSheet.Cells(j, 2).Value
gSheet.Activate
gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
tSheet.Activate
tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlSubtract
Application.CutCopyMode = False
End Select
Next m
End Select
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
-
May 29th, 2006, 02:04 AM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 29th, 2006, 02:27 AM
#3
Thread Starter
Lively Member
Re: Tricky Excel?
Well, what I have is actually;
VB Code:
For i = 5 To 350
Select Case gSheet.Cells(i, 1).Value
Case "Z3798"
For j = i To gSheet.Cells(i, 2).End(xlDown).Row
Select Case gSheet.Cells(j, 2).Value
Case "Credit"
gSheet.Rows(j).EntireRow.Copy tSheet.Rows(j)
For l = 5 To 350
Select Case gSheet.Cells(l, 1).Value
Case "W3900"
For m = l To gSheet.Cells(l, 2).End(xlDown).Row
Select Case gSheet.Cells(m, 2).Value
Case gSheet.Cells(j, 2).Value
gSheet.Activate
gSheet.Range(Cells(m, 3), Cells(m, 3).End(xlToRight)).Copy
tSheet.Activate
tSheet.Range(Cells(j, 3), Cells(j, 3).End(xlToRight)).PasteSpecial Paste:=xlValues, operation:=xlAdd
Application.CutCopyMode = False
End Select
Next m
End Select
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!
-
May 29th, 2006, 02:40 AM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 30th, 2006, 04:01 AM
#5
Thread Starter
Lively Member
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:
For i = 5 To 350
Select Case gSheet.Cells(i, 1).Value
Case "Z3798 BANKAKT DANSKE BANK"
For j = i To gSheet.Cells(i, 2).End(xlDown).Row
Select Case gSheet.Cells(j, 2).Value
Case "Kundegrupper"
gSheet.Rows(j).EntireRow.Copy tSheet.Rows(82)
Case "Finansielle kunder"
gSheet.Rows(j).EntireRow.Copy tSheet.Rows(83)
Case "Kreditinstitutter"
gSheet.Rows(j).EntireRow.Copy tSheet.Rows(84)
'... and so on
' I then use pastespecial to add other values into these rows;
Case "W3900 RESS. OG STABSOMRÅDE"
For j = i To gSheet.Cells(i, 2).End(xlDown).Row
Select Case gSheet.Cells(j, 2).Value
Case "Kundegrupper"
gSheet.Activate
gSheet.Rows(j).EntireRow.Copy
tSheet.Activate
tSheet.Rows(82).PasteSpecial Paste:=xlValues, operation:=xlAdd
Application.CutCopyMode = False
Case "Finansielle kunder"
gSheet.Activate
gSheet.Rows(j).EntireRow.Copy
tSheet.Activate
tSheet.Rows(83).PasteSpecial Paste:=xlValues, operation:=xlAdd
Application.CutCopyMode = False
Case "Kreditinstitutter"
gSheet.Activate
gSheet.Rows(j).EntireRow.Copy
tSheet.Activate
tSheet.Rows(84).PasteSpecial Paste:=xlValues, operation:=xlAdd
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?
-
May 30th, 2006, 05:38 AM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|