Shorter version of code needed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi, I have these lines that I need to make shorter with a for loop or anything cooler. I have been on the offset thing for a while now, can’t seem to figure it out.


I want someone pull me out. Thanks


Code:
Sub getCount()
    Dim i&, j&
                
    i = Me.cbByName.ListIndex
    j = i + 3
    
    With Sheet11


        [B2] = Application.CountIf(.Range("I" & j & ":" & "M" & j), "/")
        
        [B3] = Application.CountIf(.Range("N" & j & ":" & "R" & j), "/")
        [B4] = Application.CountIf(.Range("S" & j & ":" & "W" & j), "/")
        [B5] = Application.CountIf(.Range("X" & j & ":" & "AB" & j), "/")
        [B6] = Application.CountIf(.Range("AC" & j & ":" & "AG" & j), "/")
        [B7] = Application.CountIf(.Range("AH" & j & ":" & "AL" & j), "/")
        [B8] = Application.CountIf(.Range("AM" & j & ":" & "AQ" & j), "/")
        [B9] = Application.CountIf(.Range("AR" & j & ":" & "AV" & j), "/")
        [B10] = Application.CountIf(.Range("AW" & j & ":" & "BA" & j), "/")
        [B11] = Application.CountIf(.Range("BB" & j & ":" & "BF" & j), "/")
        [B12] = Application.CountIf(.Range("BG" & j & ":" & "BK" & j), "/")
        [B13] = Application.CountIf(.Range("BL" & j & ":" & "BP" & j), "/")
    End With
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Code:
Sub kellymort()
   Dim i As Long, j As Long, r As Long
   
   j = Me.cbByName.ListIndex + 3
   r = 1
   With Sheet11
   For i = 9 To 64 Step 5
      r = r + 1
      .Range("B" & r).Value = Application.CountIf(.Range(.Cells(j, i), .Cells(j, i + 4)), "/")
   Next i
End Sub
 
Upvote 0
How about
Code:
Sub kellymort()
   Dim i As Long, j As Long, r As Long
   
   j = Me.cbByName.ListIndex + 3
   r = 1
   With Sheet11
   For i = 9 To 64 Step 5
      r = r + 1
      .Range("B" & r).Value = Application.CountIf(.Range(.Cells(j, i), .Cells(j, i + 4)), "/")
   Next i
End Sub

Indeed, "there is always a better way"

Very smooth.

I appreciate it.

Have a nice time
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,353
Messages
6,124,462
Members
449,163
Latest member
kshealy

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