Hosey93
Hosey93

Reputation: 47

Loop through each cell in a range

I've got a basic understanding of how VBA works. I was hoping someone could help shorten this. You will see quickly that the code is massive and would love it if I could get it optimized for my project.

Sub Delete_missing_vals()

Dim myLastRow As Long
Dim i As Long

Application.ScreenUpdating = False

'   Find last row
myLastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Loop through range
For i = 1 To myLastRow
If Cells(i, "A").Value = -99 Or Cells(i, "A").Value = -77 Or Cells(i, "A").Value = -66 Then Range(Cells(i, "A"), Cells(i, "A")).ClearContents
If Cells(i, "B").Value = -99 Or Cells(i, "B").Value = -77 Or Cells(i, "B").Value = -66 Then Range(Cells(i, "B"), Cells(i, "B")).ClearContents
If Cells(i, "C").Value = -99 Or Cells(i, "C").Value = -77 Or Cells(i, "C").Value = -66 Then Range(Cells(i, "C"), Cells(i, "C")).ClearContents
If Cells(i, "D").Value = -99 Or Cells(i, "D").Value = -77 Or Cells(i, "D").Value = -66 Then Range(Cells(i, "D"), Cells(i, "D")).ClearContents
If Cells(i, "E").Value = -99 Or Cells(i, "E").Value = -77 Or Cells(i, "E").Value = -66 Then Range(Cells(i, "E"), Cells(i, "E")).ClearContents
If Cells(i, "F").Value = -99 Or Cells(i, "F").Value = -77 Or Cells(i, "F").Value = -66 Then Range(Cells(i, "F"), Cells(i, "F")).ClearContents
If Cells(i, "G").Value = -99 Or Cells(i, "G").Value = -77 Or Cells(i, "G").Value = -66 Then Range(Cells(i, "G"), Cells(i, "G")).ClearContents
If Cells(i, "H").Value = -99 Or Cells(i, "H").Value = -77 Or Cells(i, "H").Value = -66 Then Range(Cells(i, "H"), Cells(i, "H")).ClearContents
If Cells(i, "I").Value = -99 Or Cells(i, "I").Value = -77 Or Cells(i, "I").Value = -66 Then Range(Cells(i, "I"), Cells(i, "I")).ClearContents
If Cells(i, "J").Value = -99 Or Cells(i, "J").Value = -77 Or Cells(i, "J").Value = -66 Then Range(Cells(i, "J"), Cells(i, "J")).ClearContents
If Cells(i, "K").Value = -99 Or Cells(i, "K").Value = -77 Or Cells(i, "K").Value = -66 Then Range(Cells(i, "K"), Cells(i, "K")).ClearContents
If Cells(i, "L").Value = -99 Or Cells(i, "L").Value = -77 Or Cells(i, "L").Value = -66 Then Range(Cells(i, "L"), Cells(i, "L")).ClearContents
If Cells(i, "M").Value = -99 Or Cells(i, "M").Value = -77 Or Cells(i, "M").Value = -66 Then Range(Cells(i, "M"), Cells(i, "M")).ClearContents
If Cells(i, "N").Value = -99 Or Cells(i, "N").Value = -77 Or Cells(i, "N").Value = -66 Then Range(Cells(i, "N"), Cells(i, "N")).ClearContents
If Cells(i, "O").Value = -99 Or Cells(i, "O").Value = -77 Or Cells(i, "O").Value = -66 Then Range(Cells(i, "O"), Cells(i, "O")).ClearContents
If Cells(i, "P").Value = -99 Or Cells(i, "P").Value = -77 Or Cells(i, "P").Value = -66 Then Range(Cells(i, "P"), Cells(i, "P")).ClearContents
If Cells(i, "Q").Value = -99 Or Cells(i, "Q").Value = -77 Or Cells(i, "Q").Value = -66 Then Range(Cells(i, "Q"), Cells(i, "Q")).ClearContents
If Cells(i, "R").Value = -99 Or Cells(i, "R").Value = -77 Or Cells(i, "R").Value = -66 Then Range(Cells(i, "R"), Cells(i, "R")).ClearContents
If Cells(i, "S").Value = -99 Or Cells(i, "S").Value = -77 Or Cells(i, "S").Value = -66 Then Range(Cells(i, "S"), Cells(i, "S")).ClearContents
If Cells(i, "T").Value = -99 Or Cells(i, "T").Value = -77 Or Cells(i, "T").Value = -66 Then Range(Cells(i, "T"), Cells(i, "T")).ClearContents
If Cells(i, "U").Value = -99 Or Cells(i, "U").Value = -77 Or Cells(i, "U").Value = -66 Then Range(Cells(i, "U"), Cells(i, "U")).ClearContents
If Cells(i, "V").Value = -99 Or Cells(i, "V").Value = -77 Or Cells(i, "V").Value = -66 Then Range(Cells(i, "V"), Cells(i, "V")).ClearContents
If Cells(i, "W").Value = -99 Or Cells(i, "W").Value = -77 Or Cells(i, "W").Value = -66 Then Range(Cells(i, "W"), Cells(i, "W")).ClearContents
If Cells(i, "X").Value = -99 Or Cells(i, "X").Value = -77 Or Cells(i, "X").Value = -66 Then Range(Cells(i, "X"), Cells(i, "X")).ClearContents
If Cells(i, "Y").Value = -99 Or Cells(i, "Y").Value = -77 Or Cells(i, "Y").Value = -66 Then Range(Cells(i, "Y"), Cells(i, "Y")).ClearContents
If Cells(i, "Z").Value = -99 Or Cells(i, "Z").Value = -77 Or Cells(i, "Z").Value = -66 Then Range(Cells(i, "Z"), Cells(i, "Z")).ClearContents
If Cells(i, "AA").Value = -99 Or Cells(i, "AA").Value = -77 Or Cells(i, "AA").Value = -66 Then Range(Cells(i, "AA"), Cells(i, "AA")).ClearContents
If Cells(i, "AB").Value = -99 Or Cells(i, "AB").Value = -77 Or Cells(i, "AB").Value = -66 Then Range(Cells(i, "AB"), Cells(i, "AB")).ClearContents
If Cells(i, "AC").Value = -99 Or Cells(i, "AC").Value = -77 Or Cells(i, "AC").Value = -66 Then Range(Cells(i, "AC"), Cells(i, "AC")).ClearContents
If Cells(i, "AD").Value = -99 Or Cells(i, "AD").Value = -77 Or Cells(i, "AD").Value = -66 Then Range(Cells(i, "AD"), Cells(i, "AD")).ClearContents
If Cells(i, "AE").Value = -99 Or Cells(i, "AE").Value = -77 Or Cells(i, "AE").Value = -66 Then Range(Cells(i, "AE"), Cells(i, "AE")).ClearContents
If Cells(i, "AF").Value = -99 Or Cells(i, "AF").Value = -77 Or Cells(i, "AF").Value = -66 Then Range(Cells(i, "AF"), Cells(i, "AF")).ClearContents
If Cells(i, "AG").Value = -99 Or Cells(i, "AG").Value = -77 Or Cells(i, "AG").Value = -66 Then Range(Cells(i, "AG"), Cells(i, "AG")).ClearContents
If Cells(i, "AH").Value = -99 Or Cells(i, "AH").Value = -77 Or Cells(i, "AH").Value = -66 Then Range(Cells(i, "AH"), Cells(i, "AH")).ClearContents
If Cells(i, "AI").Value = -99 Or Cells(i, "AI").Value = -77 Or Cells(i, "AI").Value = -66 Then Range(Cells(i, "AI"), Cells(i, "AI")).ClearContents
If Cells(i, "AJ").Value = -99 Or Cells(i, "AJ").Value = -77 Or Cells(i, "AJ").Value = -66 Then Range(Cells(i, "AJ"), Cells(i, "AJ")).ClearContents
If Cells(i, "AK").Value = -99 Or Cells(i, "AK").Value = -77 Or Cells(i, "AK").Value = -66 Then Range(Cells(i, "AK"), Cells(i, "AK")).ClearContents
If Cells(i, "AL").Value = -99 Or Cells(i, "AL").Value = -77 Or Cells(i, "AL").Value = -66 Then Range(Cells(i, "AL"), Cells(i, "AL")).ClearContents
If Cells(i, "AM").Value = -99 Or Cells(i, "AM").Value = -77 Or Cells(i, "AM").Value = -66 Then Range(Cells(i, "AM"), Cells(i, "AM")).ClearContents
If Cells(i, "AN").Value = -99 Or Cells(i, "AN").Value = -77 Or Cells(i, "AN").Value = -66 Then Range(Cells(i, "AN"), Cells(i, "AN")).ClearContents
If Cells(i, "AO").Value = -99 Or Cells(i, "AO").Value = -77 Or Cells(i, "AO").Value = -66 Then Range(Cells(i, "AO"), Cells(i, "AO")).ClearContents
If Cells(i, "AP").Value = -99 Or Cells(i, "AP").Value = -77 Or Cells(i, "AP").Value = -66 Then Range(Cells(i, "AP"), Cells(i, "AP")).ClearContents
If Cells(i, "AQ").Value = -99 Or Cells(i, "AQ").Value = -77 Or Cells(i, "AQ").Value = -66 Then Range(Cells(i, "AQ"), Cells(i, "AQ")).ClearContents
If Cells(i, "AR").Value = -99 Or Cells(i, "AR").Value = -77 Or Cells(i, "AR").Value = -66 Then Range(Cells(i, "AR"), Cells(i, "AR")).ClearContents
If Cells(i, "AS").Value = -99 Or Cells(i, "AS").Value = -77 Or Cells(i, "AS").Value = -66 Then Range(Cells(i, "AS"), Cells(i, "AS")).ClearContents
If Cells(i, "AT").Value = -99 Or Cells(i, "AT").Value = -77 Or Cells(i, "AT").Value = -66 Then Range(Cells(i, "AT"), Cells(i, "AT")).ClearContents
If Cells(i, "AU").Value = -99 Or Cells(i, "AU").Value = -77 Or Cells(i, "AU").Value = -66 Then Range(Cells(i, "AU"), Cells(i, "AU")).ClearContents
If Cells(i, "AV").Value = -99 Or Cells(i, "AV").Value = -77 Or Cells(i, "AV").Value = -66 Then Range(Cells(i, "AV"), Cells(i, "AV")).ClearContents
If Cells(i, "AW").Value = -99 Or Cells(i, "AW").Value = -77 Or Cells(i, "AW").Value = -66 Then Range(Cells(i, "AW"), Cells(i, "AW")).ClearContents
If Cells(i, "AX").Value = -99 Or Cells(i, "AX").Value = -77 Or Cells(i, "AX").Value = -66 Then Range(Cells(i, "AX"), Cells(i, "AX")).ClearContents
If Cells(i, "AY").Value = -99 Or Cells(i, "AY").Value = -77 Or Cells(i, "AY").Value = -66 Then Range(Cells(i, "AY"), Cells(i, "AY")).ClearContents
If Cells(i, "AZ").Value = -99 Or Cells(i, "AZ").Value = -77 Or Cells(i, "AZ").Value = -66 Then Range(Cells(i, "AZ"), Cells(i, "AZ")).ClearContents
If Cells(i, "BA").Value = -99 Or Cells(i, "BA").Value = -77 Or Cells(i, "BA").Value = -66 Then Range(Cells(i, "BA"), Cells(i, "BA")).ClearContents
If Cells(i, "BB").Value = -99 Or Cells(i, "BB").Value = -77 Or Cells(i, "BB").Value = -66 Then Range(Cells(i, "BB"), Cells(i, "BB")).ClearContents
If Cells(i, "BC").Value = -99 Or Cells(i, "BC").Value = -77 Or Cells(i, "BC").Value = -66 Then Range(Cells(i, "BC"), Cells(i, "BC")).ClearContents
If Cells(i, "BD").Value = -99 Or Cells(i, "BD").Value = -77 Or Cells(i, "BD").Value = -66 Then Range(Cells(i, "BD"), Cells(i, "BD")).ClearContents
If Cells(i, "BE").Value = -99 Or Cells(i, "BE").Value = -77 Or Cells(i, "BE").Value = -66 Then Range(Cells(i, "BE"), Cells(i, "BE")).ClearContents
If Cells(i, "BF").Value = -99 Or Cells(i, "BF").Value = -77 Or Cells(i, "BF").Value = -66 Then Range(Cells(i, "BF"), Cells(i, "BF")).ClearContents
If Cells(i, "BG").Value = -99 Or Cells(i, "BG").Value = -77 Or Cells(i, "BG").Value = -66 Then Range(Cells(i, "BG"), Cells(i, "BG")).ClearContents
If Cells(i, "BH").Value = -99 Or Cells(i, "BH").Value = -77 Or Cells(i, "BH").Value = -66 Then Range(Cells(i, "BH"), Cells(i, "BH")).ClearContents
If Cells(i, "BI").Value = -99 Or Cells(i, "BI").Value = -77 Or Cells(i, "BI").Value = -66 Then Range(Cells(i, "BI"), Cells(i, "BI")).ClearContents
If Cells(i, "BJ").Value = -99 Or Cells(i, "BJ").Value = -77 Or Cells(i, "BJ").Value = -66 Then Range(Cells(i, "BJ"), Cells(i, "BJ")).ClearContents
If Cells(i, "BK").Value = -99 Or Cells(i, "BK").Value = -77 Or Cells(i, "BK").Value = -66 Then Range(Cells(i, "BK"), Cells(i, "BK")).ClearContents
If Cells(i, "BL").Value = -99 Or Cells(i, "BL").Value = -77 Or Cells(i, "BL").Value = -66 Then Range(Cells(i, "BL"), Cells(i, "BL")).ClearContents
If Cells(i, "BM").Value = -99 Or Cells(i, "BM").Value = -77 Or Cells(i, "BM").Value = -66 Then Range(Cells(i, "BM"), Cells(i, "BM")).ClearContents
If Cells(i, "BN").Value = -99 Or Cells(i, "BN").Value = -77 Or Cells(i, "BN").Value = -66 Then Range(Cells(i, "BN"), Cells(i, "BN")).ClearContents
If Cells(i, "BO").Value = -99 Or Cells(i, "BO").Value = -77 Or Cells(i, "BO").Value = -66 Then Range(Cells(i, "BO"), Cells(i, "BO")).ClearContents
If Cells(i, "BP").Value = -99 Or Cells(i, "BP").Value = -77 Or Cells(i, "BP").Value = -66 Then Range(Cells(i, "BP"), Cells(i, "BP")).ClearContents
If Cells(i, "BQ").Value = -99 Or Cells(i, "BQ").Value = -77 Or Cells(i, "BQ").Value = -66 Then Range(Cells(i, "BQ"), Cells(i, "BQ")).ClearContents
If Cells(i, "BR").Value = -99 Or Cells(i, "BR").Value = -77 Or Cells(i, "BR").Value = -66 Then Range(Cells(i, "BR"), Cells(i, "BR")).ClearContents
If Cells(i, "BS").Value = -99 Or Cells(i, "BS").Value = -77 Or Cells(i, "BS").Value = -66 Then Range(Cells(i, "BS"), Cells(i, "BS")).ClearContents
If Cells(i, "BT").Value = -99 Or Cells(i, "BT").Value = -77 Or Cells(i, "BT").Value = -66 Then Range(Cells(i, "BT"), Cells(i, "BT")).ClearContents
If Cells(i, "BU").Value = -99 Or Cells(i, "BU").Value = -77 Or Cells(i, "BU").Value = -66 Then Range(Cells(i, "BU"), Cells(i, "BU")).ClearContents
If Cells(i, "BV").Value = -99 Or Cells(i, "BV").Value = -77 Or Cells(i, "BV").Value = -66 Then Range(Cells(i, "BV"), Cells(i, "BV")).ClearContents
If Cells(i, "BW").Value = -99 Or Cells(i, "BW").Value = -77 Or Cells(i, "BW").Value = -66 Then Range(Cells(i, "BW"), Cells(i, "BW")).ClearContents
If Cells(i, "BX").Value = -99 Or Cells(i, "BX").Value = -77 Or Cells(i, "BX").Value = -66 Then Range(Cells(i, "BX"), Cells(i, "BX")).ClearContents
If Cells(i, "BY").Value = -99 Or Cells(i, "BY").Value = -77 Or Cells(i, "BY").Value = -66 Then Range(Cells(i, "BY"), Cells(i, "BY")).ClearContents
If Cells(i, "BZ").Value = -99 Or Cells(i, "BZ").Value = -77 Or Cells(i, "BZ").Value = -66 Then Range(Cells(i, "BZ"), Cells(i, "BZ")).ClearContents
If Cells(i, "CA").Value = -99 Or Cells(i, "CA").Value = -77 Or Cells(i, "CA").Value = -66 Then Range(Cells(i, "CA"), Cells(i, "CA")).ClearContents
If Cells(i, "CB").Value = -99 Or Cells(i, "CB").Value = -77 Or Cells(i, "CB").Value = -66 Then Range(Cells(i, "CB"), Cells(i, "CB")).ClearContents
If Cells(i, "CC").Value = -99 Or Cells(i, "CC").Value = -77 Or Cells(i, "CC").Value = -66 Then Range(Cells(i, "CC"), Cells(i, "CC")).ClearContents
If Cells(i, "CD").Value = -99 Or Cells(i, "CD").Value = -77 Or Cells(i, "CD").Value = -66 Then Range(Cells(i, "CD"), Cells(i, "CD")).ClearContents
If Cells(i, "CE").Value = -99 Or Cells(i, "CE").Value = -77 Or Cells(i, "CE").Value = -66 Then Range(Cells(i, "CE"), Cells(i, "CE")).ClearContents
If Cells(i, "CF").Value = -99 Or Cells(i, "CF").Value = -77 Or Cells(i, "CF").Value = -66 Then Range(Cells(i, "CF"), Cells(i, "CF")).ClearContents
If Cells(i, "CG").Value = -99 Or Cells(i, "CG").Value = -77 Or Cells(i, "CG").Value = -66 Then Range(Cells(i, "CG"), Cells(i, "CG")).ClearContents
If Cells(i, "CH").Value = -99 Or Cells(i, "CH").Value = -77 Or Cells(i, "CH").Value = -66 Then Range(Cells(i, "CH"), Cells(i, "CH")).ClearContents
If Cells(i, "CI").Value = -99 Or Cells(i, "CI").Value = -77 Or Cells(i, "CI").Value = -66 Then Range(Cells(i, "CI"), Cells(i, "CI")).ClearContents
If Cells(i, "CJ").Value = -99 Or Cells(i, "CJ").Value = -77 Or Cells(i, "CJ").Value = -66 Then Range(Cells(i, "CJ"), Cells(i, "CJ")).ClearContents
If Cells(i, "CK").Value = -99 Or Cells(i, "CK").Value = -77 Or Cells(i, "CK").Value = -66 Then Range(Cells(i, "CK"), Cells(i, "CK")).ClearContents
If Cells(i, "CL").Value = -99 Or Cells(i, "CL").Value = -77 Or Cells(i, "CL").Value = -66 Then Range(Cells(i, "CL"), Cells(i, "CL")).ClearContents
If Cells(i, "CM").Value = -99 Or Cells(i, "CM").Value = -77 Or Cells(i, "CM").Value = -66 Then Range(Cells(i, "CM"), Cells(i, "CM")).ClearContents
If Cells(i, "CN").Value = -99 Or Cells(i, "CN").Value = -77 Or Cells(i, "CN").Value = -66 Then Range(Cells(i, "CN"), Cells(i, "CN")).ClearContents
If Cells(i, "CO").Value = -99 Or Cells(i, "CO").Value = -77 Or Cells(i, "CO").Value = -66 Then Range(Cells(i, "CO"), Cells(i, "CO")).ClearContents
If Cells(i, "CP").Value = -99 Or Cells(i, "CP").Value = -77 Or Cells(i, "CP").Value = -66 Then Range(Cells(i, "CP"), Cells(i, "CP")).ClearContents
If Cells(i, "CQ").Value = -99 Or Cells(i, "CQ").Value = -77 Or Cells(i, "CQ").Value = -66 Then Range(Cells(i, "CQ"), Cells(i, "CQ")).ClearContents
If Cells(i, "CR").Value = -99 Or Cells(i, "CR").Value = -77 Or Cells(i, "CR").Value = -66 Then Range(Cells(i, "CR"), Cells(i, "CR")).ClearContents
If Cells(i, "CS").Value = -99 Or Cells(i, "CS").Value = -77 Or Cells(i, "CS").Value = -66 Then Range(Cells(i, "CS"), Cells(i, "CS")).ClearContents
If Cells(i, "CT").Value = -99 Or Cells(i, "CT").Value = -77 Or Cells(i, "CT").Value = -66 Then Range(Cells(i, "CT"), Cells(i, "CT")).ClearContents
If Cells(i, "CU").Value = -99 Or Cells(i, "CU").Value = -77 Or Cells(i, "CU").Value = -66 Then Range(Cells(i, "CU"), Cells(i, "CU")).ClearContents
If Cells(i, "CV").Value = -99 Or Cells(i, "CV").Value = -77 Or Cells(i, "CV").Value = -66 Then Range(Cells(i, "CV"), Cells(i, "CV")).ClearContents
If Cells(i, "CW").Value = -99 Or Cells(i, "CW").Value = -77 Or Cells(i, "CW").Value = -66 Then Range(Cells(i, "CW"), Cells(i, "CW")).ClearContents
If Cells(i, "CX").Value = -99 Or Cells(i, "CX").Value = -77 Or Cells(i, "CX").Value = -66 Then Range(Cells(i, "CX"), Cells(i, "CX")).ClearContents
If Cells(i, "CY").Value = -99 Or Cells(i, "CY").Value = -77 Or Cells(i, "CY").Value = -66 Then Range(Cells(i, "CY"), Cells(i, "CY")).ClearContents
If Cells(i, "CZ").Value = -99 Or Cells(i, "CZ").Value = -77 Or Cells(i, "CZ").Value = -66 Then Range(Cells(i, "CZ"), Cells(i, "CZ")).ClearContents
If Cells(i, "DA").Value = -99 Or Cells(i, "DA").Value = -77 Or Cells(i, "DA").Value = -66 Then Range(Cells(i, "DA"), Cells(i, "DA")).ClearContents
If Cells(i, "DB").Value = -99 Or Cells(i, "DB").Value = -77 Or Cells(i, "DB").Value = -66 Then Range(Cells(i, "DB"), Cells(i, "DB")).ClearContents
If Cells(i, "DC").Value = -99 Or Cells(i, "DC").Value = -77 Or Cells(i, "DC").Value = -66 Then Range(Cells(i, "DC"), Cells(i, "DC")).ClearContents
If Cells(i, "DD").Value = -99 Or Cells(i, "DD").Value = -77 Or Cells(i, "DD").Value = -66 Then Range(Cells(i, "DD"), Cells(i, "DD")).ClearContents
If Cells(i, "DE").Value = -99 Or Cells(i, "DE").Value = -77 Or Cells(i, "DE").Value = -66 Then Range(Cells(i, "DE"), Cells(i, "DE")).ClearContents
If Cells(i, "DF").Value = -99 Or Cells(i, "DF").Value = -77 Or Cells(i, "DF").Value = -66 Then Range(Cells(i, "DF"), Cells(i, "DF")).ClearContents
If Cells(i, "DG").Value = -99 Or Cells(i, "DG").Value = -77 Or Cells(i, "DG").Value = -66 Then Range(Cells(i, "DG"), Cells(i, "DG")).ClearContents
If Cells(i, "DH").Value = -99 Or Cells(i, "DH").Value = -77 Or Cells(i, "DH").Value = -66 Then Range(Cells(i, "DH"), Cells(i, "DH")).ClearContents
If Cells(i, "DI").Value = -99 Or Cells(i, "DI").Value = -77 Or Cells(i, "DI").Value = -66 Then Range(Cells(i, "DI"), Cells(i, "DI")).ClearContents
If Cells(i, "DJ").Value = -99 Or Cells(i, "DJ").Value = -77 Or Cells(i, "DJ").Value = -66 Then Range(Cells(i, "DJ"), Cells(i, "DJ")).ClearContents
If Cells(i, "DK").Value = -99 Or Cells(i, "DK").Value = -77 Or Cells(i, "DK").Value = -66 Then Range(Cells(i, "DK"), Cells(i, "DK")).ClearContents
If Cells(i, "DL").Value = -99 Or Cells(i, "DL").Value = -77 Or Cells(i, "DL").Value = -66 Then Range(Cells(i, "DL"), Cells(i, "DL")).ClearContents
If Cells(i, "DM").Value = -99 Or Cells(i, "DM").Value = -77 Or Cells(i, "DM").Value = -66 Then Range(Cells(i, "DM"), Cells(i, "DM")).ClearContents
If Cells(i, "DN").Value = -99 Or Cells(i, "DN").Value = -77 Or Cells(i, "DN").Value = -66 Then Range(Cells(i, "DN"), Cells(i, "DN")).ClearContents
If Cells(i, "DO").Value = -99 Or Cells(i, "DO").Value = -77 Or Cells(i, "DO").Value = -66 Then Range(Cells(i, "DO"), Cells(i, "DO")).ClearContents
If Cells(i, "DP").Value = -99 Or Cells(i, "DP").Value = -77 Or Cells(i, "DP").Value = -66 Then Range(Cells(i, "DP"), Cells(i, "DP")).ClearContents
If Cells(i, "DQ").Value = -99 Or Cells(i, "DQ").Value = -77 Or Cells(i, "DQ").Value = -66 Then Range(Cells(i, "DQ"), Cells(i, "DQ")).ClearContents
If Cells(i, "DR").Value = -99 Or Cells(i, "DR").Value = -77 Or Cells(i, "DR").Value = -66 Then Range(Cells(i, "DR"), Cells(i, "DR")).ClearContents
If Cells(i, "DS").Value = -99 Or Cells(i, "DS").Value = -77 Or Cells(i, "DS").Value = -66 Then Range(Cells(i, "DS"), Cells(i, "DS")).ClearContents
If Cells(i, "DT").Value = -99 Or Cells(i, "DT").Value = -77 Or Cells(i, "DT").Value = -66 Then Range(Cells(i, "DT"), Cells(i, "DT")).ClearContents
If Cells(i, "DU").Value = -99 Or Cells(i, "DU").Value = -77 Or Cells(i, "DU").Value = -66 Then Range(Cells(i, "DU"), Cells(i, "DU")).ClearContents
If Cells(i, "DV").Value = -99 Or Cells(i, "DV").Value = -77 Or Cells(i, "DV").Value = -66 Then Range(Cells(i, "DV"), Cells(i, "DV")).ClearContents
If Cells(i, "DW").Value = -99 Or Cells(i, "DW").Value = -77 Or Cells(i, "DW").Value = -66 Then Range(Cells(i, "DW"), Cells(i, "DW")).ClearContents
If Cells(i, "DX").Value = -99 Or Cells(i, "DX").Value = -77 Or Cells(i, "DX").Value = -66 Then Range(Cells(i, "DX"), Cells(i, "DX")).ClearContents
If Cells(i, "DY").Value = -99 Or Cells(i, "DY").Value = -77 Or Cells(i, "DY").Value = -66 Then Range(Cells(i, "DY"), Cells(i, "DY")).ClearContents
If Cells(i, "DZ").Value = -99 Or Cells(i, "DZ").Value = -77 Or Cells(i, "DZ").Value = -66 Then Range(Cells(i, "DZ"), Cells(i, "DZ")).ClearContents
If Cells(i, "EA").Value = -99 Or Cells(i, "EA").Value = -77 Or Cells(i, "EA").Value = -66 Then Range(Cells(i, "EA"), Cells(i, "EA")).ClearContents
If Cells(i, "EB").Value = -99 Or Cells(i, "EB").Value = -77 Or Cells(i, "EB").Value = -66 Then Range(Cells(i, "EB"), Cells(i, "EB")).ClearContents
If Cells(i, "EC").Value = -99 Or Cells(i, "EC").Value = -77 Or Cells(i, "EC").Value = -66 Then Range(Cells(i, "EC"), Cells(i, "EC")).ClearContents
If Cells(i, "ED").Value = -99 Or Cells(i, "ED").Value = -77 Or Cells(i, "ED").Value = -66 Then Range(Cells(i, "ED"), Cells(i, "ED")).ClearContents
Next i

