+ Reply to Thread
Results 1 to 3 of 3

VBA Code to clearcontents

  1. #1
    Registered User
    Join Date
    02-05-2016
    Location
    nottingham
    MS-Off Ver
    7
    Posts
    13

    VBA Code to clearcontents

    Afternoon Guys,

    I'm try to make these codes work the first code is the following,

    I input a value in row B once inputed it moves to Row A then should clear from Row B its does couple of times.

    CODE
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myval As Variant
    On Error Resume Next
    If Application.Intersect(Target, Range("B1:B5")) _
    Is Nothing Then
    Exit Sub
    Else
    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value + myval
    End If
    On Error GoTo 0
    Sheet1.Range("B1:B5").Select
    Selection.ClearContents
    Sheet1.Range("B1:B3000").Select
    End Sub

    My next issue is

    i want to run the same code but with

    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value - myval
    End If

    but in row G

    Your help will be must appreciated

    Regards
    Paul

  2. #2
    Registered User
    Join Date
    04-14-2012
    Location
    egypt
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: VBA Code to clearcontents

    try this
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-14-2012
    Location
    egypt
    MS-Off Ver
    Excel 2013
    Posts
    8

    Re: VBA Code to clearcontents

    try this
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    02-05-2016
    Location
    nottingham
    MS-Off Ver
    7
    Posts
    13

    Re: VBA Code to clearcontents

    hi this code keeps coming back with a error object range fail

  5. #5
    Registered User
    Join Date
    02-05-2016
    Location
    nottingham
    MS-Off Ver
    7
    Posts
    13

    Re: VBA Code to clearcontents

    the first part of this code works fine its the second where my problem is as i'm trying subtract the value

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myval As Variant
    On Error Resume Next
    If Application.Intersect(Target, Range("B1:B3000")) _
    Is Nothing Then
    Exit Sub
    Else
    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value + myval
    End If
    With ActiveSheet
    Intersect(.UsedRange, .Columns("B")).ClearContents

    End With
    End Sub
    Private Sub Worksheet_Change1(ByVal Target As Range)
    Dim myval As Variant
    On Error Resume Next
    If Application.Intersect(Target, Range("G1:G5")) _
    Is Nothing Then
    Exit Sub
    Else
    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value - myval
    End If
    With ActiveSheet
    Intersect(.UsedRange, .Columns("G")).ClearContents
    End Sub

  6. #6
    Registered User
    Join Date
    02-05-2016
    Location
    nottingham
    MS-Off Ver
    7
    Posts
    13

    Re: VBA Code to clearcontents

    the first part of this code works fine its the second where my problem is as i'm trying subtract the value

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim myval As Variant
    On Error Resume Next
    If Application.Intersect(Target, Range("B1:B3000")) _
    Is Nothing Then
    Exit Sub
    Else
    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value + myval
    End If
    With ActiveSheet
    Intersect(.UsedRange, .Columns("B")).ClearContents

    End With
    End Sub
    Private Sub Worksheet_Change1(ByVal Target As Range)
    Dim myval As Variant
    On Error Resume Next
    If Application.Intersect(Target, Range("G1:G5")) _
    Is Nothing Then
    Exit Sub
    Else
    myval = Target.Value
    Target.Offset(0, -1).Value = _
    Target.Offset(0, -1).Value - myval
    End If
    With ActiveSheet
    Intersect(.UsedRange, .Columns("G")).ClearContents
    End Sub

+ 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] Using Range.clearcontents causing type mismatch error in code
    By Captian LDS in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-28-2014, 04:54 PM
  2. [SOLVED] ClearContents with IF statement
    By cbrd in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2012, 09:52 AM
  3. [SOLVED] VBA .ClearContents code
    By SHELTONUNDERDOG in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-10-2012, 09:13 PM
  4. [SOLVED] ClearContents query
    By TF10 in forum Excel General
    Replies: 6
    Last Post: 07-18-2012, 06:07 AM
  5. ClearContents
    By GeneralShamu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-16-2011, 09:28 AM
  6. ClearContents
    By tqm1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-22-2007, 08:47 AM
  7. question about ClearContents
    By Mattew in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2005, 12:05 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