Something has changed in my VBA that is not allowing me to complete certain routines. I have listed a very simple example below. If anyone has experienced this I would be really appreciate any support in resolving this issue.
Simple example of issue:
When I use the following code it works fine.
Sheets("Sheet1").Select
Range("B3").Select
When I combine them I get a "1004" error
Sheets("Sheet1").Range("B3").Select
I checked the reference/document library and nothing appears to have changed in here. It has to be something simple but I just can't put my finger on it.
If you absolutely must do it in a single line of code then swap the Select for an Application.GoTo which accepts both worksheet and cell range.
application.goto range("Sheet1!B3")
However, it is almost never necessary (and most often counter-productive) to use the Range .Select method to reference a cell or cells to work on. See How to avoid using Select in Excel VBA macros for methods on getting away from relying on select and activate to accomplish your goals.
You already have your answer:
first Select the worksheet
the Select a range on that worksheet
Your code will work if you happen to be on Sheet1 when it is run, but will fail if you are not on Sheet1. In my opinion VBA is kind of dumb with regard to this issue.
Related
Please help me with the code to insert a column in excel using vba. Here is what I am doing -
Sheet1.Range("A:A").EntireColumn.insert
That code works fine for me.
Sheet1.Range("A:A").Insert works also. Range("A:A") is already referencing an EntireColumn.
There are a few things to check if it's not working for you:
You're referencing an object called Sheet1. Is that definitely the codename of the worksheet you want to change? Make sure you understand the difference between sheet name and codename. You could try referencing it by the sheet's name instead: Worksheets("Tabname").Range("A:A")...
Is the worksheet protected? That would give you an error if it is.
Is there any data in the right-most column of the spreadsheet? That would also cause an error as Excel doesn't know what to do with it. If you're not 100% sure, select the entire right-most column of the sheet and hit delete to remove anything that might cause an issue.
Lastly, can you insert a column manually? i.e. select left most column, [right-click] and [Insert]?
I think you got the Sheets property wrong, this works for me :
Sheets(1).Range("A:A").EntireColumn.Insert
You should clearly mention what you are trying to do; what type of problem you are facing.
However, you should try out the below code. hope this will help
Sheet1.Range("A:A").EntireColumn.Insert Shift:=xlToRight
while inserting a new row or column make sure to refer to the entire row or column. Check if there are any marge cells at the right of column A. That can possibly be causing the problem.
Sub TryMe()
Columns(1).Insert
End Sub
Also, this is a very generic question. If you can Google something and figure it out in a fraction of the time that it takes to craft a question and post it on SO, just Google it. If your question is very unique, and after hitting multiple dead ends using Google, then ask the SO community. I found the 3 links below using a Google search that took around 1 second.
https://www.automateexcel.com/vba/insert-row-column/
https://excelchamps.com/vba/insert-column/
https://www.educba.com/vba-insert-column/
hope you can help me with a little issue. I want to copy a range of my current workbook to another open workbook.
Doesnt sound like an issue but it already crashes within the copy line.
I already tried to save the current/active Worksheet/book in a varaible and execute it like that but the error still appears. I used the combination of range and cells a ew times already so i doubt that this is the issue.
Maybe some of you had a similar problem already?
Or possible there is a better way of doing it?
Thanks alot in advance.
ThisWorkbook.Worksheets("Test").Range(Cells(8, 2), Cells(AmountofRows, AmountofColumns)).Copy
I suspect it's due to you not being on the Active sheet when you run the command.
Try using this instead
With ThisWorkbook.Worksheets("Test")
.Range(.Cells(8, 2), .Cells(AmountofRows, AmountofColumns)).Copy
End With
The way you have used Cells will just use the active sheet which will then fail when you try to copy with a mixed declaration. Make sure you always define your ranges fully
I have a userform that is supposed to insert a row on Sheet3 and populate some cells in that row with some values. it works great as long as I have sheet3 displayed. (The form is shown modeless to give me access to the sheets).
Anyway, I happened to have another sheet active and ran it again and was surprised to see it inserted the row not in sheet3, but in the one I had displayed... (Thank God I saved first!)
In the code, I specified a range object as follows to find the insertion point: (I'll truncate the code a bit to keep it simple)
Dim RecordRange As Range
Set RecordRange = Sheet3.Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1,0).EntireRow.Insert
blah blah blah.
A workaround is to activate the sheet first:
Sheet3.Activate
That at least inserts into the correct sheet, but I'd rather not have sheet3 be displayed when I add the record, so I even surrounded that line with:
Application.ScreenUpdating = False
Sheet3.Activate
Application.ScreenUpdating = True
unfortunately, ScreenUpdating doesn't work from within a Userform Code module, so that bites...
I still don't understand why it chooses to insert the row into whatever sheet is active, when I've already specified sheet3 in the code. I have another Macro very similar that doesn't have this problem.
any ideas?
...so you figure your workaround needs a workaround? Hmm... I think that qualifies as a double-negative since a workaround is supposed to, by definition, fix problems rather than cause them...
A workaround is a bypass of a recognized problem in a system. A
workaround is typically a temporary fix that implies that a genuine
solution to the problem is needed. But workarounds are frequently as
creative as true solutions, involving outside the box thinking in
their creation.
Typically they are considered brittle in that they will not respond
well to further pressure from a system beyond the original design. In
implementing a workaround it is important to flag the change so as to
later implement a proper solution.
Placing pressure on a workaround may result in later system failures.
For example, in computer programming workarounds are often used to
address a problem or anti-pattern in a library, such as an incorrect
return value. When the library is changed, the workaround may break
the overall program functionality, effectively becoming an
anti-pattern, since it may expect the older, wrong behaviour from the
library. (Wikipedia)
Just sayin'... :-)
So instead of fixing the workaround, your original issue is (thankfully) straightforward.
You're using Sheet3 as an object, and I suspect you haven't assigned anything (like Worksheets("Sheet3") to an object called Sheet3.
Try this instead:
Dim RecordRange As Range
Set RecordRange = Worksheets("Sheet3").Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1,0).EntireRow.Insert
If you indeed intended to use Sheet3 as an object then make sure it's declared as set, so for example you could instead use:
Dim RecordRange As Range
Dim Sheet3 As Worksheet
Set Sheet3 = Worksheets("Sheet3")
Set RecordRange = Sheet3.Cells(RowVariable,ColumnVariable)
RecordRange.Offset(1, 0).EntireRow.Insert
Also, just to point out: If either of these solves your issue then the problem would've "made itself known", by including one line at the top of every module (at least during development & troubleshooting):
Option Explicit
This link has a short explanation, but basically it forces you to properly declare all variables (thus helping to prevent future workarounds!)
I'm creating an Excel file from data in an Access database.
All I'm currently trying to do is offset a selection I've made in Excel from Access, and I keep receiving errors.
ExcelWS.Range(Range("H6"), Range("H6").End(xlDown)).Offset(0, -1).Select
ExcelWS is an Excel. Worksheet object.
My goal is to select from G6 to the end of the workbook. However, not every cell in column G is populated, so a simple End(xlDown) from cell G6 only takes me part way through the file. That being said, all cells in column H are populated, so I'm able to do an End(xlDown) from cell H6. From there, I want to move my selection one to the left, giving me the desired range in column G selected.
The above code is a product of about 2hrs of googling, and trying tons of different combinations of similar code. However, I'm still receiving an error.
Run-time error '1004':
Method 'Range' of object '_Global' failed
The odd thing is that 1/10 times, I'll receive the desire results. The rest of the time I receive the above error.
I feel as though my issue has something to do with running that line of code from Access, however that's currently my only way of completing this task as I have other things that rely on being able to do it from Access.
Any help would be appreciated, and if there's any more info I can give I'll try my best.
Thanks.
To run Select the sheet shoudl be active/ in -focus.
So you ned to set foucus to sheet.
ExcelWS.Activate
ExcelWS.Range(Range("H6"), Range("H6").End(xlDown)).Offset(0, -1).Select
Simalarly, to make the seet in-focus, your workbook needs to be active/ infocus.
Try to avoid Select as much as possible. Also fully qualify the names.
Dim rngTest as Range
Set rngTest= ExcelWS.Range(ExcelWS.Range("H6"), ExcelWS.Range("H6").End(xlDown)).Offset(0, -1)
Thanks to #cyboashu, I was able to get it working.
I took the examples he gave me and mushed them together to make it do what I wanted.
I realized after posting the question that it was working every other time, consistently.
Anyway, the code I used is
Dim ExcelR1 As Range
Set ExcelR1 = ExcelWS.Range(ExcelWS.Range("H6"), ExcelWS.Range("H6").End(xlDown)).Offset(0, -1)
ExcelR1.Activate
I recently started working with a friend's excel project, making macros. I was mostly doing this through the "Record Macro" function, as I am not knowledgeable in Visual Basic to code it myself, and there is not enough time to learn the language.
All was going smoothly until I learned that ctrl-[ would not work to trace a cell's precedents inside of a macro because it just selected the cell that the first precedent was linked to, and would not change for future precedents. I looked up how to code it in Visual Basic and ended up using "Selection.Precedents.Select" to trace the cell's precedents. Because the precedent I am trying to link to is on a different sheet (still in the same workbook) it returns "Run-time error '1004': No cells were found."
It looked like this:
Range("U5").Select
Selection.Precedents.Select
Because this did not work, I went to the "Trace Precedents" button on the Formula Auditing tab, and it was able to trace back to the precedent on the other sheet (ctrl-[ also worked to select the cell). I then tested a macro using a different cell as the precedent on the same sheet (so U5 was linked to V5, instead of the cell on the other sheet), and it was able to select the precedent cell.
I did some research and found a lot of answers that just said "Use Selection.Precedents.Select", which I did, and it didn't work.
Edit:
I got it working, I had to do:
Worksheets("Sensitivity Table").Activate
Range("U5").Select
Range("U5").ShowPrecedents
ActiveCell.NavigateArrow True, 1
Probably not the most efficient way to do it, but as far as I know, it works. You just have to remove arrows at the end of the code.
The Precedents property of the range object only applies to ranges on the current worksheet. To get all the precedents, you would need to parse the formula.
See:
Charles Williams Answer
A typical approach is discussed in Colin Legg's Blog