dcsimg
Results 1 to 7 of 7

Thread: Visual Basic Private Sub won't run

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    2

    Visual Basic Private Sub won't run

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Intersect(Target, Range("q:q")) Is Nothing Then
    With Target(1, 2)
    .Value = Date & " "
    .EntireColumn.AutoFit
    End Sub

    I have found several similar versions of this, but none of them function. I feel like I'm just missing the on switch.

    Excel in Office home recently updated. Windows 10 computer.

    Bill

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,415

    Re: Visual Basic Private Sub won't run

    Welcome to VBForums

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,852

    Re: Visual Basic Private Sub won't run

    please describe what you want it to actually do

    it would appear that if the cell changed is not in column Q then put the date and a space into the cell in the next column, which does not really make sense to me

    if you put a break point on some line in that sub then you can confirm that the event is firing
    BUT as there is 2 errors in the code, if it was running a break would occur as soon as the sub fired
    the sub MUST be on the code pane for the sheet where the change is to happen
    best to use the drop down boxes at the top of the code pane, rather than just paste the procedure onto some code pane

    target(1, 2) is the same as target.offset(, 1)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Visual Basic Private Sub won't run

    As Pete says, a description of what you're trying to accomplish would be helpful.

    Having said that, if you fix the errors he mentions, I don't think you'd like the results. If you change a value in column A, it will write a date in column B, same row. That will then write to C, which will write to D, etc. until you get to Q, which is where it will stop. Is that what you want?

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2020
    Posts
    2

    Re: Visual Basic Private Sub won't run

    No. If I place a value in blank cell a I want a date recorded in blank cell b. I have this resolved I finally found a code that worked. The only thing I would like to do now is figure out how to do the exact same thing in a separate place on the sheet with a separate entry. For example, if I put an entry in column k I want a date recorded in column j. I can make that code work just like the previous one but I can't have them both running at once. In other words, I can make an entry in the AB situation or I can make an entry in the KJ situation but I can't figure out how to make the code work for both.k

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,569

    Re: Visual Basic Private Sub won't run

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("a:a")) Is Nothing Then
            MsgBox "do something based on change in column A"
        ElseIf Not Intersect(Target, Range("k:k")) Is Nothing Then
            MsgBox "do something based on change in column K"
        End If
    End Sub

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,852

    Re: Visual Basic Private Sub won't run

    if you wanted to do the same thin for column A or column K
    instead of the intersects, you can try lie
    Code:
    if target.column = 1 or target.column = 11 then target.offset(,1) = date
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width