Macro Correction(adding a line to kill the files)

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

Hi Experts,

Code: Select all

Sub STEP5testing()
    Dim ws As Worksheet, b2
    Dim Wb1 As Workbook, Wb2 As Workbook
    
    Set ws = Workbooks.Open("C:\Users\**I've been banned**\Desktop\1.xls").Worksheets(1)
    b2 = ws.[b2]
    ws.Parent.Close False
    
    With CreateObject("scripting.filesystemobject")
    If b2  "" Then
        Set Wb1 = Workbooks.Open("C:\Users\**I've been banned**\Desktop\**I've been banned**\9.15\STEP1.xlsb")
        Kill "C:\UsersWolfieeeStyle\Desktop\1.xls"
        Kill "C:\Users\**I've been banned**\Desktop\ap.xls"
        Kill "C:\Users\**I've been banned**\Desktop\oo.xls"
        Else
        Set Wb1 = Workbooks.Open("C:UsersWolfieeeStyleDesktopWolfieeeStyle9.15STEP3.xlsb")
        Kill "C:\Users\**I've been banned**\Desktop\1.xls"
        Kill "C:\Users\**I've been banned**\Desktop\ap.xls"
        Kill "C:\Users\**I've been banned**\Desktop\oo.xls"
    End If
End With

End Sub




I edited the macro but it is not perfect
I am not geting any error
But the macro is incorrect
I have to kill all the files in the last(No matter condition met or Not but in the last this killing process should happen Sir)
So plz help me in the same
Last edited by zyxw1234 on 06 Aug 2020, 09:41, edited 1 time in total.

User avatar
Leif
Administrator
Posts: 7209
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Macro Correction(adding a line to kill the files)

Post by Leif »

zyxw1234 wrote:
06 Aug 2020, 08:47
Kill "C:UsersWolfieeeStyleDesktop1.xls"
Kill "C:UsersWolfieeeStyleDesktopap.xls"
Kill "C:UsersWolfieeeStyleDesktopoo.xls"
Are you using the pathnames as you show above, or adding in backslashes (e.g. C:\Users\**I've been banned**\Desktop1.xls)?
Leif

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

No i have putted the backslash but whent post it then that backslash disappeared

User avatar
Leif
Administrator
Posts: 7209
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Macro Correction(adding a line to kill the files)

Post by Leif »

zyxw1234 wrote:
06 Aug 2020, 09:29
No i have putted the backslash but whent post it then that backslash disappeared
We have been down this path before, and you demonstrated that you could enter a backslash.

Are you copying this code from another forum?
Leif

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

no i am using vpn thats y

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Correction(adding a line to kill the files)

Post by HansV »

Please copy and paste the macro correctly. As it is, it doesn't make sense.
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

Done i corrected the post

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Correction(adding a line to kill the files)

Post by HansV »

I assume that the line

Code: Select all

    If b2  "" Then
should be something like

Code: Select all

    If b2 <> "" Then
Remarks:

1) You can remove the lines

Code: Select all

    With CreateObject("scripting.filesystemobject")
and

Code: Select all

    End With
They aren't used for anything.

2) Since you delete the same files in the If and Else parts, you don't need to do that in the If ... End If block.

3) You don't use the variables Wb1 and Wb2.

So the macro can be shortened to

Code: Select all

Sub STEP5testing()
    Dim ws As Worksheet, b2

    Set ws = Workbooks.Open("C:\Users\**I've been banned**\Desktop\1.xls").Worksheets(1)
    b2 = ws.[b2]
    ws.Parent.Close False

    If b2 <> "" Then
        Workbooks.Open "C:\Users\**I've been banned**\Desktop\**I've been banned**\9.15\STEP1.xlsb"
    Else
        Workbooks.Open "C:\Users\**I've been banned**\Desktop\**I've been banned**\9.15\STEP3.xlsb"
    End If

    Kill "C:\UsersWolfieeeStyle\Desktop\1.xls"
    Kill "C:\Users\**I've been banned**\Desktop\ap.xls"
    Kill "C:\Users\**I've been banned**\Desktop\oo.xls"
End Sub
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

One more thing HansV Sir
i will place this macro to macro.xlsm & i want that macro.xlsm file also should be closed after process completed

User avatar
HansV
Administrator
Posts: 78438
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro Correction(adding a line to kill the files)

Post by HansV »

You can add the following line above End Sub:

Code: Select all

    ThisWorkbook.Close
Best wishes,
Hans

zyxw1234
Banned
Posts: 253
Joined: 22 Apr 2020, 17:24

Re: Macro Correction(adding a line to kill the files)

Post by zyxw1234 »

Thnx Alot HansV Sir for Helping me in solving this problem Sir
Have a Awesome Day Sir
Problem Solved