473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Excel VBA: Grouping rows (code doesn't get right results)

I currently have VBA code in Excel to pull desired results, but it's not working correctly. I really need help! I am attaching the spreadsheet and the code. In the spreadsheet, I use cells F5:P16 to show the results of my Sub CaptureGrp, so I can see if different sections of code work. Before I run Sub CaptureGrp I delete data from F5:P16.

I have data in cells A5:E16. I am trying to group data based on "A" (RuleID). I created a variable "Scen" based on results of "E" (AcctNum). I am trying to populate "F" (DID1) and "G" (DID2) based on "D" (Side).

Step 1 of populating "F" (DID1) and "G" (DID2):
* If Side = L, then value of "Scen" goes into "G"
* If Side = R, then value of "Scen" goes into "F"
So each row will have a blank in either "F" or "G"

Step 2 of populating "F" (DID1) and "G" (DID2):
* If rows with the same RuleID has a Side of L and R, then the value of "Scen" gets copied to all cells in "G" and all cells in "F"
So after this step, only RuleID's that have only one Side (L or R) will have blank cells in either "F" or "G"
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Scen As String
  3.  
  4. Sub CaptureGrp()
  5. 'This Function determines the start & end of a RuleID Group
  6. 'On the 1st pass the value of "Scen" is placed in DID1 or DID2
  7. 'The 2nd pass Groups by RuleID field and determines if blanks need to be filled in for DID1 or DID2
  8. 'DID1 works, but the same code used in DID2 did not work
  9. 'The section that does DID2 is commented out so you can test rest of Sub
  10. Dim CurrRuleID As String, PrevRuleID As String, NextRuleID As String
  11. Dim DID1 As String, DID2 As String, lBlankCell As String, rBlankCell As String
  12. Dim SameGroup As Boolean, NewGroup As Boolean, hasL As Boolean, hasR As Boolean
  13. Dim rowStart As Integer, rowEnd As Integer, lRow As Integer, Rows As Integer
  14.  
  15. lRow = 5   'Data starts on line 5
  16. rowStart = 0
  17. rowEnd = 0
  18. Rows = 0
  19. hasL = False
  20. hasR = False
  21. Scen = ""
  22.  
  23. For lRow = lRow To 16
  24.     CurrRuleID = Cells(lRow, "A").Value
  25.     PrevRuleID = Cells(lRow - 1, "A").Value
  26.     NextRuleID = Cells(lRow + 1, "A").Value
  27.  
  28.     Call Scenario(lRow, Scen)
  29.     Worksheets("Grouping").Cells(lRow, 15) = Scen
  30.  
  31.     If CurrRuleID = PrevRuleID Then
  32.         SameGroup = True
  33.         Worksheets("Grouping").Cells(lRow, 11).Value = SameGroup
  34.  
  35.         Call hasLorR(lRow, hasL, hasR)  'WORKS
  36.  
  37.         If CurrRuleID <> NextRuleID And rowStart <> 0 Then
  38.             rowEnd = lRow
  39.  
  40.             Call hasLorR(lRow, hasL, hasR)  'WORKS
  41.  
  42.         ElseIf CurrRuleID = NextRuleID And rowStart = 0 Then
  43.             rowStart = lRow
  44.         End If
  45.  
  46.     ElseIf CurrRuleID <> PrevRuleID Then
  47.         NewGroup = True
  48.         Worksheets("Grouping").Cells(lRow, 12).Value = NewGroup
  49.  
  50.         If rowEnd = 0 Then
  51.             rowStart = lRow
  52.         End If
  53.  
  54.         'Inserted code below to fix Single rows where rowStart = rowEnd
  55.         If CurrRuleID <> NextRuleID Then
  56.             If rowEnd = 0 Then
  57.                 rowEnd = lRow
  58.             End If
  59.         End If
  60.  
  61.         Call hasLorR(lRow, hasL, hasR)  'WORKS
  62.     End If
  63.     'Used to write results to spreadsheet to verify code section
  64.     Worksheets("Grouping").Cells(lRow, 9).Value = rowStart
  65.     Worksheets("Grouping").Cells(lRow, 10).Value = rowEnd
  66.     Worksheets("Grouping").Cells(lRow, 13).Value = hasL
  67.     Worksheets("Grouping").Cells(lRow, 14).Value = hasR
  68.  
  69.     '1st Pass - Populates Scen for every row   WORKS
  70.     If Worksheets("Grouping").Cells(lRow, 4) = "L" Then
  71.         Worksheets("Grouping").Cells(lRow, 7) = Scen
  72.     Else: Worksheets("Grouping").Cells(lRow, 4) = "R"
  73.         Worksheets("Grouping").Cells(lRow, 6) = Scen
  74.     End If
  75.  
  76.     '2nd Pass - Groups by RuleID field and determines
  77.     'if blanks need to be filled in for DID1 or DID2
  78.     If rowStart < rowEnd And (hasR And hasL = True) Then
  79.         Rows = rowEnd - rowStart
  80.         Worksheets("Grouping").Cells(lRow, 16) = Rows
  81.  
  82.         DID1 = Cells(lRow, "F").Value
  83.         DID2 = Cells(lRow, "G").Value
  84.      'If RuleID has an L and R Side, then results for DID1 and DID2
  85.      'needs to be copied to blank cells with same RuleID group
  86.         For Rows = 1 To Rows
  87.             If DID1 <> "" Then   'Left Side (DID1) WORKS
  88.                 Worksheets("Grouping").Cells(lRow - Rows, 6).Value = DID1
  89.             ElseIf DID1 = "" Then
  90.                 Cells(lRow, "F").Find("*", SearchOrder:=xlByColumns, SearchDirection:=1) = lBlankCell
  91.                 Worksheets("Grouping").Cells(lRow - Rows, 6).Value = lBlankCell
  92.             End If
  93.         Next Rows
  94.         'Code STOPS on section below - commented out to test other sections
  95.         'For Rows = 1 To Rows
  96.         '    If DID2 <> "" Then     'Right Side (DID2)doesn't Work
  97.         '        Worksheets("Grouping").Cells(lRow - Rows, 7).Value = DID2
  98.         '    ElseIf DID2 = "" Then
  99.         '        Cells(lRow, "G").Find("*", SearchOrder:=xlByColumns, SearchDirection:=1) = rBlankCell
  100.         '        Worksheets("Grouping").Cells(lRow - Rows, 7).Value = rBlankCell
  101.         '    End If
  102.         'Next Rows
  103.     End If
  104.  
  105. 'If rowStart & rowEnd have been assigned, then reset them
  106.     If rowStart <> 0 And rowEnd <> 0 Then
  107.         rowStart = 0
  108.         rowEnd = 0
  109.         Rows = 0
  110.         hasL = False
  111.         hasR = False
  112.         Scen = ""
  113.     End If
  114. Next lRow
  115.  
  116. End Sub
  117. Function hasLorR(lRow, hasL, hasR)
  118. 'Sets hasL and hasR variables based on each row
  119. 'hasL and hasR variables are declared in CaptureGrp function
  120. 'Variables read at End of Group to determine if rows in Group need a 2nd Pass above
  121.  
  122.     If Worksheets("Grouping").Cells(lRow, 4) = "L" Then
  123.         hasL = True
  124.     Else: Worksheets("Grouping").Cells(lRow, 4) = "R"
  125.         hasR = True
  126.     End If
  127. 'hasR and hasL = False were REMOVED to consolidate results to last row of group
  128.  
  129. End Function
  130. Function Scenario(lRow, Scen)
  131. 'Determines Scen variable based on AcctNum field
  132. 'The end results are placed in either DID1 or DID2 field
  133.  
  134. Dim AcctType As String
  135. Dim Z As String
  136.  
  137. Z = Worksheets("Grouping").Cells(lRow, 5).Value
  138. AcctType = Left(Z, 2)
  139.  
  140. Select Case AcctType
  141.     Case "QM": Scen = "QTOACT"
  142.     Case "CC": Scen = "QTOACT"
  143.     Case "BS": Scen = "FINACT"
  144.     Case "IS": Scen = "FINACT"
  145. End Select
  146.  
  147. End Function
  148.  
I'm not sure if my Excel file attached. Sample Data for cells A5:E16 is below:
RuleID Descr Op Side AcctNum DID1 DID2
AB287 this L < 0 L CC560100
AB287 is L > 0 R CC562050
AB287 not L > 0 R CC562050
CN356 really L Not 0 L QM558040
CN356 important L Not 0 L QM558040
CN356 just R > 0 R BS119060
CN356 a R > 0 R BS119060
DC879 description R Not 0 L QM558040
MX289 anything R > 0 R QM558040
JJ546 field L Not 0 L QM560070
JJ546 name R < 0 R IS170500
EH561 really R < 0 L QM558040

I really appreciate any help I get. Thank you

Mike
Aug 26 '15 #1
0 1349

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jon Spragg | last post by:
Hi, I am running an Excel Application that uses a Control Button to invoke a java application that performs some computation and writes the results to a text file. The VB code behind the...
0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
1
by: Christoph Boes | last post by:
Hi guys, hopefully I am in the right forum. I have a datafield. But many rows are empty. What is the macro to delete these rows if they are empty? Thank you Cheers *** Sent via...
1
by: Paul | last post by:
Dear all, I am trying to create a DLL in C# and try to invoke the DLL from my excel VBA code. This may not be a purely C# problem, since I am not sure whether I have done something wrong with...
3
by: pochimen | last post by:
Hi everyone, i`m trying to make access interact with excel in vba I have an excel sheet with 3 columns and 30 rows, and an access table of 4000 rows. The idea is to compare every row of the...
2
by: prakashsakthivel | last post by:
What I want to know is that while one excel application is closed by VBA code from module, another excel application has to be opened. That means, I have written code to open new excel sheet in the...
76
by: JFKJr | last post by:
Hi, the following Excel VBA code is used to select 5 rows by double clicking the cell in the first column corresponding to that row. The code is working fine in excel. But, I need Access VBA code,...
2
by: prashantdixit | last post by:
Hi all, I am new to Excel/VBA and would require your help. I have stuck again somewhere and will be highly obliged if you can help me. I have two worksheet 1. Import File Utility (Sheet A)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.