1

Okay, so my problem is the following. What I'm basically doing is that I have a patientfile, on sheet1, I have some basic information of each patient. Column A of that page is manually edited. It is our "main page". We have 25 rooms, and usually all rooms are filled. So when we get a new patient, it will be entered on the line of where an old patient was.

On sheet2 I have extended information on each patient. The patientname is taken from sheet1 and after that comes extended information of the patient. Sheet2 can be sorted in different ways, for example, last name of patient, room number etc. So the patients won't always be in the same order as they are on sheet 1.

To explain what I want is the following: Whenever a patient gets discharged, I want the extended information of that patient cleared in sheet2, as it needs to be "reset" for the information of the new patient.

Below are images of what I mean:

Before new patient comes in Sheet1 Sheet2

New patient comes in Patient6 was replaced with Patient12 on sheet1, thus on sheet2 the extra information for Patient6 (which now stands with Patient12) was removed. Sheet1 Sheet2 Like this, extended information for patient12 can be added again, without risking that extend information of the previous patient sticks around and suddenly belongs to patient12

Like these images, the same goes for all other cells in sheet1.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oSheet1 As Worksheet
    Dim oSheet2 As Worksheet

    Dim oLookFor As Range
    Dim oFound As Range
If Not Intersect(Target, Columns(1)) Is Nothing Then


    Set oSheet1 = ThisWorkbook.Worksheets("Blad1")
    Set oSheet2 = ThisWorkbook.Worksheets("Blad2")

    Set oLookFor = oSheet1.Range("A1")

    Set oFound = oSheet2.Columns(1).Find(what:=oLookFor.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

    If Not oFound Is Nothing Then
        oFound.Range("B" & Target.Row & ":D" & Target.Row).ClearContents
    Else
        MsgBox oLookFor.Value & " not found on sheet " & oSheet2.Name
    End If

End If
End Sub

The above code, whether it be with .Columns(1) or .Range(A:A) will only work for 1 cell, because the oLookFor is set to just look at sheet1 A1.

That's where I miss the part which I can't figure out.

If the patient that gets switched is in cell A1, then sure, the code does what it has to do. It looks at the patients name in A1, searches this name and removes the extra information in sheet2. But what I now need to add, that if a patient's name in cell A3 changes, search the new name in sheet2 and remove the extra information. Same goes for the other cells.

ALSO: it is never the case that ALL patients change at once, it goes one by one.

I'm guessing for it to work, I would have to check which values in cells A1 to A5 actually change. Then only for the cell that changes lookup the value in sheet2 and clear the corresponding row. But I really have no idea how to set this up...

Update Been fooling around myself some more. By combining the code of @Dschuli and some standard example on how to use a For Each cell statement I've managed to make it work :) Below is the code which does what I want:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim oSearchArea As Range
    Dim oSheet2 As Worksheet

    Dim oLookUpArea As Range
    Dim oFound As Range

    Set oSearchArea = Intersect(Target, Target.Parent.Range("A1:A5"))
    Set oSheet2 = ThisWorkbook.Worksheets("Blad2")
    Set oLookUpArea = oSheet2.Columns(1)

    If Not oSearchArea Is Nothing Then
        Application.EnableEvents = False 'pervent triggering another change event

        Dim Cel As Range
        For Each Cel In oSearchArea.Cells
            Set oFound = oLookUpArea.Find(what:=Cel.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

            If Not oFound Is Nothing Then
                oFound.Columns("B:D").ClearContents
            Else
                MsgBox Cel.Value & " not found on sheet " & oSheet2.Name
            End If
        Next Cel

        Application.EnableEvents = True 'don't forget to re-enable events in the end
    End If
End Sub
juzbeingme
  • 11
  • 2
  • You can use `.Find` with `Worksheet_Change` in sheet 1 to check for the value in Col B of Sheet2? – Siddharth Rout Aug 29 '19 at 17:00
  • how would i combine. find with .clearcontent as I'm new with VBA and couldn't really find an example... – juzbeingme Aug 29 '19 at 17:18
  • See [This](http://www.siddharthrout.com/index.php/2018/01/05/find-and-findnext-in-excel-vba/) on how to use `.Find`. You can incorporate that code in the `Worksheet_Change`. Give it a try and then we will take it from there. – Siddharth Rout Aug 29 '19 at 23:30
  • Thanks for the help Siddharth, got the updated code above. Leads me to the next "problem" though. Any hints on how to get that done? – juzbeingme Aug 30 '19 at 10:22
  • `but can I also change this code so it will look at changes in all 25 cells of column A Sheet 1 and looks up the corresponding value?` Yes! :) Hint: change `Range("A1")` in `If Not Intersect(Target, Range("A1")) Is Nothing Then` ;) Btw you have managed on your own quite well! :) – Siddharth Rout Aug 30 '19 at 10:25
  • Thankyou :) Though that hint I don't quite understand. I know normally you would set the Range in "If Not Intersect" from "A1" to "A:A". But now there's also the oLookFor which is set to "A1". Therefore they don't match up. What I need is that if A1 sheet 1 is changed it will look for the value of A1 in sheet 2 column A:A; if A2 sheet 1 is changed it will look for the value of A2 in sheet 2 column A:A etc... Right now when I set If Not Intersect(Target, Range("A1")) Is Nothing Then to If Not Intersect (Target, Range("A:A")) I get "Types do not match" error on Set oFound... – juzbeingme Aug 30 '19 at 11:57
  • I am on the road now. Gimme couple of hours... – Siddharth Rout Aug 30 '19 at 14:18

2 Answers2

0

but I have 25 values it has to look at.

You have got most of what you wanted. As for the last part, set your range accordingly as shown below and then use that as I hinted in the comment above.

Is this what you are trying?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    '~~> This is the range which you want to capture
    '~~> amend it as per your requirements
    Set rng = Range("A1,D1,E4,G8,H14")

    If Not Intersect(Target, rng) Is Nothing Then
        '
        '~~> Do what you want
        '
        MsgBox Target.Address
    End If
End Sub

On a side note, since you are working with Worksheet_Change, I would recommend you give a glance at This Thread as well.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • I'm really new to VBA. Using your code does get rid of the error I got, however, if I add my own code below `if intersect` (`dim oSheet` etc.) it starts randomly clearing the rows on the other sheet. Tried setting `.Find(what:...` value to `rng`, tried to use a `For each cell` loop...nothing seems to work :/. Really at a loss here now... – juzbeingme Aug 31 '19 at 06:24
  • Can you update the question with the latest code that you are using? Also what do you expect that code should be doing and what is it actually doing. – Siddharth Rout Aug 31 '19 at 07:58
  • So if you want to check for values in just col A then use `If Not Intersect(Target, Columns(1)) Is Nothing Then` – Siddharth Rout Aug 31 '19 at 13:50
  • edited my question once more, now also with images and more detailed info on what the file is about. Maybe that makes it more clear where I want to go with this. – juzbeingme Aug 31 '19 at 15:09
0

A shot at your problem - assuming you always look/search in column 1 ("A") and you're target area does not contain any blank cells.

Version 3 now as stated in the comment below.

Private Sub Worksheet_Change(ByVal Target As Range)         'Version 3
    Dim oSheet1 As Worksheet
    Dim oSheet2 As Worksheet

    Dim oSensitiveArea As Range
    Dim oLookUpArea As Range
    Dim relevantChanges As Range
    Dim oFound As Range
    Dim oLookFor As Range

    Dim columnsToClear As String

     Set oSheet1 = ThisWorkbook.Worksheets("Blad1")
     Set oSheet2 = ThisWorkbook.Worksheets("Blad2")

    'Define the ranges that the event procedure should react on and where to look for
    'In this case its the first column of the two sheets
    Set oSensitiveArea = oSheet1.Columns(1)
    Set oLookUpArea = oSheet2.Columns(1)
    columnsToClear = "B:D"

    Set relevantChanges = Intersect(Target, oSensitiveArea)

    If Not relevantChanges Is Nothing Then

        For Each oLookFor In relevantChanges.Cells

            If Len(oLookFor.Value) = 0 Then Exit For               'Stop the loop if a blank cell (length = 0) is encountered

            Set oFound = oLookUpArea.Find(what:=oLookFor.Value, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

            If Not oFound Is Nothing Then
                oFound.EntireRow.Columns(columnsToClear).ClearContents
            Else
                MsgBox oLookFor.Value & " not found on sheet " & oSheet2.Name
            End If

        Next oLookFor

    End If

End Sub
Dschuli
  • 309
  • 3
  • 10
  • i believe ````oLookUpArea = oSheet1```` should be ````oLookUpArea = oSheet2```` but when i test the code I get a "Types do not match" error on ````If Len(oLookFor.Value) = 0 Then````. I also got this error when I did not change oLookUpArea – juzbeingme Aug 31 '19 at 10:15
  • Too quick ann not tested ... my fault .. sorry...... Made the corrections. – Dschuli Aug 31 '19 at 11:02
  • no error anymore, but 2 problems with this code: 1) It can find EVERY value on Sheet2 so it clears everything, what I need is that it only clears the row of which the changed value is on. 2) It now clears the entire row, I need it to clear columns B:D, but keep the new value in A. weirdly enough if I change ````oFound.EntireRow.ClearContents```` to ````oFound.Range("B" & oFound.Row & ":D" & oFound.Row).ClearContents```` it ALWAYS clears every other row... – juzbeingme Aug 31 '19 at 11:23
  • re 1) it will find all currencies of the value in the changed cell (value after the change!!) in column A only (all of column A) but I guess I misunderstood what you meant by .... in all 25 cells.... Have changed the code to act on all changed cells (Version 3 above) Re 2) Here I need to better understand what you want to achieve: On a cell change in sheet 1/Column A: Do you want to delete Columns B:D of a find in Sheet 2 based on the old value of the changed cell? And replace Column A with the new value? Doable, but more tricky. Let me know. – Dschuli Sep 01 '19 at 10:47