I'm trying to make an array of ranges, but I'm getting "application-defined or object-defined error". The error is appearing on the line with Set Rng. The format should be fine, even if the line is somewhat long. I've specified the sheet I'm trying to get the ranges from, and the sub is currently in Module 1. It could just be a typo error somewhere, but after having re-checked the line six times I'd be disappointed if that was it.
Sub TableRange(ByVal Target As Range)
Dim Rng As Range
Dim Area As Range
Set Rng = Worksheets("Tables").Range("A3:D23,A28:C39,A44:E61,A66:C102,A107:E121,A126:C135,A140:C149,A153:C162,A167:C192,A197:F215,A220:C269,A274:D282,A287:D295,A300:D304")
Set Rng = Union(Rng, Worksheets("Tables").Range("A309:C358,A363:C389,A394:C412,A417:C437,A442:C462,A467:D475,A480:D487,A492:C531,A536:D544,A549:D557,A562:C574,A579:D598,A603:D622"))
For Each Area In Rng.Areas
If Not Intersect(Target, Worksheets("Tables").Range(Area)) Is Nothing Then
'do stuff
End If
Next Area
End Sub
If there is anything else I should mention or that I can do to improve my question, let me know and I'll edit my post accordingly.
Update: Range array has been fixed, thanks to #user3964075, but now I seem to be stuck with the same error on If Not Intersect - Is Nothing Then
The code is fine, but string constant exceeds maximum length (255 chars) allowed for Range property. You can easily fix it with:
Set Rng = Worksheets("Tables").Range("A3:D23,A28:C39,A44:E61,A66:C102,A107:E121,A126:C135,A140:C149,A153:C162,A167:C192,A197:F215,A220:C269,A274:D282,A287:D295,A300:D304")
Set Rng = Union(Rng, Worksheets("Tables").Range("A309:C358,A363:C389,A394:C412,A417:C437,A442:C462,A467:D475,A480:D487,A492:C531,A536:D544,A549:D557,A562:C574,A579:D598,A603:D622"))
Maybe consider using Named Range instead of this long list.
EDIT: To fix the second problem, change it to:
If Not Intersect(Target, Area) Is Nothing Then
'do stuff
End If
You are not dealing with the Range.Areas property properly. Try a loop through the index.
dim a as long
For a =1 to Rng.Areas.count
If Not Intersect(Target, Rng.Areas(a)) Is Nothing Then
'do stuff
End If
Next a
I copied the Set Rng Line and had the same error.
When I removed one range, i.e. A28:C39 it works. It does not matter which Range will be removed.
Maybe there is a maximum of Ranges?
Try:
Set Rng = Worksheets("Tabelle1").Range("A3:D23,A44:E61,A66:C102,A107:E121,A126:C135,A140:C149,A153:C162,A167:C192,A197:F215,A220:C269,A274:D282,A287:D295,A300:D304,A309:C358,A363:C389,A394:C412,A417:C437,A442:C462,A467:D475,A480:D487,A492:C531,A536:D544,A549:D557,A562:C574,A579:D598,A603:D622")
Related
I have tried about every solution that I could find.
What I am trying to do is run a Function for selected range (The ActiveSheet)
I can't even get to the looping part because it errors on line 5 (arc = ActiveSheet.Row.Count). The error it throws is "Object Required". I have tried several different solutions found online with no luck. I am a complete noob at excel vba (my background is vb.net and c#). I would greatly appreciate a nudge in the right direction on what going wrong. Thanks in advance :)
Sub TestV2()
Dim rng As Range
Dim selectedRange As Range
Dim arc As Range
Set arc = ActiveSheet.Rows.Count
Set selectedRange = ActiveSheet.Rows.Count
For Each rng In selectedRange.Cells
If Application.CalculationState = xlDone Then
FireValidate
End If
Next rng
End
End Sub
You're getting this error because you declared arc as Range but you are trying to assign a Long number to it.
The Set keyword is required in VBA, to assign object references.
Set arc = ActiveSheet.Rows.Count
Set selectedRange = ActiveSheet.Rows.Count
Both arc and selectedRange are declared As Range, which is an object type - so the Set keyword is correct.
The problem is with the expressions on the right-hand side of the assignment operator: ActiveSheet.Rows.Count evaluates to a Long integer, which is not an object type. ActiveSheet.Rows.Count gets you the number of rows on the ActiveSheet, ...which should be the same number regardless of what specific sheet is currently active (all sheets have the same number of rows).
Hence, object required: you can't legally assign a Range object reference to a Long integer value; you need the right-hand side expression to evaluate to an object reference.
Tim Williams' answer shows how you can correctly assign the selectedRange to the Application.Selection, assuming what's currently selected is a Range object (a type mismatch error will occur if that's not the case).
What I am trying to do is run a Function for selected range (The ActiveSheet)
ActiveSheet doesn't return the selected range - it gives you the sheet that's currently active.
I am a complete noob at excel vba (my background is vb.net and c#)
You can reference the Excel object model from VB.NET or C# as well, and automate Excel through Visual Studio Tools for Office (VSTO) if you're more comfortable with these more powerful .net languages - but the object model will behave identically.
If you just want the current selection then:
Set selectedRange = Selection
You could try to modify your code as follows:
Sub TestV2()
Dim rng As Range
For Each rng In Selection
If Application.CalculationState = xlDone Then
FireValidate
End If
Next rng
End Sub
Hopefully it helps you.
There are three errors in your code:
.1. The key error. Use ActiveSheet.Rows.Count directly may encounter "Object Required" exception, instead, you can use the following worked code:
Dim ws As Worksheet
Set ws = ActiveSheet
'MsgBox ws.Rows.Count
Set arc = ws.Rows.Count
As hod said(this is not the key issue, but need to fix),
You're getting this error because you declared arc as Range but you are trying to assign a Long number to it.
As William have pointed, you can use the Set selectedRange = Selection directly if you want to operate the selected range.
Sub TestV2()
Dim rng As Range
Dim selectedRange As Range
Set selectedRange = Selection
For Each rng In selectedRange.Cells
If Application.CalculationState = xlDone Then
'FireValidate
'MsgBox "OK"
End If
Next rng
End Sub
And I think most time we just need the UsedRange.Rows but not the Rows all.
ActiveSheet.UsedRange.Rows.Count for the current sheet.
Worksheets("Sheet name").UsedRange.Rows.Count for a specific sheet.
I once built a VBA button to automatically lock all cells with data in them. And it was working perfectly. Now I wanted to copy that button to another worksheet. So I created another button, copy and pasted the whole VBA over, then edited the worksheet names and range. And, it's only working like 5% of the time, the rest of the time, I'm getting an "Run-Time error '1004': No cells were found." I've tried a few fixed, changing Sheets to Worksheets, or adding a ", 23" to the specialcells argument. However, nothing is working right now. When I try stepping in, it sometimes say both rng and lckrng as empty, and sometimes only show lockrng as empty and not show rng at all. Problem is this used to be a working code, and now, it still works around 5% of time. Any idea why? Thank you very much!
Private Sub CommandButton1_Click()
Dim rng As Range
Dim lockrng As Range
Sheets("Uploading Checklist (M)").Unprotect Password:="signature"
Set rng = Range("A1:M14")
'Selecting hardcoded data and formulas
Set lockrng = Union(rng.SpecialCells(xlCellTypeConstants), rng.SpecialCells(xlCellTypeFormulas))
lockrng.Locked = True
Sheets("Uploading Checklist (M)").Protect Password:="signature"
End Sub
Maybe this is too simplistic, but it seems to do what you want. The animated .gif shows it working to "lock all cells with data in them". (I made the second button just for convenience). If nothing else it might be good to start from something like this that works and modify to suit your needs.
Dim cell As Range, sh As Worksheet
Sub Button4_Click()
Set sh = Worksheets("Sheet1")
sh.Unprotect Password:="s"
For Each cell In sh.UsedRange
If cell <> "" Then cell.Locked = True Else cell.Locked = False
Next
sh.Protect Password:="s"
End Sub
Sub Button5_Click()
Set sh = Worksheets("Sheet1")
sh.Unprotect Password:="s"
End Sub
The Union you are attempting will not work if either of the parameters is Nothing (i.e. you either have no constants in the range, or you have no formulas in the range).
Prior to doing the Union, you should check the parameters aren't Nothing but, once you start changing your code to do that, it would be just as simple to do the locking in two parts - so I recommend you rewrite the code as follows:
Private Sub CommandButton1_Click()
With Sheets("Uploading Checklist (M)")
.Unprotect Password:="signature"
With .Range("A1:M14")
'Lock any constants
If Not .SpecialCells(xlCellTypeConstants) Is Nothing Then
.SpecialCells(xlCellTypeConstants).Locked = True
End If
'Lock any formulas
If Not .SpecialCells(xlCellTypeFormulas) Is Nothing Then
.SpecialCells(xlCellTypeFormulas).Locked = True
End If
End With
.Protect Password:="signature"
End With
End Sub
Why does the cell value not set in another function when clearing the contents of the range in another function?
I'm trying to set a cell value to "All" after clearing the cells in the range. I've even tried to get a message box to pop up to see if i can somehow check if my check value is correct.
DelRange is the range i'm clearing.
Building is the cell that i'm checking the value for and if it's blank, it needs to change to "All".
clearPreviw is used to clear another sheet, which it's doing.
Sub ClearSheet()
Dim Dash As Worksheet
Dim DelRange As Range
Dim Building As Range
Set Dash = ActiveWorkbook.Worksheets("DASH")
Set DelRange = Dash.Range("FilterData")
Set Building = Dash.Range("SelBuild")
DelRange.ClearContents
Call clearPreview
'This part below doesn't work when the Range.ClearContents has been done, but doing it on it's own without clearing the range works fine
If Building.Value = "" Then
MsgBox "Building is empty", vbOKOnly
Building.Value = "All"
End If
End Sub
I've run this test as a separate process which works, but once again when running it as a call function right after .ClearContents seems to stop this.
Sub test()
Dim Dash As Worksheet
Dim DelRange As Range
Dim Building As Range
Set Dash = ActiveWorkbook.Worksheets("DASH")
Set DelRange = Dash.Range("FilterData")
Set Building = Dash.Range("SelBuild")
If Building.Value = "" Then
MsgBox "Building is empty", vbOKOnly
Building.Value = "All"
End If
End Sub
I've been poking at it and searching but i can't wrap my head around this.
I think you are missing:
Building.ClearContents;
Also I would prefer:
If IsEmpty(Building.Value) Then
over:
If Building.Value = "" Then
This link gives you a good start on how to set range variables (although I would advice you against the use of .Select and .Activate).
After that, use .ClearContents or .Clear, depending on your needs.
If you properly cleared the ranges, there is no need to check if they are empty, so this might be a redundant step within your current planning.
I've searched online and found a few solutions, but none of them make sense to me. I'm wondering why this specifically doesn't work:
Dim rng As Range: Set rng = Range("A5:A10")
For Each cell In rng
Dim contents As String: contents = ThisWorkbook.Sheets("ROI's").Range("cell").Value
MsgBox (contents)
Next cell
(BTW this is within a larger macro which works)
It keep saying that the error is on the third line
In addition to Scott Craners answer, take the parenthesis away from around contents in MsgBox (contents), you are not placing it into a variable so it should not be enclosed.
Sub try2()
Dim rng As Range
Dim cell As Range
Dim contents As String
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Set rng = Range("A1:A10")
For Each cell In rng
contents = ws.Range(cell.Address(0, 0)).Value
MsgBox (contents)
Next cell
End Sub
I've been practicing wtih various problems concerning VBA...the above is just a snippet synthesizing what all the fine people above me have said about making this work. My 2 cents, brackets or not around the contents variable, the result is the same.
I have looked around for an answer and in fact the code that I am using is from this site. I have the VBA for changing PivotTable filters by inputing a value into a seperate cell like so:
Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Sheets("Dashboard").Range("Lane1")) Is Nothing Then
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG"). _
ClearAllFilters
Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG").CurrentPage _
= Sheets("Dashboard").Range("Lane1").Value
End If
End SUb
The code works fine. It lets me enter the value and filters accordingly, but when I delete the value in the cell it does not filter "all". Instead, the problem I run into is that a runtime error '1004' is thrown when I delete the value in the cell. I hit end and it gives me the answer. However the error keeps popping up.
I am pretty much a newborn when it comes VBA, so it might be so that I have missed something glaringly obvious.
Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng=Me.Range("Lane1")
If Not Application.Intersect(Target, rng) Is Nothing Then
With Sheets("Dashboard").PivotTables("PivotTable1").PivotFields("TLEG")
.ClearAllFilters
If Len(rng.Value)>0 Then .CurrentPage = rng.Value
End With
End If
End SUb