Problem: Windows(CurrentFileName).Activate not executed in the second piece of code, although everything is in order in the first piece of code. That is, the CurrentFileName variable in the second version is empty. What could be the reason? - Tell me please.

 Dim M3_FullPath As String, CurrentFileName As String CurrentFileName = ActiveSheet.Parent.name 

'Piece of code 1

 Windows(M3_FileName).Activate Sheets("M3").Select ActiveSheet.Unprotect Password:=Password ActiveSheet.Range("$A$1:$Q$1048576").AutoFilter ActiveSheet.Range("$A$1:$Q$1048576").AutoFilter Field:=1, Criteria1:=ArrayTM, Operator:=xlFilterValues n = Range("A1").End(xlDown).Row Range("B2:Q" + CStr(n)).Copy Windows(CurrentFileName).Activate Sheets(nameM3).Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False 

'Piece of code 2

 Windows(M3_FileName).Activate Sheets("ТИ").Select ActiveSheet.Unprotect Password:=Password ActiveSheet.Range("$A$1:$P$1048576").AutoFilter ActiveSheet.Range("$A$1:$P$1048576").AutoFilter Field:=1, Criteria1:=ArrayTM, Operator:=xlFilterValues n = Range("A1").End(xlDown).Row Range("B2:P" + CStr(n)).Copy Windows(CurrentFileName).Activate Sheets(nameDI).Range("B2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False 

Closed due to the fact that PashaPash 11 Dec '16 at 17:48 is off topic.

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • It is not clear when you assign the name of the parent book to a variable ... It is possible that the CurrentFileName variable is cleared before processing the second code. - vikttur
  • @vikttur, not, not cleared the fact of the matter. - Artem Nov.
  • @vikttur, this variable was set once and everything and is used in 4 places. I now think maybe the problem with the visibility of something. Just in CurrentFileName the essence is that there is a book and sheets in it, so here the sheets are assigned their parent - that is, this book itself. In the first piece of code, everything is normally assigned, and in the second piece, the variable is empty - Artem
  • If the variable is empty, it is reset to somewhere. For example, a value is assigned in one procedure, and used in another, with the variable level of the procedure. - vikttur
  • @vikttur, thanks! I solved the problem. It turned out not to be a macro at all, but to expand the files that were uploaded. - Artem

0