I am using the following code:
Dim rng As Range
Set rng = Workbooks("import sheet.xls").Sheets("import").Range("project_name,project_author,project_code,project_breaker,default_fault_ac_mcb,default_fault_ac_mccb,default_fault_dc,default_fault_acb,default_rvdrop,default_svdrop,default_eff,default_pfactor,default_ratio,default_freq,default_sfactor_ac,default_spfactor")
For Each cell In rng.Cells
MsgBox cell
Next cell
Now project_name, project_author, etc. are different named ranges in the sheet. The problem is when I add another named range to the above list (already defined), I get a runtime error 1004 ("select method of range class failed").
Is there a limit to the number of named ranges one can add to range object?
I couldn't find anything on why this happens but I am still looking.
However this works. You can combine the range using UNION
Sub Sample()
Dim rng As Range, cell As Range, newRng As Range
Set rng = Sheets("import").Range("project_name,project_author,project_code,project_breaker,default_fault_ac_mcb,default_fault_ac_mccb,default_fault_dc,default_fault_acb,default_rvdrop,default_svdrop,default_eff,default_pfactor,default_ratio,default_freq,default_sfactor_ac,default_spfactor")
Set newRng = Union(rng, Sheets("import").Range("default_sid"))
For Each cell In newRng.Cells
MsgBox cell
Next cell
End Sub
UPDATE: I couldn't find any Microsoft document which describes this behavior, but after some experimentation, I can confirm that the maximum number of named ranges one can add to range object is 16. This behavior can be seen in in Excel 2003, 2007, and 2010.
Strangely this behavior was not replicated in Excel 2011 (MAC). I tested with 19 names and it worked.
MORE UPDATE
After more testing, I discovered that if you name your ranges as Name1, Name2, Name3 etc then I was able to add more named Ranges in Excel 2003-2007-2010. So it has to do something with the naming of the names.
I have filed a bug with Microsoft.
SAMPLE FILE FOR TESTING:
https://skydrive.live.com/redir.aspx?cid=cdd3f8abe20bbe3b&resid=CDD3F8ABE20BBE3B!157&parid=CDD3F8ABE20BBE3B!150&authkey=!ADP_QVBopsqenBA
I have not investigated your problem but the points below may give you and Sid some hints on the possible solution.
If you access Rng.Address there is a limit on the size of the string. I have found comments on the internet claiming the limit is 253 but I do not believe this. Rng.Address is always truncated at the end of a complete sub-range so the string you get back is syntactically correct just incomplete. So the limit depends on the exact string size of the sub-ranges.
I had the opposite problem. I had a sheet with thousands of rows. I used AutoFilter to select the rows of interest and saved the range. But Rng.Address only gave me the first 19 or so ranges of interest. I had to use For Each Cell In Rng and assemble the range string to get the full string.
Your string is 255 characters long. There is clearly no limit on the number of sub-ranges in a union. You could try adding the sub-ranges to the union individually.
Best of luck.
PS If you want my assemble range string routine you are welcome.
Related
I am trying to copy contents from a specific range in a Word table, to another range within the same table. I am aware this code is quite messy/simple, but i am currently just trying to get it to work and mapping the different ranges. The current copy/paste method worked well until i added the last range, where it returns "Run-time error '4605'
This command is not available.".
My first attempt was using selection.copy/paste, but that seems to be less efficient (and returned a similar, if not the same, error). I have tried using Rng=Rng2.text, however that somehow only transfers part of the range.
When i round the code in debug step-by-step it works - so it seems like the code is running too fast for the clipboard to follow along or something like that? I suppose i have to use a different method, but currently i am out of ideas (not very experienced with working with ranges in Word) - any ideas?
Note that some of the ranges share cells, which may complicate things. Also, this is my first post on here, so i apologize in advance if it may be a bit messy.
The code:
Sub test()
Set Rng = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(23, 3).Range.start, End:=ActiveDocument.Tables(4).Cell(26, 4).Range.End)
Rng.Copy
Set Rng2 = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(23, 2).Range.start, End:=ActiveDocument.Tables(4).Cell(26, 3).Range.End)
Rng2.Paste
Set Rng = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(22, 4).Range.start, End:=ActiveDocument.Tables(4).Cell(22, 5).Range.End)
Rng.Copy
Set Rng2 = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(22, 3).Range.start, End:=ActiveDocument.Tables(4).Cell(22, 4).Range.End)
Rng2.Paste
Set Rng = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(23, 6).Range.start, End:=ActiveDocument.Tables(4).Cell(26, 6).Range.End)
Rng.Copy
Set Rng2 = ActiveDocument.Range(start:=ActiveDocument.Tables(4).Cell(23, 4).Range.start, End:=ActiveDocument.Tables(4).Cell(26, 5).Range.End)
Rng2.Paste
End sub
I have three parts to this problem. I have a single cell with a Week number in Sheet1!A1. I have a static range in Sheet1!B1:F1 that needs to be copied. Then I need to paste the value in a dynamic range in Sheet2 offset by the week number for rows. This is part of a larger macro I am writing for a sheet I use regularly, but I seem to have those parts down. I may be either oversimplifying or oversimplifying but this is what I have currently.
Sub CopyPaste()
Sheets(1).Range("B1:F1").Copy
OffsetRange = Sheets(1).Cells(1,1).Value
Sheets(2).Cells(1+OffsetRange,1).Paste
End Sub
When I run this, it either gives me a Runtime Error 9 or Runtime Error 438.
Anyone know whats causing these errors? When I paste the range, does the cells object point towards the first cell of the copied range when I paste in at the location?
Try it as,
Option Explicit
Sub CopyPasteOffset()
Dim OffsetRange As Long
OffsetRange = Worksheets(1).Cells(1, 1).Value
Worksheets(1).Range("B1:F1").Copy _
Destination:=Worksheets(2).Cells(1 + OffsetRange, 1)
End Sub
The .Paste method is a member of Worksheet, not Range or Cells. You may have it confused with .PasteSpecial which is a member of the Range object. In any event, it is unnecessary as a destination can be applied directly to the copy command.
Running VBA code allowed me to detect that applying a filter in a sheet creates a range name that does NOT appear in the name manager (the range name either just associates to the headers or the whole table). Is there any way of knowing why this happens and/or a way of preventing it? Is it just an Excel glitch?
the part of the vba code that causes the error I tried to run was the following:
For Each Rng In ActiveWorkbook.Names
Set Rng2 = Range(Rng)
If Not Intersect(Rng2, Range(rng1.Offset(1, 0), rng1.End(xlDown)).EntireRow) Is Nothing Then ActiveWorkbook.Names(Rng.Name).Delete
Next Rng
When I was debugging, I noticed that my Rng (which is a name object, by the way) points towards a range I never created (and I know this because the sheet that it's in has no other range names and I never put any in it)
I used this vba code to verify that the name existed in this sheet:
Sub test()
Dim Rng As Name
For Each Rng In Sheets("WindHail Zone 2").Names
'ActiveWorkbook.Names(Rng.Name).Delete
MsgBox Rng
Next Rng
End Sub
I removed the comment block from the first line of the for loop to the second line to delete it. I removed the filter on that tab and reput it on, only to come to the same issue.
Thanks!
Not so clear to me what your're trying to achieve but as long as "hidden" names and range intersection are involved you must take into account what follows:
filtering does create hidden "names"
but all of them ends with the string "_FilterDatabase"
range intersection wold return an error if applied to ranges not belonging to the same sheet
so here what you should try
For Each Rng In ActiveWorkbook.Names
Set rng2 = Range(Rng)
If rng2.Parent.Name = rng1.Parent.Name And InStr(Rng.Name, "_FilterDatabase") = 0 Then 'the first check is for ranges belonging to the same worksheet and the second is for rng2 not deriving from any filtering
' now you can safely "Intersect"
If Not Intersect(rng2, Range(rng1.Offset(1, 0), rng1.End(xlDown)).EntireRow) Is Nothing Then ActiveWorkbook.Names(Rng.Name).Delete
End If
Next Rng
I'm trying to copy only data (excluding blanks created by If statement) from "Data" tab then paste to the bottom of a data column on the "Summary" tab. The trouble arises from trying to figure out how to get VBA to recognize the range of usable data.
There are a couple of different ways to do this, depending on what you need. Here's one SO thread that discusses a few uses. Here's another page that discusses using UsedRange or .Rows. And, as #findwindow noted, you can use .xlEnd.
This is a pretty common use of VBA, so if you Google around (or even look through SO), you'll find some information.
Edit: Per your comment, just set a range, and loop through the cells in the range until you find a non-numeric number:
Dim rng as Range, cel as Range
Dim lastRow as Integer
Set rng = Range("A1:A10000")
for each cel in rng
If not isnumeric(cel.value) then
'Do whatever code you want, when the cell is NOT numeric, ie
lastRow = cel.Row
End if
next cel
I'm having troubling referring to a Dynamic Name Range in VBA.
My ranges are defined as
=OFFSET(Sheet!$B$2,0,0,COUNTA(Sheet!$B:$B)-1,1)
My code should search one range for all entries in another range, the intention being that any missing entries will be added. So far I have
Sub UpdateSummary()
Dim Cell As Range
Dim rngF As Range
Set rngF = Nothing
' Step through each cell in data range
For Each Cell In Worksheets("Aspect").Range("A_Date")
' search Summary range for current cell value
Set rngF = Worksheets("Summary").Range("Sum_Date").Find(Cell.Value) // Does not work
If rngF Is Nothing Then
' Add date to Summary
End If
Set rngF = Nothing
Next Cell
End Sub
The For loop seems to work ok. However, using the .Find method is giving me an error message.
Application-defined or object-defined error
It does work if I replace the named range with a specific range ($B$2:$B$5000), so it seems to be down to how the named range is being passed.
Any ideas would be appreciated.
Thanks.
The error is almost definitely because Excel can't find a named range Sum_Date that refers to a range on a worksheet named Summary. The most common causes are
Sum_Date refers to a sheet other than Summary. Check the RefersTo property of Sum_Date and make sure nothing is misspelled.
There is not a named range Sum_Date, that is, it's misspelled in the VBA code. Check the spelling of the named range in the Name Manager.
There is an error in the RefersTo formula of Sum_Date. It sounds like you already verified that this isn't the case.
I've had the a similar if not the same problem & here's how I solved it:
I first realized that the method I used to create my named range, using the Name Manager, my named range had a scope of Workbook. This is important because, it doesn't belong to the worksheet, & therefore will not be found there.
So, Worksheets("Summary").Range("Sum_Date") would not work for me.
Since my range belonged to the workbook, the way I was able to find is to use ActiveWorkbook.Names("Sum_Date")
For me I used it to remove the formula from named range that I am using in many places. The huge advantage is that named range is updated only once instead of the formula being called for every cell location that ranged is called. Huge time delay difference!
Public last_Selection As String
Private Sub Worksheet_Change(ByVal Target As Range)
'excel data change detection
If Range(last_Selection).Column = 2 Then
'Disable events, so this only executes once
Application.EnableEvents = False
'This can be done with a complex formula in a cell,
'but this is easily understood
Range("B1").End(xlDown).Select
ActiveWorkbook.Names("last_Entry").Value = ActiveCell.Row
'Re-enable so this routine will execute on the next change
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'constantly store the last cell to know which one was previously edited
last_Selection = Target.Address
End Sub
I know this is a very old thread, but I had the same issue today and I was looking for solution for quite a long time. So maybe this will help someone.
The named "range" defined by the =OFFSET(...) formula is actually a named FORMULA, so in VBA you have to evaluate it first to get the range. E.g.:
Set rgNamedRange = Worksheets("Summary").Evaluate("Sum_Date")
Credits to a guy named "shg" from mrexcel.com, who got me on right track. :)
I have been experimenting with this for a few days and eventually I came up with the following. It may not be the most efficient but it did work for me!
The named range of "OhDear" was set up in the normal way
Dim vItem As Variant
Set vItem = Names("OhDear")
Debug.Print vItem.Name
Worth a try don't you think!
This does not work if instead of using a variant you use something like: Dim Nm as Name: Set Nm = Names("OhDear"). Any variations using 'Nm' failed!!!