danger355

AutoMaps (sheet1)

Jun 17th, 2020
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' (C) https://www.reddit.com/user/danger355
  2. ' This work is licensed under a Creative Commons Attribution 4.0 International License
  3.  
  4. Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
  5.  
  6. 'Check if Custom Property(ies) exist, add to worksheet properties if no
  7. On Error Resume Next
  8. ActiveWorkbook.CustomDocumentProperties.Add Name:="Location elev.", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=10
  9. ActiveWorkbook.CustomDocumentProperties.Add Name:="Vicinity elev.", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=16
  10. ActiveWorkbook.CustomDocumentProperties.Add Name:="Images needed", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=2
  11. ActiveWorkbook.CustomDocumentProperties.Add Name:="First check", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=0
  12. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 1 Name", LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Location"
  13. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 2 Name", LinkToContent:=False, Type:=msoPropertyTypeString, Value:="Vicinity"
  14. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 1 Width", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=600
  15. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 1 Height", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=480
  16. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 2 Width", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=600
  17. ActiveWorkbook.CustomDocumentProperties.Add Name:="Image 2 Height", LinkToContent:=False, Type:=msoPropertyTypeNumber, Value:=480
  18. bolExistsLocElev = (Err = 0)
  19. On Error GoTo 0
  20.  
  21. 'Notification of named cells requirement (only run once)
  22. If ActiveWorkbook.CustomDocumentProperties("First check").Value = 0 Then
  23.     ActiveCell.Offset(0, 1).Select
  24.     MsgBox ("This spreadsheet has macros that" & vbNewLine _
  25.     & "require the use of named cells to" & vbNewLine _
  26.     & "automatically generate map images." & vbNewLine _
  27.     & vbNewLine _
  28.     & "Please name the appropriate cells:" & vbNewLine _
  29.     & vbNewLine _
  30.     & "   " & Chr(34) & "starter" & Chr(34) & "  " & Chr(34) & "lat" & Chr(34) & "  " & Chr(38) & "  " & Chr(34) & "long" & Chr(34) & vbNewLine _
  31.     & vbNewLine _
  32.     & " (verbatim & case sensitive)"), , "First Run"
  33.     ActiveWorkbook.CustomDocumentProperties("First check").Value = 1
  34. Exit Sub
  35. End If
  36.  
  37. 'Make sure only 1 or 2 images are needed in Custom Property
  38. If ActiveWorkbook.CustomDocumentProperties("Images needed").Value > 2 Then
  39.     MsgBox "Images Needed quantity in " & vbNewLine & "Custom Properties must be " & vbNewLine & Chr(34) & "1" & Chr(34) & " or " & Chr(34) & "2" & Chr(34) & "only.", , "Oops..."
  40.     ActiveCell.Offset(0, 1).Select
  41. Exit Sub
  42. End If
  43.  
  44. 'Check if Starter Cell exists
  45. On Error Resume Next
  46. Set rCellCheck = Nothing
  47. Set rCellCheck = Range("starter")
  48. On Error GoTo 0
  49. If Not rCellCheck Is Nothing Then
  50. 'If Starter Cell does exist, and it's double-clicked...
  51. If Target.Address = Range("starter").Address Then
  52. 'If the cell for Latitude is invalid (no Latitude), then the program will not run
  53. 'Instead, an error message will be displayed
  54.  
  55. 'Check if Lat and Long Cells exist, exit if not
  56.    On Error Resume Next
  57.     Set rCellCheck = Nothing
  58.     Set rCellCheck = Range("lat")
  59.     On Error GoTo 0
  60.     If rCellCheck Is Nothing Then
  61.         ActiveCell.Offset(0, 1).Select
  62.         MsgBox ("named cell " & Chr(34) & "lat" & Chr(34) & " does not exist."), , "Oops..."
  63.         Exit Sub
  64.     End If
  65.    
  66.     On Error Resume Next
  67.     Set rCellCheck = Nothing
  68.     Set rCellCheck = Range("long")
  69.     On Error GoTo 0
  70.     If rCellCheck Is Nothing Then
  71.         ActiveCell.Offset(0, 1).Select
  72.         MsgBox ("named cell " & Chr(34) & "long" & Chr(34) & " does not exist."), , "Oops..."
  73.         Exit Sub
  74.     End If
  75.    
  76. 'Check if Lat and Long Cells have error
  77.    If IsNumeric(Range("lat")) = False Or IsNumeric(Range("long")) = False Then
  78.         ActiveCell.Offset(0, 1).Select
  79.         MsgBox ("GPS Coordinates not found." & vbNewLine _
  80.         & vbNewLine _
  81.         & "(Wrong FA Code?)"), , "Oops..."
  82.         Exit Sub
  83.     End If
  84. 'Always centers the dialogue box on the open spreadsheet
  85.    With UserForm1
  86.         .StartUpPosition = 0
  87.         .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
  88.         .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
  89.         .LocationBox.Caption = ActiveWorkbook.CustomDocumentProperties("Image 1 Name").Value & " Map"
  90.         .VicinityBox.Caption = ActiveWorkbook.CustomDocumentProperties("Image 2 Name").Value & " Map"
  91.         .Show
  92.     End With
  93.     ActiveCell.Offset(0, 1).Select
  94. End If
  95. End If
  96. End Sub
Add Comment
Please, Sign In to add comment