+ Reply to Thread
Results 1 to 13 of 13

If anything in cell.. then put period in cell beside

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    If anything in cell.. then put period in cell beside

    If there is anything in a cell in Column D, then I want the function to automatically put a period beside the cell in column E. If there is nothing in the cell in Column D, then I don't want there to have anything happen.

    I want this to happen automatically in the worksheet... so I don't have to run a macro everytime. Thx
    Last edited by Xx7; 02-18-2011 at 11:17 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    Place this macro in the SHEET MODULE so it is active all the time. It will watch for activity in column D. If any cell in column D is changed, it place a period in column E. If the change CLEARS a cell in column D, the column E period would be removed, too.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    On Error Resume Next
    For Each cell In Intersect(Target, Range("D:D"))
        Application.EnableEvents = False
        If cell <> "" Then
            cell.Offset(, 1) = "."
        Else
            cell.Offset(, 1) = ""
        End If
        Application.EnableEvents = True
    Next cell
    
    End Sub
    Last edited by JBeaucaire; 02-16-2011 at 12:19 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: If anything in cell.. then put period in cell beside

    Jerry,

    It seems to give me a run-time error anytime I enter info in columns other than D. Any way around this?

    Thanks!

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    My bad, correction added above.

  5. #5
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: If anything in cell.. then put period in cell beside

    Beautiful... works like a charm

  6. #6
    Registered User
    Join Date
    03-09-2007
    Posts
    75

    Re: If anything in cell.. then put period in cell beside

    Dear Jerry,

    Would it be better if we use intersect so that when the data gets huge, this will only run macro on the specific cell updated.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    On Error Resume Next
    If Not Intersect(Target, Range("D:D")) Is Nothing Then    
    
    Application.EnableEvents = False
        If Activecell <> "" Then
            Activecell.Offset(, 1) = "."
        Else
            Activecell.Offset(, 1) = ""
        End If
        Application.EnableEvents = True
    
    End If 
    
    
    End Sub
    Pls advice as I am also exploring.

    Cheers,
    CL

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    My macro in post #2 only operates on the cell(s) just changed. My design is set to watch all the cells you change, even if you do more than one cell at one time, and operate on the ones that are in column D. Did you notice the Intersect() reference already in my macro?

    Your macro utilized Activecell references which I would not do.

  8. #8
    Registered User
    Join Date
    03-09-2007
    Posts
    75

    Re: If anything in cell.. then put period in cell beside

    Hi Jerry,

    I had the impression that the "For" loop command will run thru all cells. Thanks for pointing out the occassion that user may mass update cells using copy-paste actions.

    Cheers,
    CL

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    The FOR/NEXT loop runs based on controls you define. I defined my FOR as "all the cells just changed that intersect column D."

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    As per forum rules, continue your discussion here, not in Private Messages.

    Please post your question here;

  11. #11
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: If anything in cell.. then put period in cell beside

    Trying to do this: If anything in column D, then put period in column E.

    ALSO

    If anything in column A or B, then put the current date in column I. (code below for this)


    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
            With Target
                If .Count > 1 Then Exit Sub
                
                
                If Not Intersect(Range("A5:A2000"), .Cells) Is Nothing Then
                    Application.EnableEvents = False
                    If IsEmpty(.Value) Then
                        .Offset(0, 8).ClearContents
                    Else
                        With .Offset(0, 8)
                            .NumberFormat = "dd-mmm-yy"
                            .Value = Date
                        End With
                    End If
                    Application.EnableEvents = True
                End If
                
                If Not Intersect(Range("b5:b2000"), .Cells) Is Nothing Then
                    Application.EnableEvents = False
                    If IsEmpty(.Value) Then
                        .Offset(0, 7).ClearContents
                    Else
                        With .Offset(0, 7)
                            .NumberFormat = "dd-mmm-yy"
                            .Value = Date
                        End With
                    End If
                    Application.EnableEvents = True
                End If
                
                
            End With
        End Sub

    When I use the above together with the below... they clash. Any way around this?

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    On Error Resume Next
    If Not Intersect(Target, Range("D:D")) Is Nothing Then    
    
    Application.EnableEvents = False
        If Activecell <> "" Then
            Activecell.Offset(, 1) = "."
        Else
            Activecell.Offset(, 1) = ""
        End If
        Application.EnableEvents = True
    
    End If 
    
    
    End Sub

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: If anything in cell.. then put period in cell beside

    I did not/do not/will not use an ACTIVECELL reference in a ws_change macro. Didn't we talk about this already?


    There can only be one ws_change macro. Anything you want will need to be built into the one macro.
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    
    On Error Resume Next
    Application.EnableEvents = False
    
    For Each cell In Target
        If cell.Row >= 5 Then
            Select Case cell.Column
                Case 1, 2   'column A or B
                    If cell <> "" Then
                        Range("I" & cell.Row).Value = Format(Date, "dd-mmm-yy")
                    Else
                        Range("I" & cell.Row).Value = ""
                    End If
                Case 4  'column D
                    If cell <> "" Then
                        cell.Offset(, 1) = "."
                    Else
                        cell.Offset(, 1) = ""
                    End If
            End Select
        End If
    Next cell
    
    Application.EnableEvents = True
    End Sub

  13. #13
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: If anything in cell.. then put period in cell beside

    Gotcha... thank you!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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