macro not running correctly

steve400243

Active Member
Joined
Sep 15, 2016
Messages
429
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello, This macro "getuniques" runs a new report if a user selects yes in the message box when accessing the file. If the report is opened and a new one ran and it is on Sheet1, it gets a run-time error 1004, Application defined or subject defined error, and the "Sheets("ANALYSIS") row of code Highlights? If the user is on the ANALYSIS sheet and selects YES, it works fine? Anyone know why this would happen? And what can correct this?

Code:
Private Sub Workbook_Open()
    If MsgBox("Hello, Would you like to Create a new report?", vbYesNo, "CFS DATA ANALYSIS REPORT") = vbYes Then
        Call t
        Call getuniques
    End If
End Sub

VBA Code:
Sub getuniques()
   Dim d As Object, c As Variant, i As Long, lr As Long
   Set d = CreateObject("Scripting.Dictionary")
   lr = Cells(Rows.Count, 1).End(xlUp).Row
   c = Sheets("Sheet1").Range("A2:A1000" & lr).Value2
   [COLOR=rgb(184, 49, 47)]Sheets("ANALYSIS").Range("U3", Range("U" & Rows.Count).End(xlUp)).ClearContents[/COLOR]
   For i = 1 To UBound(c, 1)
     If c(i, 1) <> "" Then d(c(i, 1)) = Empty
   Next i
   Sheets("ANALYSIS").Range("U3").Resize(d.Count) = Application.Transpose(d.Keys)
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
The second instance of Range is not qualified with a sheet name, so it defaults to the active sheet (sheet1), this should fix it.

VBA Code:
Sheets("ANALYSIS").Range("U3", Sheets("ANALYSIS").Range("U" & Rows.Count).End(xlUp)).ClearContents
 
Upvote 0
The second instance of Range is not qualified with a sheet name, so it defaults to the active sheet (sheet1), this should fix it.

VBA Code:
Sheets("ANALYSIS").Range("U3", Sheets("ANALYSIS").Range("U" & Rows.Count).End(xlUp)).ClearContents

Excellent, Thanks for your help!
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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