Anzeige
Archiv - Navigation
1364to1368
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Bitte um Hilfe bei Änderung eines Makros (Zellbez)

Bitte um Hilfe bei Änderung eines Makros (Zellbez)
04.06.2014 13:08:15
Jenny
Hallo alle zusammen,
habe untenstehendes, früher mal funktionierendes Makro.
Problem ist jetzt, ich habe in der Tabelle Hyperlinks noch ein paar Spalten eingefügt und bin jetzt überfragt, wie ich das Makro ändern muss damit es wieder funktioniert.
Irgendwo müssten in dem Makro zwei Spalten angegeben sein, in denen jeweils ein Wort steht, das dann mit Spalte A der Tabelle2 verglichen werden sollen. Diese beiden Worte stehen jetzt in den Spalten D und E.
Dann werden ja weiter unten im Makro Hyperlinks in gleichnamige Tabelle geschrieben. Hier soll jetzt die erste Spalte, in die Hyperlinks geschrieben werden, die Spalte J sein.
Könnt ihr mir da bitte helfen?
Liebe Grüße
Jenny
Option Explicit
Public Sub SearchHyperlinks()
Dim lngRow As Long
Dim objCell As Range
Dim objRegEx As Object, objMatch As Object
Dim strSearchName As String, strFoundName As String
Dim strFirstAddress As String, strHyperlinkAddress As String
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Set objRegEx = CreateObject("VBScript.RegExp")
With Worksheets("Hyperlinks") 'gegebenenfalls anpassen !!!!!!!!!!!!!!
For lngRow = 1 To .Cells(.Rows.Count, 2).End(xlUp).Row
strSearchName = Trim$(.Cells(lngRow, 2).Value)
If strSearchName  vbNullString Then
With Worksheets("Tabelle2") 'gegebenenfalls anpassen !!!!!!!!!!!!!!
Set objCell = .Columns(1).Find(What:=strSearchName, _
after:=.Cells(.Rows.Count, 1), _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
End With
If Not objCell Is Nothing Then
strFirstAddress = objCell.Address
Do
If objCell.Hyperlinks.Count  0 Then
strHyperlinkAddress = Split(objCell.Hyperlinks.Item(1).Address, "?") _
(0)
strFoundName = Trim$(objCell.Value)
If strFoundName  strSearchName Then
With objRegEx
.IgnoreCase = True
.Pattern = "^" & strSearchName & " | " & strSearchName & "$" _
Set objMatch = .Execute(strFoundName)
End With
If objMatch.Count = 1 Then
If objMatch.Item(0).Value = strSearchName & " " Or _
objMatch.Item(0).Value = " " & strSearchName Then _
Call WriteLink(strHyperlinkAddress, lngRow)
End If
Else
Call WriteLink(strHyperlinkAddress, lngRow)
End If
End If
Set objCell = Worksheets("Tabelle4").Columns(1).FindNext(objCell) ' _
gegebenenfalls anpassen !!!!!!!!!!!!!!
Loop Until objCell.Address = strFirstAddress
End If
End If
Next
End With
Set objCell = Nothing
Set objMatch = Nothing
Set objRegEx = Nothing
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Private Sub WriteLink( _
ByVal pvstrHyperlinkAddress As String, _
ByVal pvlngRow As Long)
Dim lngColumn As Long
Dim blnFound As Boolean
With Worksheets("Hyperlinks") 'gegebenenfalls anpassen !!!!!!!!!!!!!!
For lngColumn = 3 To .Cells(pvlngRow, .Columns.Count).End(xlToLeft).Column
If .Cells(pvlngRow, lngColumn).Value = pvstrHyperlinkAddress Then
blnFound = True
Exit For
End If
Next
If Not blnFound Then
lngColumn = WorksheetFunction.Max(4, _
.Cells(pvlngRow, .Columns.Count).End(xlToLeft).Column + 1)
.Cells(pvlngRow, lngColumn).Value = pvstrHyperlinkAddress
End If
End With
End Sub

3
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
sorry im ersten Beitrag war der falsche Code
04.06.2014 13:10:06
Jenny

Option Explicit
Public Sub SearchHyperlinks()
Dim lngRow As Long
Dim objCell As Range
Dim objRegEx As Object, objMatch As Object
Dim strSearchName As String, strFoundName As String
Dim strFirstAddress As String, strHyperlinkAddress As String
Dim strSearch2 As String
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
Set objRegEx = CreateObject("VBScript.RegExp")
With Worksheets("Hyperlinks")
For lngRow = 1 To .Cells(.Rows.Count, 2).End(xlUp).row
strSearchName = Trim$(.Cells(lngRow, 4).Value)
strSearch2 = Trim$(.Cells(lngRow, 5).Value)
If strSearchName  vbNullString Then
With Worksheets("Tabelle4")
Set objCell = .Columns(1).Find(What:=strSearchName, _
After:=.Cells(.Rows.Count, 1), _
LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
End With
If Not objCell Is Nothing Then
strFirstAddress = objCell.Address
Do
If objCell.Hyperlinks.Count  0 _
And InStr(1, objCell.Text, strSearch2) > 0 Then
If InStr(objCell.Hyperlinks.Item(1).Address, "profile.php") Then
strHyperlinkAddress = Split(objCell.Hyperlinks.Item(1).Address, "&")(0)
Else
strHyperlinkAddress = Split(objCell.Hyperlinks.Item(1).Address, "?")(0)
End If
strFoundName = Trim$(objCell.Value)
If strFoundName  strSearchName Then
With objRegEx
.IgnoreCase = True
.Pattern = "^" & strSearchName & " | " &  _
strSearchName & "$" _
_
_
_
_
Set objMatch = .Execute(strFoundName)
End With
If objMatch.Count = 1 Then
If objMatch.Item(0).Value = strSearchName & " "   _
_
Or _
objMatch.Item(0).Value = " " & strSearchName  _
_
Then _
Call WriteLink(strHyperlinkAddress, lngRow)
End If
Else
Call WriteLink(strHyperlinkAddress, lngRow)
End If
End If
Set objCell = Worksheets("Tabelle4").Columns(1).FindNext( _
objCell) ' _
Loop Until objCell.Address = strFirstAddress
End If
End If
Next
End With
Set objCell = Nothing
Set objMatch = Nothing
Set objRegEx = Nothing
With Application
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
ActiveWorkbook.Save
Application.Quit
'Shell "shutdown.exe -s -f"
End Sub
Private Sub WriteLink( _
ByVal pvstrHyperlinkAddress As String, _
ByVal pvlngRow As Long)
Dim lngColumn As Long
Dim blnFound As Boolean
With Worksheets("Hyperlinks")
For lngColumn = 3 To .Cells(pvlngRow, .Columns.Count).End(xlToLeft).Column
If .Cells(pvlngRow, lngColumn).Value = pvstrHyperlinkAddress Then
blnFound = True
Exit For
End If
Next
If Not blnFound Then
lngColumn = WorksheetFunction.Max(4, _
.Cells(pvlngRow, .Columns.Count).End(xlToLeft).Column + 1)
.Cells(pvlngRow, lngColumn).Value = pvstrHyperlinkAddress
End If
End With
End Sub

Anzeige
Nur ein Hinweis, ...
05.06.2014 00:14:14
Luc:-?
…Jenny;
Konstanten, die sich irgendwann mal ändern könnten, deklariert man auch als solche am PgmAnfang (auch Adressen, wobei man bei letzteren auch Namen festlegen könnte und die dann - ggf auch als Konstante - im Pgm verwendet → deren AdressBezüge ändern sich nämlich normalerweise automatisch bei BlattÄnderung!), was spätere Änderungen wesentlich erleichtert, weil man nicht das ganze Pgm durchsuchen muss, was ich hier auch nicht für dich tue… ;-]
Gruß Luc :-?

AW: sorry im ersten Beitrag war der falsche Code
08.06.2014 10:47:34
fcs
Hallo Jenny,
ich hab jetzt mal versucht, die verschiedenen im Code verwendeten Spalten zu identifizieren und durch Konstanten ersetzt. Zusätzlich hab ich an der einen oder anderen Stelle die Struktur ein wenig bereinigt/verändert. Für die involvierten Tabellenblätter hab ich Objektnamen eingefügt. Das macht die Codepflege etwas einfacher.
Die Werte der Konstanten musst du ggf. an den aktuellen Aufbau der Tabellen anpassen.
Gruß
Franz
Textdatei mit angepasstem Code: https://www.herber.de/bbs/user/91040.txt
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige