Re-execute if the workbook exists or not using vba Excel -


i want execute, if workbook exists re- run if not exists create workbook.

i have uniques values(x) , array(names). need compare them if both equal if not has create workbook name of array(names) not had in uniques values(x)

my code:

sub mac()  dim c integer dim x range dim s_agingscm string dim array_scm_aging variant dim newbook workbook dim newbook_scm workbook dim master_workbook workbook dim rngcopy_aging range dim rngfilter_ws2 range  c = lbound(array_scm_aging) ubound(array_scm_aging)             set master_workbook = thisworkbook             s_agingscm = array_scm_aging(c, 1)             set x = master_workbook.sheets("bass").range("ay" & c)                     if x = s_agingscm                                                                                 rngfilter_ws2                                  .autofilter field:=32, criteria1:="<>(a)  0 - 360", operator:=xlfiltervalues                                 .autofilter field:=37, criteria1:=s_agingscm, operator:=xlfiltervalues                           set rngcopy_aging = .specialcells(xlcelltypevisible)                                              .autofilter ' switch off autofilter                          end                           rngcopy_aging.copy newbook.worksheets("aging inventory").cells(1, 1)                          application.displayalerts = false                               else                     dim fso: set fso = createobject("scripting.filesystemobject")                     dim folder: set folder = fso.getfolder("c:\")                     dim file, filenames                                         dim rngcopy_scmaging range                                         each file in folder.files                             if right(file.name, 4) = "xlsx"                             filenames = filenames & file.name & ";"         ' give list of filenames                             end if                     next                      if instr(filenames, s_agingscm) = 0                                                   newbook_scm                                                              set newbook_scm = workbooks.add                             .title = s_agingscm                           newbook_scm.worksheets("sheet1").name = "aging inventory"                             rngfilter_ws2                                  .autofilter field:=32, criteria1:="<>(a)  0 - 360", operator:=xlfiltervalues                                 .autofilter field:=37, criteria1:=s_agingscm, operator:=xlfiltervalues                                  set rngcopy_scmaging = .specialcells(xlcelltypevisible)                                                        .autofilter ' switch off autofilter                             end                                  rngcopy_scmaging.copy destination:=newbook_scm.worksheets("aging inventory").cells(1, 1)                                                        .saveas filename:="kpi" & " " & s_agingscm & " " & format_date & ".xlsx"                           application.displayalerts = false                           newbook_scm.close                                                    end '                      else                     end if  end sub 

i stuck here since 2 days. want if workbook exists overwrite new workbook or else if not exists create new workbook.

can please me out.

a quick way placing: -

if fso.fileexists(application.defaultfilepath & "\kpi" & " " & s_agingscm & " " & format_date & ".xlsx")     fso.deletefile application.defaultfilepath & "\kpi" & " " & s_agingscm & " " & format_date & ".xlsx", true end if 

above line

.saveas filename:="kpi" & " " & s_agingscm & " " & format_date & ".xlsx" 

but not account if file not deleted (i.e. open)


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -