Closed Thread
Results 1 to 4 of 4

VBA for Automation

Hybrid View

joone VBA for Automation 04-05-2007, 12:40 AM
Leith Ross Hello Joone, The Intersect... 04-05-2007, 01:05 AM
joone Thanks for your reply Leith.... 04-05-2007, 01:25 AM
Leith Ross Hello Joone, The code... 04-05-2007, 02:06 AM
  1. #1
    Registered User
    Join Date
    04-04-2007
    Posts
    4

    VBA for Automation

    Thread Locked. Code not wrapped


    I need help with the VBA I got. Below is my spreadsheet. I basically want Column B updated autmatically when I put numbers on Column C and D. Below that is the code that needs editing/adding. Right now, Column C is working but not for Column D. Please help. Thnx.

    (Spreadsheet):
    A1: Item Name B1: Current Balance C1: Daily Usage (-) D1: Restock (+)
    A2: Item Alpha B2:45 C2:______ D2:______
    A3: item Bravo B3: 120 C3:______ D3: ______

    (VBA):
    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "C8:C24" '

    On Error GoTo ws_exit
    Application.EnableEvents = False

    If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
    With Target
    If IsNumeric(.Value) Then
    If IsNumeric(.Offset(0, -1).Value) Then
    .Offset(0, -1).Value = .Offset(0, -1).Value - .Value
    .Value = ""
    End If
    End If
    End With
    End If

    ws_exit:
    Application.EnableEvents = True
    End Sub

    I hope my request is clear enough. Thanks in advance for your replies.
    Last edited by VBA Noob; 04-05-2007 at 02:40 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Joone,

    The Intersect method returns a Range object that represents the rectangular intersection of two or more ranges. Target is the cell or range of cells the user has selected on the worksheet. If the result of the intersection is Nothing then the users selection was not in the Range of interest. If it is Not Nothing you need to use the Range object returned by the Intersect method, not Target.

    Private Sub Worksheet_Change(ByVal Target As Range)
      Dim CellX As Range
      Const WS_RANGE As String = "C8:C24" '
    
        On Error GoTo ws_exit
        Application.EnableEvents = False
    
        Set CellX  =  Intersect(Target, Me.Range(WS_RANGE))
          If CellX Is Nothing Then Exit Sub    
    
        With CellX
          If IsNumeric(.Value) Then
             If IsNumeric(.Offset(0, -1).Value) Then
                .Offset(0, -1).Value = .Offset(0, -1).Value - .Value
                .Value = ""
             End If
          End If
        End With
    
    ws_exit:
    Application.EnableEvents = True
    End Sub
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    04-04-2007
    Posts
    4
    Thanks for your reply Leith. I copied and pasted your code but it's not doing what I wanted it done. The code that I posted was working but only for column C. I needed to edit the code to include column D.

    Thanks again.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Joone,

    The code works for me and I am using Windows Xp with Excel 2000. You don't say specify what "D" is to be or how the value is calculated. Post back with that info and I'll add it to the code.

    Sincerely,
    Leith Ross

Closed 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