Unable to SAVE AS a wb

Vaghela

New Member
Joined
Oct 28, 2017
Messages
16
In the following code, I am getting error when saving the file...... Please help



Sub Del_Column()


Dim wb As Workbook
Dim wbName As String




wbName = "Book"


For Each wb In Application.Workbooks


If wb.Name Like wbName & "*" Then
Debug.Print wb.Name

wb.Sheets("Sheet1").Name = "D Track"
wb.Sheets("Sheet2").Name = "Comp Yes"


With wb.Sheets("D Track").Range("B:J,L:L,N:T,V:V,X:X,Z:AM,AO:AV").EntireColumn.Delete


wb.Sheets("Comp Yes").Range("B:I,K:K,M:N,P:S,U:U,W:W,Y:AK,AM:AP").EntireColumn.Delete


End With
End If

ActiveWorkbook.SaveAs Filename:="\\Desktop\\Daily Sheet" & Format(Now(), "DDMMMYY") & ".xlsx"


Next wb


End Sub
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

ranman256

Well-known Member
Joined
Jun 17, 2014
Messages
1,957
use a drive letter:
C:\Users" & Environ("USERPROFILE") & "\Desktop\Daily Sheet" & Format(Now(), "DDMMMYY") & ".xlsx"
 
Last edited:

Vaghela

New Member
Joined
Oct 28, 2017
Messages
16
use a drive letter:
C:\Users" & Environ("USERPROFILE") & "\Desktop\Daily Sheet" & Format(Now(), "DDMMMYY") & ".xlsx"



Its not the drive letter as I am successfully running the code for other macro

Windows("A_report.xls").Activate
Sheets("Sheet1").Select
Range("A1").Select
ActiveWorkbook.SaveAs Filename:= _
"\\Desktop\Report " & Format(Now(), "DDMMYY") & ".xls"


The only difference in both the codes is on queried code I am using a wb with name starting Book "*" and for the above it is a specific named wb (A_report). Is this an issue? if yes how to resolve it
 

Eric Golf

Active Member
Joined
Dec 19, 2007
Messages
324
Hi Vaghela,

What is the exact error you are getting? Without that everything is a guess.

I do note that your save statement is outside the If statement but inside the loop, so will attempt to save all workbooks processed as the same name?

Edit - It is also saving the ActiveWorkbook which may or may not equal wb.

Hope this helps,

Eric Golf.
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,363
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Try
Code:
Sub Del_Column()

Dim wb As Workbook
Dim wbName As String

wbName = "Book"

For Each wb In Application.Workbooks
    If wb.Name Like wbName & "*" Then
        Debug.Print wb.Name
        
        wb.Sheets("Sheet1").Name = "D Track"
        wb.Sheets("Sheet2").Name = "Comp Yes"
        
        wb.Sheets("D Track").Range("B:J,L:L,N:T,V:V,X:X,Z:AM,AO:AV").EntireColumn.Delete
        wb.Sheets("Comp Yes").Range("B:I,K:K,M:N,P:S,U:U,W:W,Y:AK,AM:AP").EntireColumn.Delete
    
        wb.SaveAs FileName:="\\Desktop\\Daily Sheet" & Format(Date, "DDMMMYY") & ".xlsx"

    End If
    


Next wb


End Sub
There was no need for the with statement (which was wrong anyway) & I've moved the Saveas inside the the If statement.
 

Vaghela

New Member
Joined
Oct 28, 2017
Messages
16
Try
Code:
Sub Del_Column()

Dim wb As Workbook
Dim wbName As String

wbName = "Book"

For Each wb In Application.Workbooks
    If wb.Name Like wbName & "*" Then
        Debug.Print wb.Name
        
        wb.Sheets("Sheet1").Name = "D Track"
        wb.Sheets("Sheet2").Name = "Comp Yes"
        
        wb.Sheets("D Track").Range("B:J,L:L,N:T,V:V,X:X,Z:AM,AO:AV").EntireColumn.Delete
        wb.Sheets("Comp Yes").Range("B:I,K:K,M:N,P:S,U:U,W:W,Y:AK,AM:AP").EntireColumn.Delete
    
        wb.SaveAs FileName:="\\Desktop\\Daily Sheet" & Format(Date, "DDMMMYY") & ".xlsx"

    End If
    


Next wb


End Sub
There was no need for the with statement (which was wrong anyway) & I've moved the Saveas inside the the If statement.


This has worked,,, thanks for quick response
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,363
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,123,474
Messages
5,601,880
Members
414,479
Latest member
Beau the dog

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top