Application Defined or oBject Defined Error - Cannot Solve - excel

Sub gains()
Dim date_data As Long
Dim date_h As Long
Dim cell As Range
Dim analysis_rng As Range
Dim data_h As Range
Set analysis_rng = Application.Worksheets("DataDPI").Range("A2:A107")
Set data_h = Application.Worksheets("Data").Range("A2:A525")
For Each cell In analysis_rng
cell.Offset(0, 2) = data_h.Find(cell, LookIn:=xlValues)
Next cell
End Sub
It was working and then stopped working. Data is the same and the worksheets are labeled correctly. Error 1004
Spent 15 minutes trying to debug already
I can fill the cell.offset(0,4) with text/number just fine but I can no longer do data_h.find (range.find). i was testing it earlier and was working okay```
Purpose
I have a main dataset, it's a bunch of dates open close prices: workhseet "Data"
Secondary set is a select few dates from the main data set and other . I am just trying to see what changes are but before that, I just want to make sure i'm referencing the right data
So in the code posted above, once it was copying over the right data from "data" to cell.offset, I was going to just use offset to collect data as I wanted from main set: data_h to the new "DataDPI"

Related

I'm trying to work out a way to create and IF function to determine if filters applied to a table in VBA return an Empty table

I'm creating an excel workbook that has a dashboard on the front page of it. when two dates are selected from a drop down and the "refresh data" button is pressed, it runs a macro that fills in all the charts and tables on the dashboard page based on data tables in other worksheets within the workbook.
I'm trying to add a layer of dynamism so that if a date is selected where not all information, or even any information, is to be found, it lets the user know. (some information can be late coming to the work book)
So far I've tried a few ways with the closest maybe being the following options, but none of them work properly.
The beginning code is
Sub Macro3()
Dim DataM As Variant
Dim DataY As Variant
Dim ws1 As Worksheet
Dim DD1 As OLEObject
Dim DD2 As OLEObject
Dim i As Byte
Set ws1 = Worksheets("KPI Dashboard")
Set DD1 = ws1.OLEObjects("DMonth") 'this is the month input
Set DD2 = ws1.OLEObjects("Dyear") 'this is the year input
Set DataM = DD1.Object
Set DataY = DD2.Object
'part 1 is for utilisation
Set ws2 = Worksheets("People Info")
ws2.ListObjects("Utilisation").Range.AutoFilter Field:=2, Criteria1:=DataM.Value
ws2.ListObjects("Utilisation").Range.AutoFilter Field:=1, Criteria1:=DataY.Value
My first attempt was to count the rows in the databodyrange, but every time I tried that I would get an error saying there was no cells. I know I oculd normally put an "on error" line of code, but I want it to highlight to the user that there is a table missing data and then go onto the next table and keep going. I don't know of any ways in which I could dynamically have error responses in one function to the same error.
My second attempt was to just count the tables overall rows, with a msgbox to say there was no data like below:
If ws2.ListObjects("Utilisation").Range.SpecialCells(xlCellTypeVisible).Rows.Count = 1 Then
If MsgBox("It Looks like we have no data for Utilisation, please fill it in and run it again. do you want to continue?", vbYesNo) = vbNo Then
Exit Sub
End If
ws2.ListObjects("Utilisation").AutoFilter.ShowAllData
GoTo S1P2
End If
But it always returns the count of rows as 1, even when I can see that it clearly has 5 or 6 rows in it.
So the last thing I tried was to select the area, and see if I could count that selection to greater success, but again it would still return 1, even on the count
Can anyone think of a better way to try it?
I have also tried using the IsEmpty() function and a handful of others like that but can't seem to get it to work. it either says there are no cells in the databody range, because they have been queried out or it says there is 1 row in the table range, even when there isn't.
Any help on how to make it work, or even a different avenue with which to look down would be really helpful.

Dynamic Named Range not recalculating when using .ClearContents

Below is a simplified example:
I have contiguous data in A1:E10.
I have a Dynamic Named Range (scope = workbook) defined by the following formula:
DynRange =OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),COUNTA(Sheet1!$1:$1))
I then run the following VBA code:
Dim dynrng As Range
Set dynrng = ThisWorkbook.Names("DynRange").RefersToRange
Range("A8:E10").Delete xlUp
MsgBox ThisWorkbook.Names("DynRange").RefersToRange.rows.Count
MsgBox dynrng.rows.Count
Range("A7:E10").FillDown '(just adding the data back for our next case below)
Both message boxes return 7: i.e. the workbook named range and the VBA range variable we set to it both update as the data is modified.
Now I run the same code but with .ClearContents (or .Clear) instead of .Delete:
Dim dynrng As Range
Set dynrng = ThisWorkbook.Names("DynRange").RefersToRange
Range("A8:E10").ClearContents
MsgBox ThisWorkbook.Names("DynRange").RefersToRange.rows.Count
MsgBox dynrng.rows.Count
The first message box returns 7, but the second returns 10.
Why is the VBA range variable not updating along with the workbook named range in the second case?
(calculation is set to automatic, and I tried Application.Calculate)
Thanks in advance for any illumination!
Name.RefersToRange returns a Range object. Once you have
Set dynrng = ThisWorkbook.Names("DynRange").RefersToRange
dynrng no longer has any reference to the named range, it is simply equivalent to
Sheet1.Range("A1:E10")
If you delete rows, then the Range object is affected accordingly.
If you clear contents, Sheet1.Range("A1:E10") still has 10 rows.

Can I set a dynamic range in vba excel to use in filter

The following excel sub is a filter that is filtering out rows based on the rows in the criteria row.
The code works well when the ranges are set with absolute data. I want to change the code to take the range from references stored as cell values (an indirect reference) but I cannot find a way to adapt other code snippets I see to work and I wonder if anyone can help me. I am not a programmer.
The problem is that as new data is inserted from time to time the range reference will move and the start of the data an the associated filter is in cell (using RC notation 14,14) and the data in cell 13,12. While I know I can’t use the indirect function in vba I wondered if there is a way to dynamically assign a range to be able to use the Advance filter function.
I have the code to find the last column and row of the data block.
I have tried the following code (2 attempts) but it won’t let me use the object in this way
I have tried to crate the cell reference as a string then assign it using the range function. I then read an answer where someone had put the value of the cells directly into the range function and it has worked for them ( they were copying cells). The 2 attempt are broadly the same but in the second I am trying to be more specific.
The issue seems to be as soon as I change from an absolute reference "A50" in the range statement the range no longer works. I am unsure how to resolve this and perhaps it can't be
It may be helpful to know the that data being filtered is rows of name and telephone data along with a tally system to show attendance (one column per week for a year)
The cells with the dynamic data hold them in the form A1 not RC format
Sub UseAdvancedFilterInPlace()
'This version of the sub has absolute references and works perfectly
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Set rgData = Sheet9.Range(“A50”).CurrentRegion
Set rgcriteria = Sheet9.Range(“A46”).CurrentRegion
rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff
End Sub
Sub UseAdvancedFilterInPlace()
'This version of the sub has dynamic references and fails
Dim rdData As Range
Dim rgcriteria As Range
Call TurnOffStuff
Dim Top_of_data As String
Dim Top_of_Criteria As String
Dim My_range As Range
‘Attempt 1
'Set rgData = Range(Sheet9.Cells(13, 12).Value).CurrentRegion
'Set rgcriteria = Range(Sheet9.Cells(14, 14).Value).CurrentRegion
'Attempt 2
Set rgData = Sheet9.Range(Sheet9.Range(Cells(13, 12)).Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range(Cells(14, 14)).Value).CurrentRegion
rgData.AdvancedFilter xlFilterInPlace, rgcriteria
Call TurnOnStuff
End Sub
The actual error message I get is an application-defined or object-defined error
This worked for me.
Set rdData = Sheet9.Range(Sheet9.Range("L13").Value).CurrentRegion
Set rgcriteria = Sheet9.Range(Sheet9.Range("N15").Value).CurrentRegion
given that Range("L13").Value is A50 and Range("N15").Value is A46.
extra: Use the statement Option Explicit in the first line of every module, out of every sub or function. This option throws an error on undeclared variables, and will help you avoid renameing mistakes on variables.

How to get speed up data getting by ignoring empty cells?

I've ran into a problem with a processing tool I'm working on, namely with the initial hang time being close to 10 seconds long. The issue I'd identified is that when the data is originally Imported into the it proceeds to update every lookup array and function in the 5x1000 workspace. I've been thinking that a good way to speed up the processing was to limit the import to non-empty cells only but have been unable to get it done no matter what I tried.
By default this is the function that imports the data from the RadGridExport temp file into a buffer sheet for later processing:
Sub CopyData()
Dim rng As Range
Set rng = Workbooks("RadGridExport.xls").Sheets(1).Range("A1:E1000")
ThisWorkbook.Sheets(2).Range("A1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
End Sub
I have attempted to use IsEmpty(Cell.Value) method but that was less than effective
Sub CopyData()
Dim rng As Range
Set rng = Workbooks("RadGridExport.xls").Sheets(1).Range("A1:E1000")
For Each Cell In rng
If IsEmpty(Cell.Value) = False Then
ThisWorkbook.Sheets(2).Range("A1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
End If
Next Cell
End Sub
In fact it froze the entire workbook. That was the only way I'm familiar with that could have applied, so I'm in a bit of a dead end.
Is there a way to actually get my initial function to ignore empty cells? Or am I coming at this from the wrong way and there's some method for keeping the worksheet from doing a full update on every single cell, including those whose value doesn't change?
I have managed to find a solution to my problem.
First of all thank to #Pᴇʜ for an way to do dynamic ranges - I ended up not using it in this tool as a lot of processing relies on static cells in a range for auto calculations, but I have written it down for the future!
#JvdV 's suggestion worked like a charm, bringing the load time down from 31 seconds to 14 seconds average.
Current codes looks like this:
Sub CopyData()
Dim rng As Range
Set rng = Workbooks("RadGridExport.xls").Sheets(1).Range("A1:E1000").SpecialCells(2)
ThisWorkbook.Sheets(2).Range("A1").Resize(rng.Rows.Count, rng.Columns.Count).Cells.Value = rng.Cells.Value
End Sub
However it was still forcing a full refresh on all static values - so after digging deeper I realized that I was using a massively taxing clear gate in the main macro:
Sub clearAll()
Range("A:E").ClearContents
End Sub
Which caused that massive 14 second delay. After I changed that function to this:
Dim Workspace As Range
Set Workspace = ThisWorkbook.Sheets(2).Range("A1:E1000")
For Each Cell In Workspace
If IsEmpty(Cell.Value) = False Then
Cell.ClearContents
End If
Next Cell
The load time went down further from 14 seconds to under 2 seconds total.
Thanks again :)

VBA 'Compile error: Method or data member not found' when bringing value into range

Complete VBA beginner and on a button click trying to take a value (it will be the eventual column value for pasting command) stored at RawData sheet in A1, convert it to text or a value (i guess?) and store it as revValue then add it to a range in a copy and paste command. I know the copy and paste is working fine when i just put Range values i.e. Destination:=dir.Range("A30"), but with Destination:=dir.Cells(30, revValue) getting 'Compile error: Method or data member not found' ...sure it's a really obvious one but driving me nuts. Any help appreciated.
Private Sub Import_Click()
Dim source As Worksheet
Dim dir As Worksheet
Dim revValue As Range
Set source = Worksheets("RawData")
Set dir = Worksheets("Register")
'Store the RawData Cell value as a text or integer?
Set revValue = source.Cell(1, 1).Value
Worksheets("RawData").Activate
Worksheets("RawData").Columns("A:D").AutoFit
'These two function work fine
source.Range("C1", Range("C1").End(xlDown)).Copy Destination:=dir.Range("A30")
source.Range("D1", Range("D1").End(xlDown)).Copy Destination:=dir.Range("J30")
'This one doesn't
source.Range("E1", Range("E1").End(xlDown)).Copy Destination:=dir.Cells(30, revValue)
End Sub
.Cells uses integers but you're using revValue which is a range variable.

Resources