sheet1.activate
activesheet.range(cells(2,"Q"),cells(40,"K")).select 'here the error occurs
selection.copy
The above code works for somtimes and for sometimes throws an Error that with object variable not set what does that mean ? why the above code is not working and if i reopen the file it works again and sometimes dont. But i dont know the reason why it happens. Am i making any mistake in the syntax, If so please let me know the right way to do it. And is there any more efficient way to do to select a set of range ? other than the above ? Thank you in advance
The cause of this error is usually the omission of a Set keyword, e.g.
Dim w as Worksheet
w = Sheet1 ' throws error 91
' Correct syntax:
' Set w = Sheet1
If you tell us on what line the error occurs, then a more specific answer will be forthcoming. The syntax of your code sample looks fine, but the error may be due to another line in your code, perhaps not shown in your question.
Is there any more "efficient" way to do to select a range? Personally, I don't like the method you show, with strings as the arguments for Cells. Strings are messy, and often lead to mistakes and therefore a loss of work efficiency. Why not:
Sheet1.Range(Cells(2,18),Cells(40,11)).Select
' or
Sheet1.Cells(2,11).Resize(39,8).Select
' or
Sheet1.Range("K2").Resize(39,8).Select
Or, define K2:Q40 as a named range in your sheet, calling it e.g. "myRange", and do this:
Sheet1.Range("myRange").Select ' My personal favourite
But why are you using Select? This is usually not necessary. Just write:
Sheet1.Range("myRange").Copy
But then I could ask, why are you using Copy, which is a messy method in its own right?...
Related
I have a search function in a VBA code that searches column A. Column A is filled with acronyms and the corresponding rows in Column B are the meanings for that acronym.
I have a user form setup that the user can enter an acronym and if it is in the file, it will show a message box saying what that acronym means.
I am trying to search for an exact match of the acronym the user enters, via this line:
Range("A:A").Find(acro, LookAt = xlWhole).Select
However, when I run it, even if I copy a cell containing an acronym and paste it into the user form text box, it will act as if it could not find it and follows my On Error handle.
What did I do wrong that made it unable to find the acronym string I am looking for?
Thank you!
#nwhaught already answered and pointed out the real issue, but to add something too large for a comment:
It's best not to chain Select/Activate directly to Find():
Range("A:A").Find(acro, LookAt = xlWhole).Select
...since that requires error handling when the item is not found (and as you discovered that can mask other issues with your code)
Try something like this instead:
Dim f
Set f = Range("A:A").Find(acro, LookAt := xlWhole)
If Not f Is Nothing then
'do something with f
End If
The Is Nothing test avoids the use of error handling.
Your On Error handle is masking the true cause of the error.
Code should be Range("A:A").Find(What:=acro, LookAt:= xlWhole).Select
When debugging, it's best to disable error handling, as the whole point of handling an error is to mask its effect to the user. (which makes debugging nearly impossible)
This morning I decided to open my book of stupid questions and found one question I can't get out of my head (might be that the question should be on code review, but you tell me).
So here it goes - now in VBA you normally would declare a variable and assign a value to it by what I would use as standard like this:
Dim n as Integer
n = 1
Or with objects using Set:
Dim wb as Worksheet
Set wb = ActiveSheet
But there is also an other syntax possibility here that allows you to both declare and assign a value at the same line by doing this (lets call this alternative way):
Dim n as Integer: n = 1
Dim wb as Worksheet: Set wb = ActiveSheet
Now we have two ways to declare and assign a variable. What my book of stupid doesn't tell is is there some reason or case where the alternative way will not work or why it's almost never used? To my head if variable is at the beginning of a program given a value, it would be easier to read the code syntax using alternative syntax.
O please wise and mighty SO members, enlighten me please - when would I use or should I use the alternative way at all?
There's actually no difference; the ":" is just a line separator, formatting if you will - not a convention specific to variable declaration:
http://msdn.microsoft.com/en-gb/library/ba9sxbw4.aspx
In some cases, using it makes the code more readable, but clearly it also has the potential to confuse :)
I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?
I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?
Fairly new to VBA and I just started encountering the error Unable to get TextBoxes Property of Worksheet Class.
It highlights the following line:
ActiveSheet.TextBoxes("txtFilePath").Text = Application.ActiveWorkbook.Path & "\"
The mystery to me is that I know this line was working before without any problems. The only thing I could think of is that I was messing with protecting the sheet, but even now that it is unprotected I still get the error. I've also tried the following solution, but encounter the same error:
ActiveSheet.OLEObjects("txtFilePath").Object.Text = Application.ActiveWorkbook.Path & "\"
Can anyone explain to me why I am encountering the error and why I started encountering it so randomly? How can I fix it?
Sadly enough, there is no clear way on how to access the properties of a grouped shape (a group is basically a shape, at least in 2010). There are two possible solutions to go about this.
One is obviously to ungroup the textboxes, access the textbox in question and modify it, then regroup them. However, this will prove to be difficult to track as the Group number increases with every regroup that you do. This can pose a potential problem if you have multiple groupings.
The other way, surprisingly enough, is to do it the 'dirty' way, which is basically what the macro recorder gives us in a nutshell. The way to do it is to select the shape itself and to change the text directly, emulating a direct click-and-type motion manually. I've tested it and it works, even when the textbox in question is grouped multiple times.
Sub Macro3()
foo = Application.ActiveWorkbook.Path & "\"
ActiveSheet.Shapes.Range(Array("txtFilePath")).Select
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = foo
End Sub
Sorry to answer my own question. Figured it out by accident.
The problem was that I had my textbox (txtFilePath) grouped together with another textbox on my worksheet. When I ungrouped them everything worked fine. Can anyone explain to me why the grouping would make a difference?
I've encountered a very strange bug in VBA and wondered if anyone could shed some light?
I'm calling a worksheet function like this:
Dim lMyRow As Long
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
This is intended to get the row of the item I pass in. Under certain circumstances (although I can't pin down exactly when), odd things happen to the call to the Match function.
If I execute that line in the immediate window, I get the following:
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
?lMyRow
10
i.e. the lookup works, and lMyRow gets a value assigned to it. If I let that statement execute in the actual code, I lMyRow gets a value of 0.
This seems very odd! I don't understand how executing something in the immediate window can succeed in assigning a value, where the same call, at the same point in program execution can give a value of 0 when it runs normally in code!
The only thing I can think of is that it's some odd casting thing, but I get the same behaviour taking if the variable to which I'm assigning is an int, a double, or even a string.
I don't even know where to begin with this - help!!
The only difference between the immediate window and normal code run is the scope.
Code in the immediate window runs in the current application scope.
If nothing is currently running this means a global scope.
The code when put in a VBA function is restricted to the function scope.
So my guess is that one of your variables is out of scope.
I would put a breakpoint in your function on that line and add watches to find out which variable is not set.
And if you don't have Option Explicit at the top of your vba code module, you should add it.
You're not assigning the function name so that function will always return zero (if you're expecting a Long). It seems you should have
makeTheLookup = lMyRow
at the end of your function.
I don't know if you are still looking at this or not but I would have written it this way:
Function makeTheLookup(vItemID As Variant, rngMyRange as Range)as Long
makeTheLookUp = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
End Function
I cannot reproduce the problem with Excel 2007.
This was the code I used:
Sub test()
Dim vItemID As Variant
Dim lMyRow As Long
Dim rngMyRange As Range
Set rngMyRange = ActiveWorkbook.Sheets(1).Range("A1:Z256")
vItemID = 8
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
Debug.Print lMyRow
End Sub
It may sound stupid but are you sure that all parameters of the Match function are the same in your macro and in the immediate window? Maybe the range object has changed?
Thanks for the answers guys - I should have been slightly more specific in the way I'm making the call below:
Function makeTheLookup(vItemID As Variant, rngMyRange as Range)
Dim lMyRow As Long
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
End Function
The odd thing is, I'm passing the two parameters into the function so I can't see any way they could be different inside and outside of the function. That said, I'm still entirely clueless as to what's causing this, particularly since it's a really intermittent problem
Is there any easy way of comparing the range object in the function context to the range object in the Immediate window context and telling if they're different? Given that range is a reference type, it feels like I should just be able to compare two pointers, but I've got no idea how to do that in VBA!
I'm using Excel 2007 by the way, although I'm not sure if that makes any difference.
As will has mentioned above, It most definitely seems like a problem of scope. Or an Obscure Bug.
I would try to use Debug.print statements, as well as Watch, and see if they match up, and take it from there.