Error when saving new workbook name - vba

I have been trying for most of the day to get this to work, but I can't figure out what is stopping the save process in this code:
Sheets("DailyReview").Select
Sheets("DailyReview").Copy
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
File_Name = "\\Il-svr\company\department\team\Schedules\Daily_Review_Email\city\" & Range("A110").Value & "\" & Range("D110").Value & "\DailyReview_" & Range("C110").Value & ".xlsx"
ActiveWorkbook.SaveAs Filename:=File_Name
The last line of code gives the error: "Run-time error '1004': Method 'SaveAs' of object '_Workbook' failed. But a file actually does get renamed, just not "saved" to the path location. The path location is completely 100% correct and works fine when I save the macro workbook using the same path location. I am also using Excel 2010 and trying to save it in 2010 format. I have also tried setting the FileFormat to xlWorkbookDefault (51) with no success. I do have permission to save to this path and works fine if I save the new book manually. The ranges are just date values. For instance, the file would save as DailyReview_122914.xlsx if I used this code for today's date. I hope this is enough information. I appreciate any help.

Thank you #RubberDuck. The values were blank for cell A110 which I think was throwing it off. I rewrote the code and discarded the value of cell A110 and it works fine now. It really is the simplest things some time. Thank you!

Related

XLS - Copy & Paste in VBA - PasteSpecial Method Fails

I'm struggling with a nagging issue. I am trying to simply copy and paste a collection of cell forumlas in an XLS worksheet using VBA. The worksheet (wks1) is created and populated from an AccessDB and is working fine otherwise.
Error: "PasteSpecial Method of Range Class Failed"
wks1.Range("P5:S5").Copy
wks1.Range("P5:S10").PasteSpecial _
Paste:=xlPasteFormulas, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
I've attempted a number of variations, but keep bumping into this err msg.
Any suggestions to get this working?
Do this instead:
wks1.Range("P5:S5").Autofill wks1.Range("P5:S10")
or
wks1.Range("P5:S10").formula = wks1.Range("P5:S5").Formula
For the paste special, it has been my experience that less is more:
wks1.Range("P5:S5").Copy
wks1.Range("P5:S10").PasteSpecial xlPasteFormulas
But when only values or formulas are wanted why include the clipboard? It is faster and cleaner to just assign them directly. So I would use the copy/paste when more than the values or formulas are wanted.

Vlookup range with a defined variable

I'm consolidating data from several excel files, I have 22 files to consolidate; however, I need to specifically look up for a cell value.
In my VBA code I declared a variable called file=int(1) and I'm doing a Loop to open and consolidate each excel file to a consolidated file. All of my excel files are numbered from 1 to 22.
What I want to to is Vlookup, but the range of the Vlookup I want it to have the stored value in file while doing the loop.
For example
file = Int(1)
Do Until file = 22
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[file.xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[file.xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
ActiveCell.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Select
Selection.Offset(1, 0).Select
file = file + 1
Can you please help me?
Thanks!
Nice tidy little loop! OK, I think I have an idea what you're trying to do here. You want to have the [file.xls] in your formula update dynamically for each iteration of the loop. Since you're using the formula property, which takes a string, you can concatenate your stored file value right into it.
This should do the trick.
ActiveCell.Formula = "=IFERROR(VLOOKUP($I$6,'[" & file & ".xls]Sheet1'!$B$4:$V$27,MATCH($F$26,'[" & file & ".xls]Sheet1'!$B$3:$V$27,0),FALSE),0)"
Hope I understood properly, if not, we'll go another round!

Error using ExportAsFixedFormat command

I am trying to debug some code containing an ExportAsFixedFormat that is causing an "Automation error the object invoked has disconnected from its clients." error message. I am running in 2013 so don't need the PDF/XPS add-in and the function works elsewhere in the workbook. It seems to have something to do with the page I am trying to make the PDF out of. While the main macro is longer, I am having the same error occur when I use this simple piece of code:
Sub NewAssetScreen()
Sheets("New Asset").Select
Sheets("New Asset").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
Sheets("New Asset").Select
Range("A1").Select
End Sub
However if I run the macro with a different worksheet name it works fine. The selection part seems to be ok - ie will work with that worksheet name but it is just the ExportAsFixedFormat command that won't work on the page.
Any insights as to why this might be the case?
Thanks in advance.
Nic
Check page breaks and other things specific to that worksheet.
Try using Sheets("New Asset").ExportAsFixedFormat instead of ActiveSheet.
Lastly try copying and pasting the contents of the New Asset sheet to a new sheet and delete the old one.
Make sure there are no "/" or other non file name characters in any of the worksheet fields you are using. My issue occured when printing 100 worksheets, 2 would fail. After much frustration I found the "/" in a heading I was using for the name the file :)

VBA - Copy Values Only

Following on from my previous question, which was answered perfectly I have now written code for the remaining part of my problem, however I have now developed problems.
Part of the worksheet uses =RAND() to generate a random number. As this is a volatile function I needed to copy the output of the formula to a new location. If I was doing this manually I would do a copy -> paste special values so that I just go the numbers, and not the formula.
When trying this in VBA I get an Error 1004 during part of the code when I try to select the destination range for the paste special.
Here is the code:
' Copy Random Questions to Static Page for VLOOKUPS
With Worksheets("Quiz Generator")
Range("NEWQUEST").Copy
'Selection.Copy
End With
With Worksheets("Static Question List")
Range("TOPSTAT").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
I've tried various ways of doing this, using
RANGE().Select
Selection.Copy
RANGE().Select
Selection.Pastespecial Paste:=xlValues
Also using:
RANGE().Copy
Range().PasteSpecial Paste:=xlValues
In the original code I can get through to the
Range("TOPSTAT).Select
Before it throws the
Run-Time error '1004':
Application-defined or object-defined error
Pop-up
Any help would be gratefully received.
All of the defined ranges are correct and in Name Manager, and I've tried with cell ref's to see if it was the range name that was the issue.
Annoyingly this worked previously using the long-hand Select / Selection.Paste etc method, but since trying to tidy the code it stopped.
Thanks in advance.
I would do something like this, to
dump the range from NEWCREST as values into an array
dump the array to the first cell in TOPSTAT and resize as needed
(you don't need sheet names when working with range names unless you have local range names)
Code
Dim x
x = Range("NEWQUEST").Value2
Range("TOPSTAT").Cells(1).Resize(UBound(x, 1), UBound(x, 2)) = x

Copy and paste from one workbook to another

I have code in one workbook, this should open another workbook, copy and paste into the workbook with the code. I can select the data but can't paste it.
I have tried many different variations of code getting errors or it doesn't do anything. An example is run in template.xls, which is where I want to paste the data:
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet.Sheets("Data")
.range("A1:H3").Select.copy
selection.copy
End With
I don't know how to use the selection since this will copy from the template, I tried using a full stop before selection.
I can copy the entire sheet from dlsheet into a new workbook, if someone could tell me how to copy it to the template and not a new workbook then this would also do the trick.
dlsheet.Sheets("Data").Copy
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
dlsheet.Sheets("Data").range("A1:H3").copy
ThisWorkbook.ActiveSheet.Paste Destination:=ThisWorkbook.ActiveSheet.Range( "A1:H3")
Try this
Set dlsheet = appexcel.Workbooks.Open(strPath & "downloadedData.xls")
With dlsheet
.Sheets("Data").Range("A1:H3").Copy
.Sheets("Data").Range("A1").PasteSpecial Paste:=xlPasteValues, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End With