• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Skip missing file with macro

Anjamen

New Member
Hi,
I would need some help.
I've attached a code and I have 9 of them for each file separately and they are all the same, only thing that differs is a name of file that each code should enter and the name of macro. The name of file is a name of brand manager which is also a name of each macro. E.g. code:

Code:
Sub Sandra()

  Dim wkb As Workbook, wkbFrom As Workbook
Dim fromPath As String
fromPath = Sheets("UPUTSTVO").Range("B12")
If Right(fromPath, 1) <> "\" Then fromPath = fromPath & "\"
Set wkb = ThisWorkbook
Application.EnableEvents = False
Set wkbFrom = Workbooks.Open(fromPath & "SANDRA.XLSM")
  Windows("SANDRA.xlsm").Activate
  Sheets("TABELA").Select
  lastrow = Cells(Rows.Count, "E").End(xlUp).Row
  Range("A3:O" & lastrow).Select
  Selection.Copy
  Windows("BAZA.xlsm").Activate
  Sheets("TABELA").Select
  lastrow1 = Cells(Rows.Count, "E").End(xlUp).Row
  Range("A" & lastrow1 + 1).Select
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  :=False, Transpose:=False

  Windows("SANDRA.xlsm").Activate
  Sheets("TABELA").Select
  lastrow = Cells(Rows.Count, "E").End(xlUp).Row
  Range("W3:AN" & lastrow).Select
  Selection.Copy
  Windows("BAZA.xlsm").Activate
  Sheets("TABELA").Select
  lastrow1 = Cells(Rows.Count, "E").End(xlUp).Row
  Range("W" & lastrow1 - lastrow + 3).Select
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  :=False, Transpose:=False
  Application.CutCopyMode = False

  Windows("SANDRA.xlsm").Activate
  Sheets("TABELA").Select
  lastrow = Cells(Rows.Count, "E").End(xlUp).Row
  Range("BG3:BG" & lastrow).Select
  Selection.Copy
  Windows("BAZA.xlsm").Activate
  Sheets("TABELA").Select
lastrow1 = Cells(Rows.Count, "E").End(xlUp).Row
  Range("BG" & lastrow1 - lastrow + 3).Select
  Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
  :=False, Transpose:=False
  Application.CutCopyMode = False
  Windows("SANDRA.xlsm").Close Saved = False

End Sub
I also made a single code that calls/uses all of these 9 codes to connect with names and you can see that here

Sub Baza()

Call ANJA
Call JELENA_B
Call MILA_I_VESNA
Call MILENA
Call NATASA
Call NEDA
Call NIKOLA
Call RADA
Call SANDRA

End Sub

The main problem is if there is no e.g. file SANDRA that macro SANDRA should enter, the code breaks. How can I fix this - to set a code to skip macro if its file is missing.
Thnx a lot
 
Last edited by a moderator:
Without going through all your code, I guess this is what you want.
Code:
Sub Baza()
On Error Resume Next
Call ANJA
Call JELENA_B
Call MILA_I_VESNA
Call MILENA
Call NATASA
Call NEDA
Call NIKOLA
Call RADA
Call SANDRA
End Sub
 
This really should be restructured to call a common module and pass the different name to each

Eg:

Code:
Sub Baza()
OnErrorResumeNext
Call mymacro(“ANJA”)
Call mymacro(“JELENA_B”)
Call mymacro(“MILA_I_VESNA”)
Call mymacro(“MILENA”)
Call mymacro(“NATASA”)
Call mymacro(“NEDA”)
Call mymacro(“NIKOLA”)
Call mymacro(“RADA”)
Call mymacro(“SANDRA”)
EndSub



Sub Mymacro(name as string)

‘Existing code here
‘ use name as File name

End sub
 
I just put the sentence "On Error Resume Next" to the code and it works for now. :) Thanks a lot, you made my Monday at firm much easier.
 
Back
Top