0

Having some issue with a simple macro. I want to replace retrieve the unique values of column B in worksheet FLMs and paste it to another sheet. It works fine normally but only when I have the FLMs worksheet open and then run the macro. However, the macro button is placed in a different sheet and when I run it from there it returns an error.

This is the code that is used:

    Sub GeheleMacro()
Dim lastrow As Long
Dim ws As String
lastrow = Cells(Rows.Count, "B").End(xlUp).Row

Sheets("FLMs").Range("B4:B" & lastrow).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=Sheets("NIET VERWIJDEREN").Range("A1"), _
    UNIQUE:=True
   End sub

I already tried using 'Worksheet("FLMs").Activate' after defining lastrow but that didn't work.

  • 1
    `lastrow` is calculated for the active sheet. If `Sheets("FLMs")` is not activated, the variable is wrongly loaded. If this is the problem, try fully qualifying the used range: `lastrow = Sheets("FLMs").Cells(Rows.Count, "B").End(xlUp).Row`. – FaneDuru Feb 15 '22 at 12:29
  • Thank you, that solved my problem! – Fastbanana Feb 15 '22 at 15:41

0 Answers0