Consulting

Results 1 to 4 of 4

Thread: This Should be Easy::Event Handler Maybe?

  1. #1

    This Should be Easy::Event Handler Maybe?

    I want to make it so that if a "$" symbol or the letter "c" is entered into a cell on WorkSheets(1) then the same symbol is automatically entered onto WorkSheets(2) in the same cell location.

  2. #2
    This works.... but I was hoping I wouldn't have to update the values of i and j everytime they change. And Maybe avoid looping altogether?

    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For i = 1 To 100
    For j = 1 To 100
    If Worksheets(1).Cells(i, j) = "c" Then
    Worksheets(2).Cells(i, j) = Worksheets(1).Cells(i, j)
    End If
    If Worksheets(1).Cells(i, j) = "$" Then
    Worksheets(2).Cells(i, j) = "$"
    End If

    Next j
    Next i

    End Sub
    [/vba]

  3. #3
    Quote Originally Posted by Saladsamurai
    This works.... but I was hoping I wouldn't have to update the values of i and j everytime they change. And Maybe avoid looping altogether?

    [vba]Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    For i = 1 To 100
    For j = 1 To 100
    If Worksheets(1).Cells(i, j) = "c" Then
    Worksheets(2).Cells(i, j) = Worksheets(1).Cells(i, j)
    End If
    If Worksheets(1).Cells(i, j) = "$" Then
    Worksheets(2).Cells(i, j) = "$"
    End If

    Next j
    Next i

    End Sub
    [/vba]


    Okay! Here is the problem with this: If I enter a "$" or "c" on 1 it gets placed in 2...that just Fine.

    BUT, if I delete a "$" or "c" from 1 it STAYS on 2. Not good.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Not anywhere near sure, but maybe:

    [vba]
    Option Explicit
    Dim bolWasChar As Boolean

    Private Sub Worksheet_Activate()
    If Selection.Count = 1 Then
    If Selection.Value Like "[Cc[$]" Then
    bolWasChar = True
    Else
    bolWasChar = False
    End If
    End If
    End Sub

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count = 1 Then
    If Target.Value Like "[Cc[$]" Then
    Sheet2.Range(Target.Address).Value = Target.Value
    ElseIf bolWasChar And Not Target.Value Like "[Cc[$]" Then
    Sheet2.Range(Target.Address).ClearContents
    End If
    End If
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count = 1 Then
    If Target.Value Like "[Cc[$]" Then
    bolWasChar = True
    Else
    bolWasChar = False
    End If
    End If
    End Sub
    [/vba]

    Hope that helps,

    Mark
    Last edited by GTO; 09-04-2009 at 06:03 PM.

Posting Permissions

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