Application.ScreenUpdating = True

End Sub

Upvotes: 0

Views: 880

Answers (6)

Rdster
Rdster

Reputation: 1872

Do something like this? In case the number of columns needed changes, this will find the last column in the contiguous block.

Sub Delete_missing_vals()
  Dim myLastRow As Long, myLastCol as Long
  Dim i As Long, j as long

  Application.ScreenUpdating = False

  '   Find last row & col
  myLastRow = Cells(Rows.Count, "A").End(xlUp).Row
  myLastCol = Cells(1,"A").End(xlToRight).Column

  '   Loop through range
  For i = 1 To myLastRow
    For j = 1 to MylastCol
      If Cells(i, j) = -99 Or Cells(i, j) = -77 Or Cells(i, j) = -66 Then 
        Cells(i, j).ClearContents
      End If
    Next j
  Next i

  Application.ScreenUpdating = True
End Sub

Upvotes: 2

rfdigital
rfdigital

Reputation: 11

Looks like you may be able to do that by iterating all cells in your Used Range. This is similar to Tackgnol's answer

Sub IterateSheetCells()
Dim cell As Range
    For Each cell In ActiveSheet.UsedRange.Cells
        With cell
            If .Value = -99 OR .Value = -77 OR .Value = -66 Then
                .ClearContents
            End If
        End With
    Next
