473,382 Members | 1,431 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,382 software developers and data experts.

VBA: Loop hangs up after one pass

I have three fields of data:
1. CaseID: need to Group by
2. Side: either L or R
3. AcctNum

My main Sub: CaptureGrpMacro determines the start(rowStart) and end(rowEnd) rows of each group. The hasLorR function determines if Side is L or R and if so turns flag to True. The Scenario function takes the Scen variable and places in ScenL or ScenR field based on Side being L or R. So if a group(CaseID) has both a Side of L and R, then whatever is in ScenR or ScenL needs to be copied to all rows within that group. This is done by the ProcessGrp function.

So if I Dim out the Call ProcessGrp function, then rowStart, rowEnd, hasL, hasR, ScenR, and ScenL is populated for each row. But when I run the Sub with ProcessGrp function, it processes the first group then hangs up on Row 118 and I have to hit Ctrl-Break to get out of loop. Can someone please take a look at my code and sample data? I can't figure out what is wrong. Thank you very much.

Mike
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Private Data_Class As String, Scen As String, ScenR As String, ScenL As String
  3.  
  4. Function FindLastRow(LastRow As Long)
  5. Dim sht As Worksheet
  6.  
  7. Set sht = ThisWorkbook.Worksheets("GrpTest")
  8. LastRow = sht.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
  9.  
  10. End Function
  11. Sub CaptureGrpMacro()
  12. 'This Function determines the start & end of a CaseID Group
  13. Dim rowStart As Integer, rowEnd As Integer
  14. Dim HRow As Integer, LastRow As Long
  15. Dim hasL As Boolean, hasR As Boolean
  16. Dim Side As String, PrevSide As String, NextSide As String
  17.  
  18. Call FindLastRow(LastRow)
  19. HRow = 10
  20.  
  21. rowStart = 0
  22. rowEnd = 0
  23. hasL = False
  24. hasR = False
  25. Scen = ""
  26. ScenL = ""
  27. ScenR = ""
  28.  
  29. For HRow = HRow To LastRow
  30.     Side = Cells(HRow, 2).Value
  31.     PrevSide = Cells(HRow - 1, 2).Value
  32.     NextSide = Cells(HRow + 1, 2).Value
  33.  
  34.     If Side <> "C" Then
  35.  
  36.         If (PrevSide = "C") And rowStart = 0 Then
  37.             rowStart = HRow
  38.         End If
  39.  
  40.         Call Scenario(HRow, Side, ScenL, ScenR)
  41.         Call hasLorR(HRow, Side, hasL, hasR)
  42.  
  43.         If (NextSide = "C") And rowEnd = 0 Then
  44.             rowEnd = HRow
  45.         End If
  46.  
  47.     End If
  48.     Worksheets("GrpTest").Cells(HRow, 5).Value = rowStart
  49.     Worksheets("GrpTest").Cells(HRow, 6).Value = rowEnd
  50.     Worksheets("GrpTest").Cells(HRow, 7).Value = hasL
  51.     Worksheets("GrpTest").Cells(HRow, 8).Value = hasR
  52.  
  53.     'If rowStart & rowEnd have been assigned, then reset them
  54.     If rowStart <> 0 And rowEnd <> 0 Then
  55.         Call ProcessGrp(HRow, rowStart, rowEnd, ScenL, ScenR, hasL, hasR)
  56.         rowStart = 0
  57.         rowEnd = 0
  58.         hasL = False
  59.         hasR = False
  60.         Scen = ""
  61.         ScenL = ""
  62.         ScenR = ""
  63.     End If
  64.  
  65. Next HRow
  66.  
  67. End Sub
  68. Function hasLorR(HRow, Side, hasL, hasR)
  69. 'Sets hasL and hasR variables based on each row
  70.  
  71.     If Side = "L" Then
  72.         hasL = True
  73.     Else: Side = "R"
  74.         hasR = True
  75.     End If
  76.  
  77. End Function
  78. Function Scenario(HRow, Side, ScenL, ScenR)
  79. 'Determines Scen variable based on AcctNum field
  80. Dim AcctType As String, Z As String, x As String
  81.  
  82. 'Sets Data Class variable
  83. x = Worksheets("GrpTest").Cells(HRow, 1).Value
  84. Data_Class = Left(x, 1)
  85.  
  86. 'Sets AcctType variable
  87. Z = Worksheets("GrpTest").Cells(HRow, 3).Value            'Account Number
  88. AcctType = Left(Z, 2)
  89.  
  90. 'Sets Scen variable
  91. Select Case AcctType
  92.     Case "QM": Scen = "QMEMOACT"
  93.     Case "CC": Scen = "QMEMOACT"
  94.     Case "BS": Scen = "FINACT"
  95.     Case "IS": Scen = "FINACT"
  96. End Select
  97.  
  98. 'Populates Scenario for every row
  99.     If Side = "L" Then
  100.         ScenR = Scen
  101.         Worksheets("GrpTest").Cells(HRow, 9) = ScenR               'Side = L
  102.     Else: Side = "R"
  103.         ScenL = Scen
  104.         Worksheets("GrpTest").Cells(HRow, 10) = ScenL               'Side = R
  105.     End If
  106.  
  107. End Function
  108. Function ProcessGrp(HRow, rowStart, rowEnd, ScenL, ScenR, hasL, hasR)
  109. Dim rng As Range
  110. Dim DRow As Integer
  111.  
  112.     If rowStart < rowEnd And (hasL And hasR = True) Then
  113.         DRow = 0
  114.         For HRow = rowEnd To rowStart Step -1
  115.             For DRow = rowStart To rowEnd
  116.                 Worksheets("GrpTest").Cells(DRow, 9) = ScenR
  117.                 Worksheets("GrpTest").Cells(DRow, 10) = ScenL
  118.             Next DRow
  119.         Next HRow
  120.     End If
  121.     DRow = 0
  122. End Function
  123.  
