There seems to be something wrong with one of my Excel Objects. Take a look at the below snippet. I created the subroutine Run_all(), and when I step through, it'll go up to the Function row_count() and start executing, however, when it gets to the first highlighted row, I get the error.
**The Sheet that I'm referencing in the function is typed correctly. For example, if I run the bottom subroutine CA_Copy_Paste_, it works correctly and I get no errors.
Why is Excel not recognizing "Sheet 3" in the function? For more context, it only works if I type "Sheet4". Does not work on "Sheet1" or "Sheet2" either.
If you insist on using .Select and .Activate to accomplish your goals then you must activate the worksheet before activating or selecting a cell or range of cells on that worksheet.
worksheets("sheet 3").activate
activesheet.range("c4").activate
If you wish to use some 'shorthand' to try and accomplish this in a single line of code then switch to the Application.GoTo method.
Application.Goto reference:=worksheets("sheet 3").range("c4")
In any event, you are best off avoiding the use of select and activate. See
How to avoid using Select in Excel VBA.
Related
I have an advanced filter that is being used to sort a large data set.
The filter is dropping the filtered data into a separate sheet.
I have a VBA Macro that allows me to highlight the portions of the filter that I want to use and paste it into a range adjacent to the filter table.
Currently I am using very simple VBA.
The copy of the active selection and paste into the next open row after a specified Cell. The cell is a row of headers that corresponds to the headers of the table that the copy selection is being made from.
Sub CopyPaste()
Selection.Copy
ActiveSheet.Range("J6").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
End Sub
The clear is very simple.
Sub ClearTable()
ActiveSheet.Range("J7:O100").Clear
End Sub
After the clear is run, I receive an error.
Get Run Time error '1004, Application-defined or object defined error.
EDIT: Clarification. .clear and .clearcontents both lead to the errored state If I attempt to paste after I clear the range.
With J7:O100 cleared, select J6 and tap [ctrl]+[down arrow]. Try and go one more row further down. That is where you are trying to paste and if you follow my directions the problem should be perfectly obvious.
Use,
Selection.Copy Destination:=ActiveSheet.Cells(Rows.Count, "J").End(xlUp).Offset(1, 0)
This looks from the bottom up and selects 1 row down not from the top down (which seems to be J1048576 and cannot be moved down a row let alone having room for the paste).
I've got a recorded macro that I tried to simplify by getting a number of activate and select statements onto a single line, but that results in a runtime error.
This is not a critical problem, but I'm just curious to understand what is going on. This is my initial code snippet (it is preceded by a Copy snippet in the procedure):
ThisWorkbook.Activate
Sheets("MS Act Report").Select
Range("G1").Select
ActiveSheet.Paste
this is my simplified code:
ThisWorkbook.Activate
Sheets("MS Act Report").Range("G1").Select
ActiveSheet.Paste
When running this I get a
runtime error '1004': Select method of Range class failed
You can only select ranges on the active sheet. If "MS Act Report" is not the active sheet, you can't issue a .select command against its cells. To simplify the code, instead of copy-pasting, just make the ranges equal.
Thisworkbook.WorkSheets("MS Act Report").Range("G1:I5").Value= _
ActiveWorkbook.Worksheets("Whatever").Range("a1:c5").Value
Some recommended reading: How to avoid using select
I have a command button which does so many calculation for me. To make a user interface I need to move the button from the "calculation" sheet to the "user interface" sheet. After moving the button I need to address the inputs and outputs to "calculation" sheet.
I used the with statement as follows:
With Sheets("Calculation")
.Range("C3", Range("C3").End(xlDown)).Clear
.Range("X3", Range("X3").End(xlDown)).Clear
.Range("Y3", Range("Y3").End(xlDown)).Clear
.Range("z3", Range("z3").End(xlDown)).Clear
.Range("AC3", Range("AC3").End(xlDown)).Clear
.Range("B3", Range("B3").End(xlDown)).Clear
.Range("E4", Range("E4").End(xlDown)).Clear
.Range("A3", Range("A3").End(xlDown)).Clear
.Range("K3", Range("K3").End(xlDown)).Clear
.Range("D3", Range("D3").End(xlDown)).Clear
End With
And I had a run time error '1004' ( which says: Applicaiton -defined or object-defined error.)
I was wondering how I can call the "calculation" sheet when I have the button in "user interface" sheet.
The reason you're getting the error is because of a typo in the second of your Range statements. Without the . you're referring to the current worksheet rather than the one in your With statement, and the runtime error is because that range is impossible to define across 2 sheets.
With Sheets("Calculation")
.Range("C3", .Range("C3").End(xlDown)).Clear
.Range("X3", .Range("X3").End(xlDown)).Clear
.Range("Y3", .Range("Y3").End(xlDown)).Clear
.Range("z3", .Range("z3").End(xlDown)).Clear
.Range("AC3", .Range("AC3").End(xlDown)).Clear
.Range("B3", .Range("B3").End(xlDown)).Clear
.Range("E4", .Range("E4").End(xlDown)).Clear
.Range("A3", .Range("A3").End(xlDown)).Clear
.Range("K3", .Range("K3").End(xlDown)).Clear
.Range("D3", .Range("D3").End(xlDown)).Clear
End With
I am building a small vba script that is merging tables from several workbook into one single worksheet of another workbook. The error is raised when I try to set the destination range's value:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
The error: "Run-time error '1004': Application-Defined or object-defined error"
I went through similar questions, and the general answer is what I found in this one: The selected cell belongs to another worksheet than the one desired.
While this makes complete sense, I still can't figure why my code breaks as I'm only using numerical reference (CurrentRow is a Long) and Resize, which should prevent me from doing such a mistake.
Additionally, I ran a couple quick tests in the Immediate window and it turns out that while the worksheet wksPivotData exists and I can access its name and a cell value, the range function simply doesn't work:
Debug.Print wksPivotData.Name
PivotData
Debug.Print wksPivotData.Cells(1, 1).Value
123
Both of those work but the next one doesn't:
Debug.Print wksPivotData.Range(1, 1).Value
Your last line, Debug.Print wksPivotData.Range(1, 1).Value won't print because you're misuing Range(). I assume you want A1?
When using Range(1,1), you're referring to a non-existent range. If you want to do cell A1, you need
With wksPivotData
myData = .Range(.Cells(1,1),.Cells(1,1)).Value
End with
Since you're using multiple worksheets, I'd use the with statement as above. Another way to write the same thing is wksPivotData.Range(wksPivotData.Cells(1,1),wksPivotData.Cells(1,1)) (You need to explicitly tell Excel what sheet you want to refer to when using Range() and cells().
Finally, for your resize, if I recall correctly, you're going to have to add the same Cell() twice in your range:
wksPivotData.Range(wksPivotData.Cells(CurrentRow, 1),ksPivotData.Cells(CurrentRow, 1)).Resize(tbl.ListRows.Count, tbl.ListColumns.Count).Value = _
tbl.Range.Value
Or, for the same thing, but different way of doing it:
With wksPivotData
.Range(.Cells(currentRow, 1), .Cells(currentRow, 1)).Resize(tbl.ListedRows.Count, tbl.ListColumns.Count).Value = tbl.Range.Value
End With
I have an Excel document that copies a template sheet into a new sheet on the first time it runs. Any more sheets that follow this template are appended to the newly created sheet.
I'm getting the error in the title in this section of code:
If Worksheets("User Configuration").Cells(9, 15).Value = 1 Then
Worksheets("Cable Cards Template").Range("A1:J33").Copy
With Worksheets("Cable Cards")
**.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues**
.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlFormats
End With
Worksheets("Cable Cards Template").Shapes("Picture 1").Copy
Worksheets("Cable Cards").Paste Cells(RangeStartRow, RangeStartColumn)
Call Sheets.FormatCableCardRows
End If
Basically if the If statement is true (the cell = 1), then a range on a particular sheet should be copied and pasted into the new sheet at the range given using PasteSpecial for values and formatting. Following that, the "newly created" sheet should have an image copied into the top left cell of the template and then a subroutine is called to format the rows of the new sheet.
I'm getting the error at the first .Range call after the With Worksheets("Cable Cards") statement. I've tried not using the With statement, copying values directly instead of paste-special etc. The weird thing is that this will run on the first go, when the new sheet is created via:
If (RangeStartRow = 1) Then
Worksheets.Add().Name = "Cable Cards" ' Create new sheet with given name only on first cable card
Columns(1).ColumnWidth = 9.43
Columns(6).ColumnWidth = 11
Columns(10).ColumnWidth = 9
Call FormatForA5Printing("Cable Cards", 71)
End If
but on the 2nd go, it fails entirely, with the Run Time Error 1004 'Application Defined or Object Defined Error'. I'd appreciate any help.
Your cells object is not fully qualified. You need to add a DOT before the cells object. For example
With Worksheets("Cable Cards")
.Range(.Cells(RangeStartRow, RangeStartColumn), _
.Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
Similarly, fully qualify all your Cells object.
Solution #1:
Your statement
.Range(Cells(RangeStartRow, RangeStartColumn), Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
does not refer to a proper Range to act upon. Instead,
.Range(.Cells(RangeStartRow, RangeStartColumn), .Cells(RangeEndRow, RangeEndColumn)).PasteSpecial xlValues
does (and similarly in some other cases).
Solution #2:
Activate Worksheets("Cable Cards") prior to using its cells.
Explanation:
Cells(RangeStartRow, RangeStartColumn) (e.g.) gives you a Range, that would be ok, and that is why you often see Cells used in this way. But since it is not applied to a specific object, it applies to the ActiveSheet. Thus, your code attempts using .Range(rng1, rng2), where .Range is a method of one Worksheet object and rng1 and rng2 are in a different Worksheet.
There are two checks that you can do to make this quite evident:
Activate your Worksheets("Cable Cards") prior to executing your Sub and it will start working (now you have well-formed references to Ranges). For the code you posted, adding .Activate right after With... would indeed be a solution, although you might have a similar problem somewhere else in your code when referring to a Range in another Worksheet.
With a sheet other than Worksheets("Cable Cards") active, set a breakpoint at the line throwing the error, start your Sub, and when execution breaks, write at the immediate window
Debug.Print Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
Debug.Print .Cells(RangeStartRow, RangeStartColumn).Address(external:=True)
and see the different outcomes.
Conclusion:
Using Cells or Range without a specified object (e.g., Worksheet, or Range) might be dangerous, especially when working with more than one Sheet, unless one is quite sure about what Sheet is active.
Assgining a value that starts with a "=" will kick in formula evaluation and gave in my case the above mentioned error #1004. Prepending it with a space was the ticket for me.