End Sub

Upvotes: 1

Tackgnol
Tackgnol

Reputation: 505

I would go with this:

Sub Delete_missing_vals()


Dim cell As Range
Dim myRange As Range

Application.ScreenUpdating = False

Set myRange = Cells(1, 1).CurrentRegion

For Each cell In myRange

    With cell
        If .Value = -99 Or .Value = -77 Or .Value = -66 Then

            .ClearContents

        End If
    End With

Next cell

Application.ScreenUpdating = True

End Sub

Upvotes: 1

dtell
dtell

Reputation: 2568

If I get it right you are Testing all columns from A to ED in ascending order. That makes 5x26+4=134 columns in ascending order.

Just create another loop in your row-loop, looping from 1 to 134.

For i = 1 To myLastRow
    For j=1 to 134
        If Cells(i, j).Value = -99 Or Cells(i, j).Value = -77 Or Cells(i, j).Value = -66 Then Range(Cells(i, j), Cells(i, j)).ClearContents
    Next j
Next i

Upvotes: 2

Vityata
Vityata

Reputation: 43585

Try something like this:

Sub Delete_missing_vals()

    Dim myLastRow   As Long
    Dim i           As Long
    Dim myVariant       as Variant

    set myVariant = Array("A","B","C","D","E","F","G","ETC")

    Application.ScreenUpdating = False

