Stumped by a For Next problem

Matt_O

Board Regular
Joined
May 23, 2013
Messages
64
Hi Excel Gurus,

I've run into a problem where I lose (or else don't capture) values correctly when I run a subroutine after calling for the next worksheet.

The routine works fine if I don't try to automate the selection of the next sheet to run this routine. Each sheet I try the routine on independently, it works as intended.

Due to my limited VBA skills I repeat a function multiple times to capture a dynamic variable (column number) by searching for the text inside the upper most cell of each used column. I have 4 versions of this routine. From ColStep to ColStep4. After all four have run I have my variables MyCol, MyCol2, MyCol3, and MyCol4.

Dim mySheetCount As Long

For mySheetCount = 4 To Sheets.Count

Sheets(mySheetCount).Select

For ColStep = 1 To ColCount Step 1

If headerText_1 = "Heading_1" Then
ActiveCell.Select
MyCol = ActiveCell.Column
Exit For
Else
ActiveCell.Offset(0, 1).Select
headerText_1 = ActiveCell.Value
End If
Next ColStep

'maybe redundant to go to this range
Range("A1").Select


For ColStep2 = 1 To ColCount Step 1

If headerText_2 = "Heading_2" Then
ActiveCell.Select
MyCol2 = ActiveCell.Column

n = Cells(Rows.Count, MyCol2).End(xlUp).Row

Exit For
Else
ActiveCell.Offset(0, 1).Select
headerText_2 = ActiveCell.Value
End If

Next ColStep2

I run into the problem at this point and it is mind baffling to me. 'j' has been set as an Integer. 'n' is the number of used Rows.

For j = 2 To n Step 1

a = Cells(j, MyCol).Value

b = Cells(j, MyCol2).Value

c = Cells(j, MyCol3).Value

d = Cells(j, MyCol4).Value

If
... code does its work...
End If

Next j

Next mySheetCount

Here's the problem, once the focus shifts to the next sheet in the workbook MyCol's value is captured correctly but the other column values (MyCol2, MyCol3, and MyCol4) lose their values and seem to default to '1' and the remaining action is limited to column A (first column). It appears that my 'For Next' loop isn't working starting at ColStep2 once I've switched sheets.

However, the variable 'n' captures the correct value on the next sheet in the For ColStep2 function.

Totally stumped!


If anyone thinks they can help I'll more than happy to show the whole code. If I comment out the For/Next for mySheetCount the codes works perfectly.

Thanks in advance,
Matt
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I found a work around. I created a separate module for the For/Next for calling the next sheet in the workbook. I'll likely never know what was causing the original issue.
 
Upvote 0
Hi

A couple of observations :-
1, Always use variables dimmed as Long when refering to Rows and Columns.

2, If "Heading_1" and "Heading_2" always exist (once in any sheet) wouldn't it be easier to capture the Column number using MATCH?

hth
 
Last edited:
Upvote 0
Hi Mike,

Thanks for the reply.

I thought choosing Integer versus Long was based on how large the number might get. The row count would likely never go past 50. But if using Long is the best practice then I'll go and update my code.

As for MATCH. Well, I've never used that function!

Time to do some research. I'm guessing using MATCH will clean up my sprawling code.

Thanks,
Matt
 
Upvote 0
Hi

The MATCH would be something like (untested) :-
Code:
MyCol = Application.Match("Heading_1", Range("A1:Z1"),0)
which would replace the first loop and of course avoid selecting cells.

hth
 
Upvote 0
Thanks again Mike.

I did some research on using Match and this is exactly what I should be using. I figured out the syntax for a variable loop up target and dynamic look up array:

MyCol = Application.Match(str, Range(Cells(1, 1), Cells(1, ColCount)), 0)

I did get stumped for a while when first testing because my test cells were not contiguous. That mystified me for all!

Time to update my code!

Cheers,
Matt
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,094
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top