+ Reply to Thread
Results 1 to 5 of 5

Variable doesn't seem to work

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2013
    Location
    UK
    MS-Off Ver
    Excel 20010
    Posts
    25

    Variable doesn't seem to work

    I'm having some trouble with the below bit of code. It's the first part of trying to make sure that users don't overwrite changes in a shared sheet. The next step is to check the value of the variable against the cell it has just been entered into, to see whether another user has already typed something into that cell. If someone has, it will move down the column to the next blank cell, enter the variable into that cell, and then attempt to save again. Hacky I know, but it was the best I could come up with.

    The problem is that the variable doesn't seem to be holding the value of the cell. I'm trying to get it to display into a MsgBox just so I can see if it's actually doing something. At the minute the MsgBox is blank.

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim inputcellvalue As String
    If ActiveCell.Column = 5 Then
    inputcellvalue = ActiveCell.Value
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs , , , , , , , ConflictResolution:=xlOtherSessionChanges
    ActiveCell.Offset(-1, 0).Select
    MsgBox inputcellvalue
    
    Else: End If
        
    End Sub
    Any ideas?
    Last edited by davepoth; 10-11-2013 at 08:26 AM.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Variable doesn't seem to work

    What is the "Target" range?

  3. #3
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Variable doesn't seem to work

    Hi davepoth,

    try the code below - I tried to explain it on the go

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icv$
    
    'If the change in any other column than column E, then exit sub
    If Target.Column <> 5 Then Exit Sub
    
    'Prevent the worksheet_change sub from firing itself over and over again :)
    Application.EnableEvents = False
    
    'If more than one cell at the same time is altered, undo the change and exit the sub.
    If Target.Count > 1 Then Application.Undo: Application.EnableEvents = True: Exit Sub
    
    
    'Turn of screenupdating for smoother and faster working
    Application.ScreenUpdating = False
    
    'Get the new value
    icv = Target.Value
    
    'Undo the change
    Application.Undo
    
    'Check if the cell was empty before the change - if so, put the value back into the cell, else put it at the bottom of the column
    If Target.Value <> "" Then Cells(Rows.Count, Target.Column).End(xlUp).Offset(1, 0).Value = icv Else Target.Value = icv
    Application.ScreenUpdating = True
    
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs , , , , , , , ConflictResolution:=xlOtherSessionChanges
    
    Application.EnableEvents = True
    Application.DisplayAlerts = True
    
    End Sub
    Please click the * below if this helps
    Please click the * below if this helps

  4. #4
    Registered User
    Join Date
    08-27-2013
    Location
    UK
    MS-Off Ver
    Excel 20010
    Posts
    25

    Re: Variable doesn't seem to work

    Just the job. Thanks!

  5. #5
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Variable doesn't seem to work

    Glad to hear it helped - please mark the thread as solved
    (Thread tools at the top, mark as solved)

+ 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. [SOLVED] Macro work in personal workbook, doesn't work in other workbooks
    By Centexcel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-30-2013, 11:47 AM
  2. [SOLVED] Macro Doesn't Work Through Button, Does Work Through Developer ->Macros Option
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-27-2013, 11:55 AM
  3. 7.2-0.1 doesn't work?
    By salmonrose in forum Excel General
    Replies: 2
    Last Post: 09-27-2011, 05:51 PM
  4. F 5 doesn't work right.
    By mrl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2010, 11:04 AM
  5. Variable doesn't work based on type
    By carsto in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-11-2007, 09:35 AM

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