Application-defined or object-defined error

azizrasul

Well-known Member
Joined
Jul 7, 2003
Messages
1,304
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have the following code, where I get the error (Application-defined or object-defined error) on the line

ActiveSheet.Range(Cells(6, z), Cells(10, z)).Select. What am I doing wrong?

Code:
    For Each rng In Range("DateRange")
        If strDateRange <> rng.Value Then
            strDateRange = rng.Value
            Cells(8, 8 + x).Value = rng.Value
            Range(Cells(8, 8 + x), Cells(8, 9 + x)).Select
            Selection.Merge
            Selection.HorizontalAlignment = -4108
            Selection.Font.Bold = True
            Cells(9, 8 + x).Value = "Baseline"
            Cells(10, 8 + x).Value = "Actual"
            Cells(10, 9 + x).Value = "Target"
            Sheets("TARGETS").Select
            ActiveSheet.Cells(5, 3).Select
            For z = 3 To 12
                If ActiveSheet.Cells(5, z) = rng.Value Then
                    ActiveSheet.Range(Cells(6, z), Cells(10, z)).Select
                    Selection.Copy
                    Sheets("KEY PERFORMANCE INDICATORS").Select
                    Cells(11, 9 + x).Select
                    ActiveSheet.Paste
                    Exit For
                End If
            Next z
            x = x + 2
        End If
    Next rng
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Is this is a standard VBA module, or a Sheet VBA module?
Do you have any merged cells or protected/locked cells in this range?
 
Upvote 0
The code is run by pressing a command button on the sheet "KEY PERFORMANCE INDICATORS".
No.
 
Upvote 0
The code is run by pressing a command button on the sheet "KEY PERFORMANCE INDICATORS".
OK, but that command button is linked to the code you posted, right? The VBA code you posted is located in a VBA module. There are different kind of VBA modules. If you have it in the wrong one, your code will not work!

Go into your VB Editor, view the VBA Project Explorer and see what kind of Module your code is found in. Is it under "Modules" or "Sheet1..." or "ThisWorkbook"?
 
Upvote 0
It's not under Modules.

It's under

Microsoft Excel Objects
Sheet1("KEY PERFORMANCE INDICATORS")

The code begins as

Private Sub cmdRun_Click()

..
..

End Sub
 
Upvote 0
Yep, just as I thought. I helped someone just a few days ago with the same problem. If you put a module that covers multiple sheets into a sheet module like you have, it will not work properly (sheet modules are just for code that affects ONLY that particular sheet).

If the code covers multiple sheets, you will need to move it to a standard module. Just click in the VBA Project Explorer, select Insert, then select Module. Then, cut and paste your VBA code from the current location to this new module. Then it should work properly.
 
Upvote 0
When I try that and press the command button, nothing happens!
 
Upvote 0
Since you moved the VBA code, you might need to re-connect the button to the macro. If you right click on the button, does it give you the option to re-map/attach a macro?

At worst, you may just need to create a new command button, which should prompt you to attach a macro, and then you can select the revised macro.
 
Upvote 0
The problem is probably because of the use of unqualified cell references, and ActiveSheet might not help either.

For example here both Cells are unqualified:
Code:
ActiveSheet.Range(Cells(6, z), Cells(10, z)).Select

This should work, at least it shouldn't error anyway:
Code:
With ActiveSheet
      .Range(.Cells(6, z), .Cells(10, z)).Select
End With

As well as that the rest of the code really needs to be looked at, all that Select stuff just isn't a good idea.

I was going to have a quick go at redoing some of it but after a good look decided not to - I couldn't follow what was happening.:)
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top