Looping through data

osscie3

Board Regular
Joined
Apr 30, 2014
Messages
70
Hi all. I'll preface this with that my code works but is very sensitive so I'm calling on some of you Excel gods to help me out.

I've written some code that will loop through the range I've defined and apply some formulas. The formulas section works fine but I'm hoping you guys can help me tweak my looping process.

Problem 1: The loop will run through the entire range that contains data but for some reason, omits the last row that has data in it. Then returns a type mismatch error.

Problem 2: It seems to only want to work when I've converted my data into a table. This may be a standard thing that I need to do and am just ignorant.

Problem 3: I have to have cell D2 as the active for the loop to work.

Thoughts? Here's my code. I've left out my calculation part because it's proprietary but you get the gist of it.

Code:
Sub CaseStatement()

Dim current As Integer
Dim merchantID As String
Dim stlAmt As Double
Dim intChng As Double
Dim MCC As Integer
Dim rng As Range
Dim n As Long




Application.ScreenUpdating = False




Set rng = Range(Range("D2"), Range("D" & Rows.Count).End(xlUp))
For n = rng.Count To 1 Step -1
    With Range("E" & n)






Range("A" & n).Select
merchantID = Range("B" & (ActiveCell.Row)).Value
MCC = Range("A" & (ActiveCell.Row)).Value
stlAmt = Range("C" & (ActiveCell.Row)).Value
intChng = Range("D" & (ActiveCell.Row)).Value

End With
Next n


End Sub
 
Always helps when you can look at the Immediate window for debugging. Sheesh. Ok, so it may be doing a mismatch on my column header. For my test data I have 8 rows, the Immediate window for debugging is listing all of my number values in that column then the last one is my column header (MCC). Here is what the debugger sent back in detail:

Code:
 8  5555 
 7  555 
 6  5555 
 5  5555 
 4  5555 
 3  5555 
 2  5555 
 1 MCC
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
so:
Rich (BB code):
 For n = LastRow To 1 Step -1
should be:
Rich (BB code):
For n = LastRow To 2 Step -1
 
Upvote 0

Forum statistics

Threads
1,216,069
Messages
6,128,599
Members
449,460
Latest member
jgharbawi

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