VBA Remove values from a loop

Stephen_IV

Well-known Member
Joined
Mar 17, 2003
Messages
1,174
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Good morning,

First of all I am not very good at VBA but was trying to exclude values in a loop. For instance this loop will give me a range of 1 to 61. Is there a way to exclude some of the values like take out values 9, 18, 19, 23, 24, 29, 33, 34, 35, 38. I could probably write a delete sub to do this afterwords but is there an easy way or am I missing something? Thanks in advance.

Code:
Sub practice()
Dim i As Integer
    For i = 1 To 61
        Cells(i, 1) = i
    Next
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
How about
Code:
Sub practice()
   Dim i As Integer
   Dim rw As Long
   
   rw = 1
   For i = 1 To 61
      Select Case i
         Case 9, 18, 19, 23, 24, 29, 33 To 35, 38
         Case Else
            Cells(rw, 1) = i
            rw = rw + 1
      End Select
   Next i
End Sub
 
Upvote 0
That's it Fluff!! Thank you very much for the info! Very much appreciate it!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Here is another macro that you may find interesting (no loop)...
Code:
[table="width: 500"]
[tr]
	[td]Sub Test()
  With Range("A1:A61")
    .Formula = "=IF(OR(ROW(A1:A61)={9,18,19,23,24,29,33,34,35,38}),"""",ROW(A1:A61))"
    .Value = .Value
    .SpecialCells(xlBlanks).Delete xlShiftUp
  End With
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
Here is another macro that you may find interesting (no loop)...
Code:
[table="width: 500"]
[tr]
	[td]Sub Test()
  With Range("A1:A61")
    .Formula = "=IF(OR(ROW(A1:A61)={9,18,19,23,24,29,33,34,35,38}),"""",ROW(A1:A61))"
    .Value = .Value
    .SpecialCells(xlBlanks).Delete xlShiftUp
  End With
End Sub[/td]
[/tr]
[/table]
If the OP is willing to use full range designations instead of just row numbers, then here is another non-looping macro that can be considered...
Code:
Sub Test()
  [A1:A61] = [ROW(1:61)]
  Range("9:9,18:19,23:24,29:29,33:35,38:38") = ""
  [A1:A61].SpecialCells(xlBlanks).Delete xlShiftUp
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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