I updated my office 365 and I am trying to run the simple code below but Im getting an error:
Rows("5:5").Select
Selection.Insert Shift:=xlDown
Error message:
Run-time error '1004'
This selection isn't valid. Make sure the copy and paste areas don't overlap unless they are the same size and shape
Surprisingly, if I record the same code and run the code is fine.
Does anyone know how to fix it?
Related
So I'm a bit new to excel VBA, and I'm creating a macro to run on financial worksheets. I want to shift the values in the totals to the right place, as they are a column to the left of the actual data (these weren't created by a formula, they were generated by a different program and are fixed text). The shifting I managed to do just fine. The problem here is finding where the totals column is, as it varies between worksheets.
This is what I have so far.
For totalRow = 7 To 2000
With ws
If ws.Visible = True Then
If InStr(Range(totalRow, "A").Value, "Totals:") > 0 Then
Exit For
End If
End If
End With
Next totalRow
Yet for some reason, it's giving me an error when I try to run it. I know it's probably something simple I'm overlooking, because I cannot for the life of me figure out the problem. I've tried using a Do-Until loop, same issue. Is it a problem with the variables I'm using?
Several suggestions:
This is the basic problem:
Error 1004 "Application-defined or Object-defined error"
Look here for several potential issues/potential fixes:
VBA Runtime Error 1004 "Application-defined or Object-defined error" when Selecting Range
Use the VBA debugger and step through your macro a line at a time, until you find the specific object it's barfing on:
https://www.techonthenet.com/excel/macros/vba_debug2013.php
EDIT:
Having said that, I think Tim Williams's suggestion is probably spot-on:
You should always scope your Range/Cells calls with a worksheet
object, otherwise they will reference whatever happens to be the
Activesheet.
But PLEASE:
If at all possible, make the effort to learn troubleshooting tools available to you (like the debugger).
One other "useful tips" link I'd urge you to look at:
http://www.jlathamsite.com/Teach/VBA/WritingBulletProofCode.pdf
I've figured out how to insert a formula into a range of cells and managed to make it work once. Unfortunately, I can't get it to work with this formula. Instead I get an
Application-defined or object-defined error.
Here's what I'm attempting to run.
Sheets("P&L").Select
Range("A1:A250").Select
Selection.FormulaR1C1 = "=IF(ISNUMBER(LEFT(RC[+1],4)*1),LEFT(RC[+1]4,4)*1,)"
Selection.Columns.AutoFit
I suspect it has something to do with the * acting as a wildcard. I've put it in block quotes, but that just gives another error.
Any help is appreciated.
You've got an extra 4 in that formula.
Selection.FormulaR1C1 = "=IF(ISNUMBER(LEFT(RC[+1],4)*1),LEFT(RC[+1],4)*1, text(,))"
I'm trying to refresh a query on a cell change, however I can't figure out how to reference the query.
My code: Sheets("Roster Query").QueryTables(0).Refresh
Just errors out with:
Run-time error '1004':
Application-defined or object-defined error
I have a sheet named "Roster Filter" that has query table I want to refresh. How can I get that QueryTable and refresh it?
Edit: Also tried:
For Each qt In Sheets("Roster Query").QueryTables
qt.Refresh
Next
This does not error out, but the query is not refreshed.
Query tables are a relic of older versions of Excel, before tables were a thing. Not sure how to even create one in Excel 2007+.
If you added your QT via the Data/Get External Data Ribbon menu, what you added was actually a ListObject.
I tested this on Sheet1, adding a simple query - Excel created the ListObject for me:
In the immediate pane, I get these results:
?Sheet1.QueryTables.Count
0
?Sheet1.ListObjects.Count
1
And I can reproduce your exact same error:
Sheet1.QueryTables(0).Refresh 'runtime error 1004
The error is simply outrageously misleading, that's all - it should really be an index out of bounds.
The solution is to refresh the ListObject instead:
Sheet1.ListObjects(1).Refresh 'works
You can access the underlying QueryTable object via the ListObject, too:
?Sheet1.ListObjects(1).QueryTable.CommandText 'gives you the query
You're seeing an error because the .Item method is base 1, not base 0
For example, this worked for me in Excel 2016:
Sheets("Roster Query").QueryTables(1).Refresh
So if you only have one QueryTable, it would be .QueryTables(1).
I'm having a run-time error 1004 on closing the excel sheet. Through my research, most of error 1004 happen when the users are trying to copy the sheet by macro or closing the sheet by macro.
In my case, the error appears even when I close the developer tab and create a new excel file. Some resources I found online include the You may receive a "Run-time error 1004" error message when you programmatically set a large array string to a range in Excel 2003 and some other forum posts. However none of them is applicable in my case since I'm trying to close the file by the 'x' button. Please let me know if should describe the question more accurately. Thanks.
I am trying to implement a "COUNTIF()" function in my vba excel application. I know how to do this programatically but I want specifically to implement this as a formula so that later changes in the sheet will hold. This is the problematic line:
ActiveSheet.Cells(3, 20).FormulaR1C1 = "=COUNTIF(R11C7:R12C7;"">0"")"
It results in the following error:
Run-time error '1004': Application-defined or object-defined error
VBA defaults to US formatting unless otherwise specified - which you could do here using FormulaR1C1Local - so you need to use a comma separator, not a semicolon.