'   Find last row
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Loop through range
    For i = 1 To myLastRow
        for col = lbound(myVariant) to Ubound(myVariant)
            If Cells(i, myVariant(col)).Value = -99 Or Cells(i,  myVariant(col)).Value = -77 Or Cells(i, myVariant(col)).Value = -66 Then Range(Cells(i,  myVariant(col)), Cells(i,  myVariant(col))).ClearContents

        next col
    next i


    Application.ScreenUpdating = True

End Sub

But there are probably other better ways to make it work. Still, you can select the columns manually in the array, which is a formidable bonus :)

Upvotes: 1

Ira Burton
Ira Burton

Reputation: 89

This should do the trick. You could do something similar to calculating your last row to calculate the last column if needed.

Sub Delete_missing_vals()

    Dim myLastRow As Long
    Dim i As Long

    Application.ScreenUpdating = False

'   Find last row
    myLastRow = Cells(Rows.Count, "A").End(xlUp).Row

'   Loop through range
    For i = 1 To myLastRow
        For j = 1 To 134
            If Cells(i, j).Value = -99 Or Cells(i, j).Value = -77 Or Cells(i, j).Value = -66 Then Cells(i, j).ClearContents
        Next j
    Next i

    Application.ScreenUpdating = True

End Sub

Upvotes: 1

Related Questions