Error 91 - Defining variables

kgardner

New Member
Joined
Sep 3, 2015
Messages
22
Hello and thank you for your time. I have created a report that is shared with clients that has a private sub for when the workbook opens. One of my clients is getting an issue with the bold line of code. Here is some basic information:

  • I wrote the code/created the workbook in Excel 2016
  • Client is having issue using Excel 2013
  • When I tested the workbook on Excel 2013, I cannot repeat the issue
  • The code is stored in the workbook

What am I missing here?


Code:
Private Sub Workbook_Open()

Dim ws As Worksheet
Dim nm As Range
Dim buffer As Range


[B]Set ws = ActiveWorkbook.Worksheets("Site_Report")[/B]
Set nm = ws.Range("Site_Analysis_Selected_Site")
Set buffer = ws.Range("Site_Buffer_Selected")


nm.Activate
ActiveCell.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Loc_Name,1)),"""")"
    
buffer.Activate
ActiveCell.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Unique_Buffer,1)),"""")"
    Range("Q4").Select




End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Just a guess, but if it takes a long time for the workbook to open on your client's computer, the ActiveWorkbook may be the file that was already open. Have you tried changing that line of code to Set ws = ThisWorkbook.Sheets("Site_Report")
 
Upvote 0
I had not thought about making that change, but I could see how the ActiveWorkbook could be connecting to another file. Thank you, Neil.
 
Upvote 0
I made changes and client just pinged me back and said the bold line of code is having issues, error code 1004. The Site_Report sheet is the default sheet on open, do I need to define which sheet I'm selecting before I use the code "nm.Activate" ?

Code:
Private Sub Workbook_Open()

Dim ws As Worksheet
Dim nm As Range
Dim buffer As Range


Set ws = ThisWorkbook.Sheets("Site_Report")
Set nm = ws.Range("Site_Analysis_Selected_Site")
Set buffer = ws.Range("Site_Buffer_Selected")

[B]nm.Activate[/B]
ActiveCell.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Loc_Name,1)),"""")"
    
buffer.Activate
ActiveCell.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Unique_Buffer,1)),"""")"
    Range("Q4").Select




End Sub
 
Upvote 0
You can just say:

Code:
nm.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Loc_Name,1)),"""")"
 
Upvote 0
You can just say:

Code:
nm.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Loc_Name,1)),"""")"

Steve - that is what I ended up doing, but the client continues to have 1004 issues on other lines of code. I went back through all of the code and defined all my variables under option explicit. I'm hoping this solves the issue.
 
Upvote 0
You either have to avoid using select/ activate which is rarely required or you need to select the correct sheet before the offending lines of code.
 
Upvote 0
So this problem is persisting. But only for this one client. All of my other clients and all of my coworkers are not experiencing any similar issues.
It seems that no matter what happens, this one user receives a 1004 error. To give some context, on open - this code places two formulas into two named range, which then calls a series of subs that refresh a google maps API that displays in the worksheet.


Below is the full amount of code, the bold line contains the issue.


Code:
Private Sub Workbook_Open()

Dim ws As Worksheet
Dim nm As Range
Dim buffer As Range


Set ws = ThisWorkbook.Sheets("Site_Report")
Set nm = ws.Range("Site_Analysis_Selected_Site")
Set buffer = ws.Range("Site_Buffer_Selected")


' nm.Activate 'previously caused error 1004 but no longer in use 
nm.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Loc_Name,1)),"""")"
    
' buffer.Activate
buffer.FormulaR1C1 = "=IFERROR(PROPER(INDEX(DE_Unique_Buffer,1)),"""")"
    Range("Q4").Select




End Sub
'change picture without change image size
Sub change_picture()


Dim Loc As String
Dim i, pfile, strpic, shp, t, l, h, w, shp2 As Variant


Loc = "http://maps.googleapis.com/maps/api/staticmap?scale=2&autoscale=1&size=640x380&maptype=roadmap&format=png&visual_refresh=true&" + Cells(45, 1).Value


For i = 49 To 123
If Cells(i, 1).Value <> "" Then
   Loc = Loc + Cells(i, 1).Value
   End If
Next i
shortenergoogle (Loc)
pfile = DownloadFile(Loc)
If pfile <> "Error" Then
strpic = "MapImage"
Set shp = ActiveSheet.Shapes(strpic)
With shp
    t = .Top
    l = .Left
    h = .Height
    w = .Width
End With


    
    DoEvents
    Set shp2 = ActiveSheet.Shapes.AddPicture(pfile, msoFalse, msoTrue, l, t, w, h)
    shp.Delete


    shp2.Name = strpic
End If






End Sub


Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim keycells As Range
Dim mapcells As Range
Dim ws As Worksheet


Set ws = ThisWorkbook.Sheets("Site_Report")


    ' The variable KeyCells contains the cells that will
    ' cause an alert when they are changed.
    Set keycells = ws.Range("Q2")
[B]    If Not Application.Intersect(keycells, Range(Target.Address)) _[/B]
[B]           Is Nothing Then [/B]'this is the line of code causing the current 1004 error 
Call change_picture
 
    End If
    
    Set mapcells = ws.Range("Q3")
    If Not Application.Intersect(mapcells, Range(Target.Address)) _
           Is Nothing Then
Call change_picture
 
    End If
    
End Sub


Sub shortenergoogle(ByVal url As String)
    Dim Token, EndAPI, InText, FimText, JSONString As String
    Token = "AIzaSyA5ha9R1tQRZpjzrIhRFeoQGcT7GwPX2c0"
    Dim HttpReq  As New WinHttpRequest
    Dim response As String
    Dim result As Variant
    
    EndAPI = "https://www.googleapis.com/urlshortener/v1/url?key=" + Token


    On Error Resume Next 'This is to avoid errors on invalid URLs
    JSONString = "{""longUrl"": """ + url + """}"
    With HttpReq
        .Open "POST", EndAPI, False
        .SetRequestHeader "Content-Type", "application/json"
        .Send JSONString
    End With
     
    response = HttpReq.ResponseText
    HttpReq.WaitForResponse
    
    result = Left(Mid(response, 40), InStr(response, "longUrl") - InStr(response, "https") - 5)
    Cells(12, 3).Value = result


     
    'Set shp = ActiveSheet.Shapes.AddPicture(result, msoFalse, msoTrue, l, t, w, h)
    'ActiveSheet.Shapes(strPic).Visible = True
    'ActiveSheet.Shapes(strPic).Delete
    
    'ActiveSheet.Shapes(strPic).Visible = False
End Sub




Function DownloadFile(ByVal myurl As String) As String
Dim url, str_tmppath, ostream, pfile As Variant




'myURL = "https://YourWebSite.com/?your_query_parameters"
url = myurl
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myurl, False, "username", "password"
WinHttpReq.Send




str_tmppath = Environ("temp")


pfile = str_tmppath + "\file.png"
myurl = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
    Set ostream = CreateObject("ADODB.Stream")
    ostream.Open
    ostream.Type = 1
    ostream.Write WinHttpReq.responseBody
    ostream.SaveToFile pfile, 2 ' 1 = no overwrite, 2 = overwrite
    ostream.Close
  DownloadFile = pfile
Else
DownloadFile = "Error"
End If


End Function
 
Upvote 0
Its because they are on a different sheet. Remove the workbook event and replace it with a worksheet event such as:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim keycells As Range
Set keycells = Range("Q2")
If Not Application.Intersect(keycells, Target) Is Nothing Then
    MsgBox "We have changed Q2"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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