Sep 7 '15 #1

✓ answered by Rabbit

Line 18 doesn't set the last row variable within the function capture group macro. It only sets a different last row variable that exists only within the find last row function. Once that function returns, that other last row variable disappears.

You can test this by using the debugger to step through the code.

5 2112
Rabbit
12,516 Expert Mod 8TB
The thing that pops out first is that your last row variable isn't populated. Passing a variable into a function creates a new variable within the scope of that function. Setting the value within that function doesn't change the value of the variable outside that function. There might be other things wrong as well but that's the first one I noticed.
Sep 7 '15 #2
Thank you for your reply Rabbit. I'm not sure I understand. The LastRow variable is set in the beginning, or at least I believe it is. The rowEnd variable is also set in the CaptureGrp function, or at least I believe it is. So what are you referring too and where if I may ask. Thank you
Sep 7 '15 #3
Rabbit
12,516 Expert Mod 8TB
Line 18 doesn't set the last row variable within the function capture group macro. It only sets a different last row variable that exists only within the find last row function. Once that function returns, that other last row variable disappears.

You can test this by using the debugger to step through the code.
Sep 8 '15 #4
Rabbit, THANK YOU!!! Your were right. The HRow variable was getting reset to rowStart of the 1st group in the ProcessGrp function, thus continuing to loop through the first group. I set HRow variable to a new variable in ProcessGrp and it worked. I really appreciate your help, I have been stuck on this for far too long. Have a great day!
Sep 8 '15 #5
Rabbit
12,516 Expert Mod 8TB
No problem, good luck on the rest of your project.
Sep 8 '15 #6

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

Similar topics

1
by: Bernhard Sollfrank | last post by:
Hello, i have to start a batch process from a dll to transfer files created by this dll to remote ftp server. I created a batch file: ftp -n -v -i -s:D:\batch\ftpcmds.txt ftp-server and a...
0
by: psb | last post by:
I make a website for a company where sales reps from all over the country order literature. This is the 4th case where the exact same thing is happening in the almost the exact same setup -The...
5
by: Andrea Vincenzi | last post by:
Help me please, I'm totally stuck! My Visual Studio 2003 debugger stopped working after I installed Windows XP Service Pack 2. Here is what happens (with any project, even a "Hello, world"...
3
by: yogesh | last post by:
I have compiled kernel 2.4.18 . I have followed the following sequence ... as make mrproper make xconfig make dep make make modules make modules_install then copied system.map and bzimage to...
0
by: Patrick.O.Ige | last post by:
We have some issues after installing Enterprise Library. When i run the project once it loads IE succesfully but after closing it and openeing another IE window it loads and never finishes so it...
14
by: alsmeirelles | last post by:
Hi, I'm building a multithreaded application and I encountered a tiny and annoying problem. I use a select to wait for data to be read from a socket, after some reads, the select simply blocks...
7
by: Avaenuha | last post by:
Hi, It appears my program can't get past a particular printf() statement. Code excerpt: printf("Sales Report\n--------------"); printf("Testing code - pre loop entry"); while(category !=...
1
by: hash4sp | last post by:
Hello ! I have a problem with pop window on my system. The application hangs after I close the pop up window. It works fine on few other systems. I am using the following line of code to...
3
by: Fiddler2 | last post by:
I noticed that after running compact/repair, I have to recompile my code for the program not to break the next time I open it. What happens is the auto exec macro runs the main() module, then hangs...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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$) { } ...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.