VBA Help, Correct way to repeat code

mrkambo

New Member
Joined
May 7, 2017
Messages
23
Hi!!

Im wondering if anyone can help, below is the code ive written so far and as it is, works well but what i need is the route number to be a variable which can be read from a cell range ie if the route range is C1:C70, it uses the value of C1 then moves onto C2 then C3 etc, im not sure whats the correct way to achieve this.

also the pcell variable id like to do a similar thing with this, ie starts at 10, then at the end of the code + 1 then runs the code again with a new route from the range above

Im sure what i need is simple, i just can't seem to work out the best way to do it

Any help would be greatly appreciated!

Code:
    SCAM = 1041
    SCPM = 1042
    r300d = 0
    r300c = 0
    
    Route = 300 'Route Number
    pcell = 10  'Cell Paste Location
    
    For n = 8 To lastRow
    If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Route) And (Left(Sheets("Source").Cells(n, 10), 3) = "Del") Or _
        (Left(Sheets("Source").Cells(n, 3), 7) = SCPM & Route) And (Left(Sheets("Source").Cells(n, 10), 3) = "Del") Then
        r300d = r300d + 1
    End If
    Next n
    
    For n = 8 To lastRow
    If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Route) And (Left(Sheets("Source").Cells(n, 10), 3) = "Col") Or _
        (Left(Sheets("Source").Cells(n, 3), 7) = SCPM & Route) And (Left(Sheets("Source").Cells(n, 10), 3) = "Col") Then
        r300c = r300c + 1
    End If
    Next n
    
    For n = 8 To lastRow
    If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Route) Then
    Sheets("results").Cells(6, 3).Value = Sheets("Source").Cells(n, 1).Value
    Sheets("results").Cells(pcell, 2).Value = Sheets("Source").Cells(n, 13).Value
    Sheets("results").Cells(pcell, 3).Value = r300c
    Sheets("results").Cells(pcell, 4).Value = r300d
    End If
    Next n
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Please accept the code changes I made.

As far as your first request to turn Route into a variable, I have questions. For every iteration of:
"For n = 8 To lastRow"
you want to test if the combination of SCAM & Route meet the criteria of the cells? Are you then needing to increase the value of r300d each time the criteria is met?

Jeff

Code:
Sub IDunno()


  Dim Source As Worksheet
  Dim Rsht As Worksheet
  
  Set ssht = Sheets("Source")
  Set Rsht = Sheets("Results")
  
  SCAM = 1041
    SCPM = 1042
    r300d = 0
    r300c = 0
    
    Route = 300 'Route Number
    pcell = 10  'Cell Paste Location
    
    With ssht
      For n = 8 To lastRow
        
        If (Left(.Cells(n, 3).Value, 7) = SCAM & Route) And (Left(.Cells(n, 10).Value, 3) = "Del") Or _
            (Left(.Cells(n, 3).Value, 7) = SCPM & Route) And (Left(.Cells(n, 10).Value, 3) = "Del") Then
            r300d = r300d + 1
        End If
        If (Left(.Cells(n, 3).Value, 7) = SCAM & Route) And (Left(.Cells(n, 10).Value, 3) = "Col") Or _
            (Left(.Cells(n, 3).Value, 7) = SCPM & Route) And (Left(.Cells(n, 10).Value, 3) = "Col") Then
            r300c = r300c + 1
        End If
      Next n
    End With
    
    With Rsht
      For n = 8 To lastRow
        If (Left(ssht.Cells(n, 3).Value, 7) = SCAM & Route) Then
          .Cells(6, 3).Value = .Cells(n, 1).Value
          .Cells(pcell, 2).Value = ssht.Cells(n, 13).Value
          .Cells(pcell, 3).Value = r300c
          .Cells(pcell, 4).Value = r300d
        End If
      Next n
    End With
    
End Sub
 
Upvote 0
Apologies for the late reply, i've only just seen the notification

I was playing around with it a lot last night and managed to get it to work the way i wanted to, not sure if the code is efficient but it does produce the results i need

Code:
SCAM = 1041
SCPM = 1042
r300d = 0
r300c = 0
pcell = 9
Lastrowa = Sheets("Admin").Cells(Rows.Count, "W").End(xlUp).Row + 1
    
    For q = 1 To Lastrowa
        r300d = 0
        For n = 8 To lastRow
            If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Worksheets("Admin").Cells(q, 23).Value) And (Left(Sheets("Source").Cells(n, 10), 3) = "Del") Or _
            (Left(Sheets("Source").Cells(n, 3), 7) = SCPM & Worksheets("Admin").Cells(q, 23).Value) And (Left(Sheets("Source").Cells(n, 10), 3) = "Del") Then
            r300d = r300d + 1
            End If
        Next n
        r300c = 0
        For n = 8 To lastRow
            If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Worksheets("Admin").Cells(q, 23).Value) And (Left(Sheets("Source").Cells(n, 10), 3) = "Col") Or _
            (Left(Sheets("Source").Cells(n, 3), 7) = SCPM & Worksheets("Admin").Cells(q, 23).Value) And (Left(Sheets("Source").Cells(n, 10), 3) = "Col") Then
            r300c = r300c + 1
            End If
        Next n
        
        For n = 8 To lastRow
            If (Left(Sheets("Source").Cells(n, 3), 7) = SCAM & Worksheets("Admin").Cells(q, 23).Value) Then
                Sheets("results").Cells(6, k).Value = Sheets("Source").Cells(n, 1).Value
                Sheets("results").Cells(pcell, j).Value = Sheets("Source").Cells(n, 13).Value
                Sheets("results").Cells(pcell, k).Value = r300c
                Sheets("results").Cells(pcell, l).Value = r300d
            End If
        Next n
        pcell = pcell + 1
    Next q
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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