VB error message "Merged cell", but no merged cells in sheet

Alex B

New Member
Joined
Jan 22, 2015
Messages
12
Hi,

set up this code to filter out unique names from a list of consultants:

Code:
Private Sub Worksheet_Activate()

    Application.ScreenUpdating = False
        
    Sheets("Resurstid").Visible = True
    
    With Sheets("Resurstid")
    
    Range("A:F").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
        "H1:M2"), CopyToRange:=Range("H4"), Unique:=True
        
    End With
        
    Sheets("Resurstid").Visible = False
    
    Application.ScreenUpdating = True
    
   Sheets("Analys per konsult").Select
   
   
End Sub


Get the error message : 'Run time error 1004: We casn't do that to a merged cell'. The following part of the code is marked in the debugger:

Code:
    Range("A:F").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
        "H1:M2"), CopyToRange:=Range("H4"), Unique:=True



Though, there are no merged cells in the sheet where I try to do the advanced filtering. Where am I doing wrong???
 
Try adding a stop before each range, otherwise you are working on the active sheet rather than sheet Resurstid
Code:
With Sheets("Resurstid")
    
    [COLOR=#ff0000].[/COLOR]Range("A:F").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=[COLOR=#ff0000].[/COLOR]Range( _
        "H1:M2"), CopyToRange:=[COLOR=#ff0000].[/COLOR]Range("H4"), Unique:=True
        
End With
 
  • Like
Reactions: GTO
Upvote 0
Code worked for me

I suspect you do have a merged cell there somewhere


I have checked, and even selected the whole sheet "Resurstid" and unmerged in case there were some merged cells. Still get the same error message. Am I referring to the right sheet in the code????
 
Upvote 0
I'd agree with Stiuart....i'm guessing there is one there somewhere....maybe even a vertical merged cell ??
How about posting the worksheet to a hosting site like dropbox or mediafire and then posting a link back here.
That way someone may be able to take a look at it !
you could also run this macro on the activesheet and see if that detects any merged cells
Code:
Sub findmerged()
    Dim c
    For Each c In ActiveSheet.UsedRange
        If c.MergeCells Then
            MsgBox c.Address & " is merged"
        End If
    Next
End Sub
 
Upvote 0
I have checked, and even selected the whole sheet "Resurstid" and unmerged in case there were some merged cells. Still get the same error message. Am I referring to the right sheet in the code????

Have you tried qualifying the ranges as per post #3?
 
Upvote 0
Warning: "air code"

Not tested, but regardless of whether there is an unknown merged cell, you cannot go wrong in being more explicit as Fluff suggests:

Rich (BB code):
Option Explicit
  
Private Sub Worksheet_Activate()
  
  Application.ScreenUpdating = False
  
  'Sheets("Resurstid").Visible = True
  'Work against this workbook (the one with the code) and specifically the worksheet 'Resurstid'
  With ThisWorkbook.Worksheets("Resurstid")
    
    .Visible = xlSheetVisible
    'This way, everything with a stop before it, as Fluff advised, is specific to this worksheet...
    'regardless of what worksheet or even which workbook is active/has focus.
    .Range("A:F").AdvancedFilter Action:=xlFilterCopy, _
                                         CriteriaRange:=.Range("H1:M2"), _
                                         CopyToRange:=.Range("H4"), _
                                         Unique:=True
    
    .Visible = xlSheetHidden
  End With
  'Sheets("Resurstid").Visible = False
  
  Application.ScreenUpdating = True
  Sheets("Analys per konsult").Select
   
End Sub

Hope that helps,

Mark
 
Upvote 0
Warning: "air code"

Not tested, but regardless of whether there is an unknown merged cell, you cannot go wrong in being more explicit as Fluff suggests:

Rich (BB code):
Option Explicit
  
Private Sub Worksheet_Activate()
  
  Application.ScreenUpdating = False
  
  'Sheets("Resurstid").Visible = True
  'Work against this workbook (the one with the code) and specifically the worksheet 'Resurstid'
  With ThisWorkbook.Worksheets("Resurstid")
    
    .Visible = xlSheetVisible
    'This way, everything with a stop before it, as Fluff advised, is specific to this worksheet...
    'regardless of what worksheet or even which workbook is active/has focus.
    .Range("A:F").AdvancedFilter Action:=xlFilterCopy, _
                                         CriteriaRange:=.Range("H1:M2"), _
                                         CopyToRange:=.Range("H4"), _
                                         Unique:=True
    
    .Visible = xlSheetHidden
  End With
  'Sheets("Resurstid").Visible = False
  
  Application.ScreenUpdating = True
  Sheets("Analys per konsult").Select
   
End Sub

Hope that helps,

Mark


Mark, perfect!!

Thanks for your efforts, guys!!!
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top