Solved

VBA Replace &Total

Posted on 2013-12-23
3
269 Views
Last Modified: 2013-12-24
Hello,
can you please help,
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)
Please see attached sample.

Number of Rows is different.

Any help is appreciated.
thanks
sample.xlsx
0
Comment
Question by:Wass_QA
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
andrew_man earned 500 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
 Range("AD" & 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

Range("AP" & (Ro_1 + 2)).FormulaR1C1 = "=SUM(INDIRECT(""$AD$2:$AD$" & Ro_1 & _
                                        """),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

by:Wass_QA
ID: 39737793
Hello,
thanks for your help,

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

by:Wass_QA
ID: 39737864
Thanks you,
I added
Dim i As Long
i = 2

thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now