Advertisement
Guest User

Code für Modul1

a guest
Oct 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Code fuer Modul1
  2.  
  3. Option Explicit
  4.  
  5. Private CurrentSheetText As String
  6.  
  7.  
  8. Public Function StartsWith(LongText As String, ShortText As String, Optional IgnoreCase As Boolean = False) As Boolean
  9.   If IgnoreCase Then
  10.     StartsWith = (UCase$(Left$(LongText, Len(ShortText))) = UCase$(ShortText))
  11.   Else
  12.     StartsWith = (Left$(LongText, Len(ShortText)) = ShortText)
  13.   End If
  14. End Function
  15.  
  16. Public Function EndsWith(LongText As String, ShortText As String, Optional IgnoreCase As Boolean = False) As Boolean
  17.   If IgnoreCase Then
  18.     EndsWith = (UCase$(Right$(LongText, Len(ShortText))) = UCase$(ShortText))
  19.   Else
  20.     EndsWith = (Right$(LongText, Len(ShortText)) = ShortText)
  21.   End If
  22. End Function
  23.  
  24.  
  25. Private Function SheetText(ByVal Wb As Workbook, ByVal Sh As Object) As String
  26.   Dim NumberOfSheet As Integer, NumberOfSheets As Integer
  27.  
  28.   NumberOfSheets = Wb.Sheets.Count
  29.  
  30.   If Sh Is Nothing Then
  31.     NumberOfSheet = 0
  32.   Else
  33.     Debug.Assert Wb Is Sh.Parent
  34.     NumberOfSheet = Sh.Index
  35.   End If
  36.  
  37.   SheetText = SheetTextImplementation(NumberOfSheet, NumberOfSheets, Sh)
  38.  
  39. End Function
  40.  
  41. Private Function SheetTextImplementation(ByVal NumberOfSheet As Integer, ByVal NumberOfSheets As Integer, ByVal Sh As Object) As String
  42.   Dim SheetTypeName As String, SheetTypeNotion As String, SheetName As String
  43.   Dim NumberOfSheetOfType As Integer, NumberOfSheetsOfType As Integer
  44.   Dim testSheet As Object, testSheetIndex As Integer
  45.  
  46.   If NumberOfSheet <= 0 Then
  47.     SheetTextImplementation = "<kein Blatt gewählt von " + Format(NumberOfSheets) + ">"
  48.     SheetTypeName = ""
  49.   Else
  50.     SheetTypeName = TypeName(Sh)
  51.    
  52.     SheetName = "<Name nicht verfügbar>"
  53.     On Error Resume Next
  54.       SheetName = Sh.Name
  55.     On Error GoTo 0
  56.    
  57.     Select Case SheetTypeName
  58.       Case ""
  59.         SheetTypeNotion = "<unbekannt>"
  60.       Case "Worksheet"
  61.         SheetTypeNotion = "Tabelle"
  62.       Case "Chart"
  63.         SheetTypeNotion = "Diagramm"
  64.       Case "DialogSheet"
  65.         SheetTypeNotion = "Dialogblatt"
  66.       Case "Application" 'Dummy-Wert, um unbekannten Statusbar-Inhalt zu signalisieren
  67.        SheetTypeNotion = "*"
  68.       Case Else
  69.         Stop
  70.         SheetTypeNotion = SheetTypeName
  71.     End Select 'SheetTypeName
  72.    
  73.     NumberOfSheetsOfType = 0
  74.     testSheetIndex = 0: NumberOfSheetOfType = 0
  75.     For Each testSheet In Sh.Parent.Sheets
  76.       If TypeName(testSheet) = SheetTypeName Then
  77.         NumberOfSheetsOfType = NumberOfSheetsOfType + 1
  78.         testSheetIndex = testSheetIndex + 1
  79.         If Sh.Index = testSheet.Index Then 'Sh Is testSheet Then -- funktioniert nicht zuverlässig, warum auch immer
  80.          Debug.Assert NumberOfSheetOfType = 0
  81.           NumberOfSheetOfType = testSheetIndex
  82.         End If
  83.       End If
  84.     Next 'Each testsheet In Sh.Parent.Sheets
  85.    
  86.     SheetTextImplementation = "Blatt " + Format(NumberOfSheet) + " von " + Format(NumberOfSheets) + " (" + SheetTypeNotion + " " + Format(NumberOfSheetOfType) + " von " + Format(NumberOfSheetsOfType) + " """ + SheetName + """)"
  87.   End If
  88.  
  89. End Function
  90.  
  91. Public Sub AddSheetName(ByVal Wb As Workbook, ByVal Sh As Object)
  92.  
  93.   CurrentSheetText = " " + SheetText(Wb, Sh)
  94.  
  95.   If VarType(Application.StatusBar) <> vbString Then
  96.     Application.StatusBar = CurrentSheetText
  97.   Else
  98.     If EndsWith(Application.StatusBar, CurrentSheetText) Then
  99.       'do nothing
  100.    Else
  101.       Application.StatusBar = Application.StatusBar + CurrentSheetText
  102.     End If
  103.   End If
  104.  
  105. End Sub
  106.  
  107. Public Sub RemoveSheetName(ByVal Wb As Workbook, ByVal Sh As Object)
  108.   Const NumberOfSheetsDummy As Long = 32767, NumberOfSheetDummy As Long = 32766
  109.  
  110.   'Ermittle tatsächlichen Text, auch wenn das Makro neu gestartet wurde und der StatusBar-Text verloren gegangen ist
  111.  If CurrentSheetText = "" Then
  112.     Dim cShT As String
  113.     Dim cShTPattern As String, cShTSBPattern As String, cShTPatternEmptied As String
  114.     Dim statusBarMatches As Boolean
  115.     Dim statusBarText As Variant
  116.    
  117.     statusBarText = Application.StatusBar
  118.     cShT = ""
  119.     cShTPattern = ""
  120.     cShTSBPattern = ""
  121.     cShTPatternEmptied = ""
  122.    
  123.     statusBarMatches = (VarType(statusBarText) <> vbString)
  124.    
  125.     If Not statusBarMatches Then
  126.       statusBarMatches = (statusBarText = "")
  127.     End If
  128.    
  129.     If Not statusBarMatches Then
  130.       cShT = SheetTextImplementation(NumberOfSheetDummy, NumberOfSheetsDummy, Application)
  131.       cShTPattern = Replace(Replace(cShT, Format(NumberOfSheetsDummy), "*"), Format(NumberOfSheetDummy), "*")
  132.       cShTSBPattern = "*" + cShTPattern
  133.       statusBarMatches = (statusBarText Like cShTSBPattern)
  134.     End If
  135.    
  136.     If Not statusBarMatches Then
  137.       cShT = SheetTextImplementation(0, NumberOfSheetsDummy, Application)
  138.       cShTSBPattern = "*" + cShTPattern
  139.       cShTPattern = Replace(Replace(cShT, Format(NumberOfSheetsDummy), "*"), Format(NumberOfSheetDummy), "*")
  140.       statusBarMatches = (statusBarText Like cShTSBPattern)
  141.     End If
  142.    
  143.     If statusBarMatches Then
  144.       'Rückwärts suchen bis Übereinstimmung gefunden
  145.      cShTPatternEmptied = Replace(cShTPattern, "*", "") 'String für die Länge, ab der gesucht wird - "?" braucht nicht ersetzt zu werden, da es genau 1 Zeichen ist und auch für genau 1 Zeichen steht
  146.      
  147.       Dim pos As Long, matchAtPos As Boolean
  148.      
  149.       pos = Len(statusBarText) - Len(cShTPatternEmptied)
  150.       matchAtPos = False
  151.      
  152.       Do While pos > 0
  153.         If Mid$(statusBarText, pos) Like cShTPattern Then
  154.           matchAtPos = True
  155.           Exit Do
  156.         End If
  157.         pos = pos - 1
  158.       Loop
  159.      
  160.       If matchAtPos Then
  161.         CurrentSheetText = Mid$(statusBarText, pos)
  162.       End If
  163.     End If
  164.   End If
  165.  
  166.   If VarType(Application.StatusBar) <> vbString Then 'Application.StatusBar == False (Boolean), wenn in der StatusBar nichts angezeigt werden soll
  167.    Application.StatusBar = ""
  168.   Else
  169.     If EndsWith(Application.StatusBar, CurrentSheetText) Then
  170.       'den Blatt-spezifischen Text entfernen, den Anfang stehenlassen
  171.      Application.StatusBar = Left(Application.StatusBar, Len(Application.StatusBar) - Len(CurrentSheetText))
  172.     Else
  173.       'do nothing
  174.    End If
  175.   End If
  176.  
  177. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement