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
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/
I am a VBA novice and am trying to "automate" a repetitive task within an excel spreadsheet. Am trying to indent a column based upon outline levels in a different column. I've been attempting to adapt code from a similar task but have run into a wall. Basically what I'm trying to accomplish is have the macro read down the OutLvl column and indent the next column based upon the outline level. Below is a screen shot of this macro and highlighted in yellow is where it got hung up. Any opinions or feedback is appreciated as I am trying to learn the application. Thanks!
Indent Macro Attempt
It looks here like you are not properly initializing the range that you intend to iterate over. The Range object is tricky, in that it can actually refer to a group of cells itself, and the array usage here is unnecessary and even confusing. Try something like this:
Dim OutLvl as Range ' This will refer to the whole range of interest
Dim t as Range ' Always good practice to declare all variables
... ' Fill in the code as you have written
Set OutLvl = Range("A1:A20") ' Now OutLvl refers to A1:A20 on the active sheet.
'Note that I can't possibly know how to appropriately set this range.
For Each t in OutLvl
... ' Now perform the loop as you have written
Next t
More on the error you have received. Note I have not watched this, so I can't speak for its accuracy:
https://www.youtube.com/watch?v=kWT2YfSHpfM
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.
I've been trying to use vlookup in VBA and it's being quite annoying.
ActiveCell.Value = Application.WorksheetFunction.VLookup(Cells(currentrow, 12), currwor.Worksheets("Sheet1").Range("R4C3:R160C10"), 6, False)
is the code
The main elements I've used are
currwor which has been set to reference another workbook and I'm looking to use vlookup on the range in there.
However it keeps kicking up an error saying subscript out of range.
I suspect currwor is the guilty party in this
Set currwor = ActiveWorkbook (then the workbook in which the above is executed is opened, and that is assigned a workbook name as well.)
I can't use a specific range because I'm trying to perform the vlookup on each cell in a range, and that range changes everytime, and the currwor is to be executed on a particular document I get but the name may vary (format however remains the same.)
Any suggestions on it?
edit: One error is that it's not supposed to be sheet1 but 'Stock Report by Product'. However it's still kicking up the subscript out of range error.
by using Set currwor = ActiveWorkbook
you assign currently active workbook to currwor however you mentioned prior to that it should be a different workbook? meaning you should have a line Set currworb = Workbooks("desired workbook name")?
Yes - Idiotic mistake. As pointed out the sheet name was incorrect and the problem was that in execution I skipped the bit where the worksheet was assigned to currwor.
Idiotic enough that I should consider deleting this post off here.
First off I want to apologize if my question has already been answered elsewhere as I did spend some time searching.
I'm using two macros in a sheet (very simple macros... they filter a table) and this sheet is a template that will be used every week. When I copy the sheet and make a new one, the macros no longer work. The error I receive is Runtime Error 9; Subscript out of range. I looked at the VBA code (which I've never really learned VBA) and see it is referencing a table title. Is there a way to fix this so it doesn't reference that specific table but rather the cells contained in that table? Example of the title name:
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
I want to access the data from a cell range of A103:A113. I tried this:
ActiveSheet.ListObjects.Range("$A$103:$A$113").AutoFilter Field:=1
This didn't work either but the error I received was different. "Run-time error '438': Object doesn't support this property or method"
What I know would work is if there was a way to have the same table name across all sheets but my limited research has seemed to point me in the direction of this not being possible.
It's impossible to reference ActiveSheet.ListObjects.Range("$A$103:$A$113") like this - either as above in your first sample, OR convert table to normal range and then use ActiveSheet.Range("$A$103:$A$113").
The problem you're facing is that you access the Range through a Table/ListObject - but the ListObject changes the name during the copy, as each Table must have a unique name. The solution is simple - instead of accessing the ListObject with its name, simply use the index in the worksheet - that will not change. Therefore, replace
ActiveSheet.ListObjects("Table1619").Range.AutoFilter Field:=1
with
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
(assuming it is the only/first table in the worksheet.)