0

Getting the above run time error while executing the code second time. First time it is executing successfully. I have attached the code to which it points when I go to Debug mode. Please help to resolve this issue. Code:-

    wbExcel.Worksheets("No of risks per user").Activate
    wbExcel.ActiveSheet.UnProtect Password:="pwd1"
    wbExcel.ActiveSheet.Columns("M:M").Select     
    Selection.EntireColumn.Hidden = False  ' It points to this line in debug mode 
    Selection.RemoveDuplicates Columns:=1, Header:=xlYes
    Selection.EntireColumn.Hidden = True
Community
  • 1
  • 1
  • can you show more code `wbExcel`? – 0m3r Mar 08 '18 at 02:05
  • if you're running this code form Access, then the plain `Selection` reference will point to Access `Selection` object (whatever it might be). To reference the Excel sheet columns selections than you have to go as per @L42 answer – DisplayName Mar 08 '18 at 08:23
  • You should avoid using `.Activate`, `.Select` and other `Selection.` when possible. See [How to avoid using select](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) – Vincent G Mar 08 '18 at 15:23

1 Answers1

1

I can't see anything out of the ordinary except referencing issue. You might want to try below and see if it will generate the same error.

With wbExcel.Worksheets("No of risks per user")
    .UnProtect Password:="pwd1"
    .Columns("M").Hidden = False
    .Columns("M").RemoveDuplicates Columns:=1, Header:=xlYes
    .Columns("M").Hidden = True
End With
L42
  • 19,427
  • 11
  • 44
  • 68