Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# VBA Replace &Total

Posted on 2013-12-23
Medium Priority
315 Views
Hello,
I need a vba that can compare Columns (AB, AE, AH, AK, AN) against CELL --- AR1
Example
AB3 is not = AR1 --- Then Empty cells AB3, AC3, AD3
AB4 is not = AR1 --- Then Empty cells AB4, AC4, AD4
and so on

AE2 is not = AR1 --- Then Empty cells AE2,AF2,AG2
AE3 is not = AR1 --- Then Empty cells AE3,AF3,AG3
and so on

AH2 is not = AR1 --- Then Empty cells AH2,AI2,AJ2
AH3 is not = AR1 --- Then Empty cells AH3,AI3,AJ3
and so on

AK2 is not = AR1 --- Then Empty cells AK2,AL2,AM2
AK3 is not = AR1 --- Then Empty cells AK3,AL3,AM3
and so on

AN2 is not = AR1 --- Then Empty cells AN2,AO2,AP2
AN3 is not = AR1 --- Then Empty cells AN3,AO3,AP3
and so on

Then a Total of Columns AD, AG, AJ, AM, AP in cell AP (LAST cell +1)

Number of Rows is different.

Any help is appreciated.
thanks
sample.xlsx
0
Question by:W.E.B
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 4

Accepted Solution

andrew_man earned 2000 total points
ID: 39737377
Sub TEST()
Dim Ro_1 As Long
Dim C As Integer

Ro_1 = Cells(65000, 1).End(xlUp).Row
C = Range("AR1").Value

For i = 2 To Ro_1

'AB3 is not = AR1 --- Then Empty cells AB3, AC3, AD3
If Range("AB" & i).Value = C Then
Range("AB" & i).ClearContents
Range("AC" & i).ClearContents
End If

'AE2 is not = AR1 --- Then Empty cells AE2,AF2,AG2
If Range("AE" & i).Value = C Then
Range("AE" & i).ClearContents
Range("AF" & i).ClearContents
Range("AG" & i).ClearContents
End If

'AH2 is not = AR1 --- Then Empty cells AH2,AI2,AJ2
If Range("AH" & i).Value = C Then
Range("AH" & i).ClearContents
Range("AI" & i).ClearContents
Range("AJ" & i).ClearContents
End If

'AK2 is not = AR1 --- Then Empty cells AK2,AL2,AM2
If Range("AK" & i).Value = C Then
Range("AK" & i).ClearContents
Range("AL" & i).ClearContents
Range("AM" & i).ClearContents
End If

'AN2 is not = AR1 --- Then Empty cells AN2,AO2,AP2
If Range("AN" & i).Value = C Then
Range("AN" & i).ClearContents
Range("AO" & i).ClearContents
Range("AP" & i).ClearContents
End If

'AD, AG, AJ, AM, AP in cell AP

"""),INDIRECT(""\$AG\$2:\$AG\$" & Ro_1 & _
"""),INDIRECT(""\$AJ\$2:\$AJ\$" & Ro_1 & _
"""),INDIRECT(""\$AM\$2:\$AM\$" & Ro_1 & _
"""),INDIRECT(""\$AP\$2:\$AP\$" & Ro_1 & """))"

Next

End Sub
sample.xlsm
0

Author Comment

ID: 39737793
Hello,

the sample is working,
I just had to change  the = to  <>
If Range("AB" & i).Value <> C Then
If Range("AE" & i).Value <> C Then
If Range("AH" & i).Value <> C Then
If Range("AK" & i).Value <> C Then
If Range("AN" & i).Value <> C Then

when I copy the code to my sheet,  I'm getting an error,
Variable not defined.

I'm using Excel 2007.
thanks
0

Author Closing Comment

ID: 39737864
Thanks you,
Dim i As Long
i = 2

thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
###### Suggested Courses
Course of the Month6 days, 9 hours left to enroll