+ Reply to Thread
Results 1 to 8 of 8

How to simplify Macro IF function

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    How to simplify Macro IF function

    Hi ,
    I have macro but is too long
    With Sheets("Sheet1")
    IF Range ("C1).Value = "" then Range ("A1:B1).Value= ""
    IF Range ("C2).Value = "" then Range ("A2:B2).Value= ""
    IF Range ("C3).Value = "" then Range ("A3:B3).Value= ""
    IF Range ("C4).Value = "" then Range ("A4:B4).Value= ""
    IF Range ("C5).Value = "" then Range ("A5:B5).Value= ""
    IF Range ("C6).Value = "" then Range ("A6:B6).Value= ""
    
    End If
    End With
    I really appreciate if anyone can help me to simplify the code
    Attached Files Attached Files
    Last edited by herukuncahyono; 07-19-2013 at 03:38 AM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: How to simplify Macro IF function

    Sub a()
    LR = Cells(Rows.Count, "A").End(xlUp).Row
    For r = 1 To LR
      If Cells(r, "C") = "" Then Range("A" & r & ":B" & r) = ""
    Next
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    Re: How to simplify Macro IF function

    hi patel45

    thanks the code work fine...

  4. #4
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    Re: How to simplify Macro IF function

    hi patel45

    thanks the code work fine...

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to simplify Macro IF function

    Hello herukuncahyono,

    Or you could do it in the Worksheet Change Event:

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim chkRange As Range
    
    Set chkRange = Intersect(Target, [C:C])
    On Error Resume Next
    If Target <> chkRange Then Exit Sub
    If Target.Value = "" Then
    With chkRange
    Target.Offset(, -1) = ""
    Target.Offset(, -2) = ""
    End With
        Else
            Exit Sub
        End If
    
    End Sub
    Attached Files Attached Files
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to simplify Macro IF function

    You could even combine it with the fine Code from patel45.

    Option Explicit
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim chkRange As Range
    
    Set chkRange = Intersect(Target, [C:C])
    On Error Resume Next
    If Target <> chkRange Then Exit Sub
    If Target.Value = "" Then
    With chkRange
    a
    End With
        Else
            Exit Sub
        End If
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    03-11-2009
    Location
    Indonesia
    MS-Off Ver
    Excel 2019
    Posts
    252

    Re: How to simplify Macro IF function

    Winon
    Thanks for the code...perfect

  8. #8
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: How to simplify Macro IF function

    Hello herukuncahyono,

    Thank you for the feedback.

    You are welcome.

    Thank you also for adding to my Reputation.
    Last edited by Winon; 07-19-2013 at 04:32 AM. Reason: Text

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Simplify an IF function
    By GuF021 in forum Excel General
    Replies: 2
    Last Post: 12-16-2011, 08:32 PM
  2. Simplify if function
    By keis386 in forum Excel General
    Replies: 11
    Last Post: 06-21-2011, 08:05 AM
  3. Creating a function to simplify a more complex function
    By mk74 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-29-2011, 08:43 AM
  4. How to simplify an IF(OR function?
    By Starfleeter in forum Excel General
    Replies: 7
    Last Post: 10-28-2010, 09:19 AM
  5. [SOLVED] simplify function
    By Luke in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 05-02-2005, 10:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1