• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

Opgelost Bereik van een VBA code

Dit topic is als opgelost gemarkeerd
Status
Niet open voor verdere reacties.

jjmermen

Gebruiker
Lid geworden
23 okt 2012
Berichten
57
Besturingssysteem
Windows
Office versie
365
In deze VBA code wil ik graag wil dat de code alleen in een bepaald bereik werkt. Bijvoorbeeld in het bereik B7:AV500. Is dat mogelijk?
In de rijen 1 t/m 5 heb ik namelijk opmaak zitten die ik niet kwijt wil. De VBA code die ik nu heb maakt alle bestaande opmaak leeg.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target
' Highlight the entire row that contain the active cell
.EntireRow.Interior.ColorIndex = 19
' Highlight the active cell
Target.Interior.ColorIndex = 6
End With
Application.ScreenUpdating = True
End Sub
 
@emields : Slordig van me. In de bijlage ook een voorbeeld bestandje, waarbij ik graag wil dat VBA het alleen in het bereik van B7:D16 werkt.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
Application.ScreenUpdating = False
' Clear the color of all the cells
Cells.Interior.ColorIndex = 0
With Target
' Highlight the entire row that contain the active cell
.EntireRow.Interior.ColorIndex = 19
' Highlight the active cell
Target.Interior.ColorIndex = 6
End With
Application.ScreenUpdating = True
End Sub
 

Bijlagen

Dus je hebt Application.Intersect al bekeken?
 
Klopt, ik heb Application.Intersect al bekeken. Mijn ervaring is niet zo dat ik zelf een VBA kan aanpassen.
De VBA die ik meegestuurd heb, heb ik van het internet gehaald.
Ik zou er graag wat hulp bij kunnen gebruiken.
 
Kijk eens naar dit:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Range(Target.Address), Range("B7:D16")) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Application.ScreenUpdating = False
        ' Clear the color of all the cells
        Cells.Interior.ColorIndex = 0
        With Target
            ' Highlight the entire row that contain the active cell
            .EntireRow.Interior.ColorIndex = 19
            ' Highlight the active cell
            Target.Interior.ColorIndex = 6
        End With
        Application.ScreenUpdating = True
    End If
End Sub
 
Oeps, ik heb te vroeg gejuicht. De opmaak buiten dit bereik wordt nog gewist.
Is dat ook aan te passen?
 

Bijlagen

Dan zo:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Application.Intersect(Range(Target.Address), Range("B7:D16")) Is Nothing Then
        If Target.Cells.Count > 1 Then Exit Sub
        Application.ScreenUpdating = False
        Range("B7:D16").Interior.ColorIndex = 0
        Range("B" & Target.Row).Resize(, 3).Interior.ColorIndex = 19
        Target.Interior.ColorIndex = 6
        Application.ScreenUpdating = True
    End If
End Sub
 
Of maak er een tabel van:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Me.ListObjects("Tabel1").DataBodyRange) Is Nothing Then
        Range("Tabel1[[Artikelcode]:[Merk]]").Interior.ColorIndex = 0
        rij = Target.Row - Me.ListObjects("Tabel1").Range.Row
        Range("Tabel1[[Artikelcode]:[Merk]]").Rows(rij).Interior.ColorIndex = 19
        Target.Interior.ColorIndex = 6
    End If
End Sub
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan