简体   繁体   中英

Excel, VBA: Referencing other Sheets when a Value changes in 1 sheet (Intersect)

I have been trying to get this to work, but I couldn't so far, and after searching I couldn't quite find a solution online either, so here it goes.

I have 3 sheets I'm using.

"wsPunting" (The one where the value changes)
"wsDetail" (The Sheet with complete data of everything)
"wsData" (The Sheet where I store certain data that I grab with macros)

Now, what I'm trying to do, is that when a Value changes in cell B2 in wsPunting (B2 is Data Validation made with a macro, not sure if this is valuable info, but better put it in here just in case), I filter my data in wsDetail, grab Column "O3:O", Remove Duplicates, and Assign that into a Data Validation in Cell B5 in wsPunting.

I got this to work already when I had <20 values entered. Now, when I pasted the actual Data that I was going to use in wsDetail, I kept getting "Type Mismatch" on the Intersect.

I've tried a few things already that I though might perhaps fix it, but I just can't seem to find it.


Private Sub Worksheet_Change(ByVal Target As Range)

    Application.ScreenUpdating = False

    On Error GoTo Booboo

    Dim rngFSU As Range
    Dim vFSU As Range
    Dim wsPunting As Worksheet, wsData As Worksheet, wsDetail As Worksheet

    Set wsPunting = ActiveWorkbook.Sheets("Puntingsblad")
    Set wsData = ActiveWorkbook.Sheets("Data")
    Set wsDetail = ActiveWorkbook.Sheets("Detail")

    Set rngFSU = wsPunting.Range("$B$2")
    Set vFSU = wsPunting.Range(Target.Address)

'The next line is where it keep dropping the error
    If Not Intersect(rngFSU, vFSU) Then

        wsDetail.Range("A2", wsDetail.Range("A3").SpecialCells(xlCellTypeLastCell)).AutoFilter Field:=1, Criteria1:=Range("B2").Value
        wsDetail.Range("O3", wsDetail.Range("O3").SpecialCells(xlCellTypeLastCell)).Copy

        wsData.Range("B2").PasteSpecial xlPasteValues
        wsData.Range("B2", wsData.Range("B1").End(xlDown)).RemoveDuplicates Columns:=Array(1)

        With wsPunting.Range("B5").Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="=Data!" & wsData.Range("B2", wsData.Range("B1").End(xlDown)).Address
            .IgnoreBlank = True
        End With

        wsDetail.Range("A2", wsDetail.Range("A3").SpecialCells(xlCellTypeLastCell)).AutoFilter Field:=1
    End If

Booboo:
    MsgBox err.Description
End Sub

This is actually my last resort, concidering I usually try fixing it myself to learn faster. But I've been stuck on this one for so long already, I just can't anymore atm.

Cheers in advance.

If Not Intersect(rngFSU, vFSU) Then

Intersect(rngFSU, vFSU) is an object of type Range , you are trying to cast it into a boolean.

This leads to an error. I understand that you want to check if there is no intersection, in which case the Range returned by Intersect is Nothing . So try this:

 If Intersect(rngFSU, vFSU) Is Nothing Then

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM