I have some very simple code for setting cell A2 to the path/filename in an auto open macro.
Sub Auto_Open()
Dim apath, aname, aref
apath = Workbooks("Workbook.xlsm").Path
aname = Workbooks("Workbook.xlsm").Name
aref = apath & "\" & aname
ActiveWorkbook.Worksheets("Sheet1").Cells(2, 1) = aref
End Sub
The last line fails on my machine, but works on a co-workers machine. The problem is with the Cells object. When I add a watch on the Cells object, it shows the "Application-Defined or object-defined error". It's as if the Cells object doesn't exist. What baffles me is that it works on my co-workers machine.
The workbook does have a couple of other protected worksheets, but Sheet1 is not protected. I get the same issue when I un-protect the other sheets.
I've tried changing the Cells reference to a Range("A2") instead, same behaviour. However, if I set it to point to a different sheet, it works fine!
What is it about this sheet that's stopping me from accessing the Cells/Range property?
Related
I know I've seen references to this issue before, but I have tried several of the suggestions and I am still getting the error. I have a workbook that assembles data from another book and generates a report. I then want to make a new workbook, copy the report information into the new book, save the new book and close it, and then move on to the next report. It should do this around 10 times. In the part of my code where I am copying and pasting the sheets, I am getting an error
Error -2147417848 Automation error The object invoked has
disconnected from its clients
I have checked other postings about this error, and tried the suggested solutions without any results. the interesting thing is that sometimes it will make it through 5 cycles of code before breaking, sometimes only 2. The only consistency is that it always breaks in the same place
fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")
I have option Explicit at the top of the module, and I have checked to make sure that there are not any globals inside of the sub it is breaking in. That being said, It's entirely possible I have overlooked something. I also put a "timer" in at one point to make sure that the excel sheets were not walking over each other.
I could really use the help!
Here is my sub's code:
Sub CreateAndSave(ByRef Reg As Integer, ByVal j As Integer)
Dim fromBook As Workbook
Dim fromSheet As Worksheet
Dim newBook As Workbook
Dim fileExists As Boolean
Dim i As Integer
Dim Holder As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set fromBook = Application.Workbooks("Region_Audit_Report")
Set newBook = Workbooks.Add
With newBook
.SaveAs Filename:="G:\DataTeam\ExcelDev\Audit Report\Region Workbooks\Region" & Reg & " " & Month(Date) & "-" & Day(Date) & "-" & Year(Date) & ".xlsx" _
, FileFormat:=xlOpenXMLWorkbook
End With
Set newBook = Application.Workbooks("Region" & Reg & " " & Month(Date) & "-" & Day(Date) & "-" & Year(Date) & ".xlsx")
fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")
fromBook.Sheets("MonthData").Copy After:=newBook.Sheets("Report")
newBook.Sheets("MonthData").Range("A1") = "Month"
newBook.Sheets("MonthData").Range("B1") = "Store#"
newBook.Sheets("MonthData").Range("C1") = "District"
newBook.Sheets("MonthData").Range("D1") = "Region"
newBook.Sheets("MonthData").Range("E1") = "Due Date"
newBook.Sheets("MonthData").Range("F1") = "Comp Date"
newBook.Sheets("MonthData").Range("G1") = "# of Errors"
newBook.Sheets("MonthData").Range("H1") = "Late?"
newBook.Sheets("MonthData").Range("I1") = "Complete?"
newBook.Sheets("MonthData").Range("A1:I1").Interior.ColorIndex = 43
newBook.Save
newBook.Close
Application.DisplayAlerts = True
End Sub
I have had this problem on multiple projects converting Excel 2000 to 2010. Here is what I found which seems to be working. I made two changes, but not sure which caused the success:
1) I changed how I closed and saved the file (from close & save = true to save as the same file name and close the file:
...
Dim oFile As Object ' File being processed
...
[Where the error happens - where aArray(i) is just the name of an Excel.xlsb file]
Set oFile = GetObject(aArray(i))
...
'oFile.Close SaveChanges:=True - OLD CODE WHICH ERROR'D
'New Code
oFile.SaveAs Filename:=oFile.Name
oFile.Close SaveChanges:=False
2) I went back and looked for all of the .range in the code and made sure it was the full construct..
Application.Workbooks("workbook name").Worksheets("worksheet name").Range("G19").Value
or (not 100% sure if this is correct syntax, but this is the 'effort' i made)
ActiveSheet.Range("A1").Select
I have just met this problem today: I migrated my Excel project from Office 2007 to 2010. At a certain point, when my macro tried to Insert a new line (e.g. Range("5:5").Insert ), the same error message came. It happens only when previously another sheet has been edited (my macro switches to another sheet).
Thanks to Google, and your discussion, I found the following solution (based on the answer given by "red" at answered Jul 30 '13 at 0:27): after switching to the sheet a Cell has to be edited before inserting a new row. I have added the following code:
'=== Excel bugfix workaround - 2014.08.17
Range("B1").Activate
vCellValue = Range("B1").Value
Range("B1").ClearContents
Range("B1").Value = vCellValue
"B1" can be replaced by any cell on the sheet.
You must have used the object, released it ("disconnect"), and used it again. Release object only after you're finished with it, or when calling Form_Closing.
I had this same problem in a large Excel 2000 spreadsheet with hundreds of lines of code. My solution was to make the Worksheet active at the beginning of the Class. I.E. ThisWorkbook.Worksheets("WorkSheetName").Activate
This was finally discovered when I noticed that if "WorkSheetName" was active when starting the operation (the code) the error didn't occur. Drove me crazy for quite awhile.
Couple of things to try...
Comment out the second "Set NewBook" line of code...
You already have an object reference to the workbook.
Do your SaveAs after copying the sheets.
The error in the below line of code (as mentioned by the requestor-William) is due to the following reason:
fromBook.Sheets("Report").Copy Before:=newBook.Sheets("Sheet1")
The destination sheet you are trying to copy to is closed. (Here newbook.Sheets("Sheet1")).
Add the below statement just before copying to destination.
Application.Workbooks.Open ("YOUR SHEET NAME")
This will solve the problem!!
Trying to hammer out bugs in my code. Currently trying to do some very simple, open worksheet, copy and paste data over. Trying to do it all without using .Select or .Activate. Hitting "Application-defined or Object defined error", which, from reading the other threads on the matter, probably means that my statements aren't fully qualified. However, I can't figure out how they're not fully qualified - other posts on the topic seem to be missing a "." somewhere in the code, but my attempts to fix it haven't gotten anywhere. Heavily truncated code as follows (If you don't see it dimmed/defined, it's elsewhere)
Sub CopyPaste()
Dim CitiReportEUR As Workbook
Dim CitiReportPathEUR As String
CitiReportPathEUR = Range("CitiReportPathEUR")
Workbooks.Open Filename:=CitiReportPathEUR
Set CitiReportEUR = ActiveWorkbook
LastRowCiti = CitiReportEUR.Sheets(1).Range("I" & Rows.Count).End(xlUp).Row
Set RngCitiEUR = CitiReportEUR.Sheets(1).Range("A1:CT" & LastRowCiti).SpecialCells(xlCellTypeVisible)
Set CabReport.Sheets("CITI").Range("C1").Resize(RngCitiEUR.Rows.Count).Value = RngCitiEUR.Value
End Sub
Currently the error is occurring when I define the range. I've had problems historically with pasting into the range as well... but that's an issue for when I can actually get the code to run that far!
Rows.Count is implicitly working with the ActiveSheet.
The use of Set when assigning values to the Value property of a Range is inappropriate. Set should only be used when assigning a reference to an object.
The Resize probably needs to cater for the number of columns in the source as well as rows.
This code is more explicit:
Sub CopyPaste()
Dim CitiReportEUR As Workbook
Dim CitiReportPathEUR As String
CitiReportPathEUR = Range("CitiReportPathEUR")
Set CitiReportEUR = Workbooks.Open(Filename:=CitiReportPathEUR)
With CitiReportEUR.Sheets(1)
LastRowCiti = .Range("I" & .Rows.Count).End(xlUp).Row
Set RngCitiEUR = .Range("A1:CT" & LastRowCiti).SpecialCells(xlCellTypeVisible)
End With
CabReport.Sheets("CITI").Range("C1").Resize(RngCitiEUR.Rows.Count, RngCitiEUR.Columns.Count).Value = RngCitiEUR.Value
End Sub
So I am trying to fix some old code in an Excel workbook that my company provided. The goal is to print out a certain section of every worksheet (Using a print-all "button"), but for some reason I keep getting an error that states that I cannot unprotect the workbook.
One weird quirk is that this only occurs when there is more than one page to print. With one page it works just fine.
Worksheets(ws.name).Select (False)
With Worksheets(ws.name).PageSetup
.PrintTitleRows = ""
.PrintTitleColumns = ""
End With
Worksheets(ws.name).Unprotect
'ActiveSheet.Unprotect
Worksheets(ws.name).PageSetup.PrintArea = "$A$1:l" & ActiveSheet.Range("B10").CurrentRegion.Rows.Count + 10
Edit: When I add a blank password to the Unprotect line, it will go to the next line
Worksheets(ws.name).PageSetup.PrintArea = "$A$1:l" & ActiveSheet.Range("B10").CurrentRegion.Rows.Count + 10
where I get an error saying that I cannot use this command on a protected sheet
I have a Personal Workmap with a template ( with vba code and a button action ) that I copy into a worksheet when needed. It creates a Table of sheet to quickly access many, > 25 sheets, like a fake popup ( excel 2016 macos ). It did works well when I did program it directly into the sheet module. It collects the sheets and creates buttons with the following code.
Set btnRng = TOC_WS.Range(Cells(lastRow, btnCol), Cells(lastRow, btnCol))
Set btn = TOC_WS.Buttons.Add(btnRng.Left, btnRng.Top, btnRng.Width, btnRng.Height)
With btn
.OnAction = "btnAction"
.Caption = WS.Name
.Name = WS.Name
End With
And the button sub....
Sub btnAction()
......
End Sub
But now for some reason it doesn't work when the called sub is in the Sheet-module. I get the notification that it can't be found. If I put it into a code Module the it works. I did of course search te web, but couldn't find anything that say's it can't working.
My question - How can I get the button action back working into the same sheet-module as the rest of the code?
In General, when a routine is a member of a Worksheet, the worksheet's name is implicitly part of the routine's name.
btn.OnAction = "Sheet1.btnAction"
Or, as in your code you want to bind it to a routine in the TOC_WS sheet:
btn.OnAction = TOC_WS.CodeName & ".btnAction"
Moreover, if you want to move the routine to the code module ThisWorkbook,
btn.OnAction = "ThisWorkbook.btnAction"
I have a problem while migrating my VBA code from Office2003 to Office2010. I would like to copy the text of a cell (Excel) to Powerpoint. Office2003 generated a new textbox and the style of the text was the same as in Excel. Now my code fails with Office2010 and I get the following message:
runtime error -2147188160 (80048240)
Shapes.PasteSpecial : Invalid request. Clipboard is empty or contains data which may not be pasted here.
The clipboard is definitly not empty.
The code is the following:
Set mySlides = obj_pp.ActivePresentation.Slides
mySlides(Slidenum).Shapes.PasteSpecial DataType:=ppPasteRTF
I have already tried other DataTypes and the Paste-function. Nothing helped. The text, I copy from Excel, is also formatted as text in Excel. Nothing special. The slide is added as an empty one. After adding the slide a picture is pasted (DataType:=ppPasteEnhancedMetafile). And after that the text should be pasted.
Could someone please help me to get this code work? Thanks in advance. Please let me know if more code is needed.
Edits:
Binding of the ppt:
Dim Datei As String
Pfad_Server = "..."
Pfad_Verzeichnis = "..."
Dateiname = "....pptx"
Datei = Pfad_Server & Pfad_Verzeichnis & "\" & Dateiname
Set obj_pp = (GetObject(, "Powerpoint.Application"))
obj_pp.Visible = True
IsOpen = False
Before running the macro I always open the ppt. This works fine.
Adding slide and pasting range as picture (works fine):
Range(Cells(start_var, 1), Cells(bereich_ende, 13)).Select
Selection.CopyPicture xlScreen, xlPicture
...
Set mySlides = obj_pp.ActivePresentation.Slides
mySlides.Add Index:=mySlides.Count + 1, Layout:=12 'ppLayoutBlank
mySlides(Slidenum).Shapes.PasteSpecial DataType:=ppPasteEnhancedMetafile
In my opinion you need to change method which copies your range. Use this lines instead your .CopyPicture line:
Selection.Copy
and it will work with pasting method:
mySlides(mySlides.Count).Shapes.PasteSpecial DataType:=9
where 9 = ppPasteRTF.