Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

replace dash to many values for specific column based on condition

0

hello

I try  writing   a simple  code   to  show  the  dash "-"   in column e   if   the  column a  is  less than  date(today)   and   if  I   write    word "show"  in  I1  then  show   again   

this  is  my  trying     and  I  hope  do  that  automaticlly  when  I  write    the  dates  in  column a   also  when write "show"  in cells  i1  in worksheet change  event 

Sub BlankMe()
Dim lr, lr1 As Integer
Dim c as range
lr = Cells(Rows.Count, 5).End(xlUp).Row
lr1 = Cells(Rows.Count, 1).End(xlUp).Row
    For Each c In ActiveSheet.Range("e" & lr)
    If Range("a" & lr1) < Date Then c.Value = "-"
        c.HorizontalAlignment = xlCenter
    Next c
End Sub

thanks  in advance 

Answer
Discuss

Discussion

Hi Leap

Are you sure you want to overwrite the values in column E (there's a simple equation in each cell there)?

When  you say '...write word "show"  in  i1" ', does "then  show   again" mean restore the balance calculation in column E?
John_Ru (rep: 6142) Feb 24, '21 at 4:31 am
  Hi  John   
  yes ,   it  should  returns   the  balances  as  they  were  at  first  time
leap (rep: 46) Feb 24, '21 at 5:16 am
Done! See my Answer please Leap
John_Ru (rep: 6142) Feb 24, '21 at 5:22 am
sorry !  about   the  formula  this  is  my  mistake   it  shouldn't  be  formula    in  column  a     macro  subtracting  between  the  columns  without  show  any  formula   and   you  should   deal   only  the  values  in column e  without  any  formula
leap (rep: 46) Feb 24, '21 at 5:38 am
Leap

If so, in the Worksheet_Change macro for example, you could replace the formula line with... 
Bal.Value = Range("C" & Bal.Row) - Range("D" & Bal.Row)
so no formula shows but the correct result apears.

Does the DashE bit of my Answer address your question anyway?
John_Ru (rep: 6142) Feb 24, '21 at 5:52 am
Add to Discussion

Answers

0
Selected Answer

Leap

Not sure why you want to do this (as per my discussion point) but the values in column E can be replaced by "-" using the slightly modified code shown below:

Private Sub DashE()

Dim lr As Integer
Dim c As Range

lr = Cells(Rows.Count, 5).End(xlUp).Row
Set c = Range("E2:E" & lr)

    For Each Bal In c
        If Range("a" & Bal.Row) < Date Then
            Bal.Value = "-"
            Bal.HorizontalAlignment = xlCenter
            Bal.Interior.Color = vbGreen 'set the color to green (since it's not a real balance)
        End If
    Next Bal

End Sub
(I got a strange error on your workbook so created an similar one and renamed the Sub).

The variable "lrl" isn't needed in your code but "lr" is used to see where column E ends and to set your range "c".  I've add a variable "Bal" to represent a balance cell in the range c (then loop through c). If the date is less than today in column A, the macro change the balance to "-" but also sets the cell colour to green (to indicate that something is a bit odd).

Below is similar code but triggered by the Worksheet_Change event (if cell i1 is chnaged or entered as "show", but nothing else). It returns the formula to any cell containing "-" and changes the colour back to clear  Key bits are in bold below:

 Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("i1")) Is Nothing Or Range("i1") <> "show" Then Exit Sub

Dim lr As Integer
Dim c As Range

lr = Cells(Rows.Count, 5).End(xlUp).Row
Set c = Range("E2:E" & lr)

    For Each Bal In c
        If Bal.Value = "-" Then
            Bal.Formula = "=C" & Bal.Row & "-D" & Bal.Row
            Bal.Interior.Color = vbWhite 'reset to clear
        End If
    Next Bal

End Sub
Both these macros are in the revised file attached (you'll need to run DashE from VB Explorer).

Hope this is what you wanted.

Discuss

Discussion

awesome  codes  !!
thanks  John  for  your  assistance 
 just  i'm  asking  about  the  formula  in column e   if  it 's possible   add a simple  line  to  hide  the  formula   in col e  actually, this  is  minor  ,   if  it's  simple   I  would  truly  appreciate  and if  that  needs  a new  macro  to  do  that   ignore this   then  I  satisfy this  answering
leap (rep: 46) Feb 24, '21 at 5:59 am
Leap

Just to repeat what I've already aid in the Discussion on the Question, in the Worksheet_Change macro for example, you could replace the formula line with...  Select All
Bal.Value = Range("C" & Bal.Row) - Range("D" & Bal.Row)
so no formula shows but the correct result apears.
John_Ru (rep: 6142) Feb 24, '21 at 6:32 am
astonishing ! 
John   you've   achieved   excellant  work 
many  thanks  again
leap (rep: 46) Feb 24, '21 at 6:49 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login