Avatar billede janvogt Praktikant
23. april 2004 - 14:59 Der er 2 kommentarer og
1 løsning

VBA farvelægge markeret celle - fortsat

I dette spørgsmål http://www.eksperten.dk/spm/489877 får jeg en kode, som farvelægger den aktive celle samt den tilsvarende i kolonne B, for 2 givne områder.

Imidlertid konflikter denne kode med Custom Views og når jeg skjuler/viser kolonner.

Det sker ikke, hvis man bruger denne kode i stedet, som anvender betinget formatering til samme formål.

Koden highlighter hele kolonnen og rækken indtil cellen.
Hvordan skal koden til denne metode se ud, hvis jeg skal løse mit oprindelige problem - altså kun at highlighte den aktive celle samt den tilsvarende i kolonne B og kun for områderne D7:I16 og K7:L16.

Håber nogen har mod på opgaven.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim iColor As Integer
'// Amended routine found on this Web site
'// Note: Don't use IF you have Conditional
'// formating that you want to keep!

'// On error resume in case
'// user selects a range of cells
On Error Resume Next
iColor = Target.Interior.ColorIndex
'Leave On Error ON for Row offset errors

If iColor < 0 Then
    iColor = 36
Else
    iColor = iColor + 1
End If

'// Need this test incase Font color is the same
If iColor = Target.Font.ColorIndex Then iColor = iColor + 1

Cells.FormatConditions.Delete

'// Horizontal color banding
With Range("A" & Target.Row, Target.Address) 'Rows(Target.Row)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

'// Vertical color banding
With Range(Target.Offset(1 - Target.Row, 0).Address & ":" & Target.Offset(-1, 0).Address) 'Rows(Target.Row)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

End Sub
Avatar billede janvogt Praktikant
23. april 2004 - 16:31 #1
Denne kode virker, men hvordan får jeg den begrænset til kun at virke i områderne D7:I16 og K7:L16?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim iColor As Integer
On Error Resume Next
iColor = Target.Interior.ColorIndex

If iColor < 0 Then
    iColor = 36
Else
    iColor = iColor + 1
End If

If iColor = Target.Font.ColorIndex Then iColor = iColor + 1

Cells.FormatConditions.Delete

' Farvelæg B-kolonne
With Range("B" & Target.Row)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

' Farvelæg aktiv celle
With Range(Target.Address)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

End Sub
Avatar billede janvogt Praktikant
23. april 2004 - 16:43 #2
Tjah, så fik selv løst den :-)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim iColor As Integer
On Error Resume Next
iColor = Target.Interior.ColorIndex

If iColor < 0 Then
    iColor = 44
Else
    iColor = iColor + 1
End If

If iColor = Target.Font.ColorIndex Then iColor = iColor + 1

Cells.FormatConditions.Delete

On Error GoTo 0
Set rng = Union(Range("D7:I16"), Range("K7:L16"))

If Not Intersect(Target, rng) Is Nothing Then

' Farvelæg B-kolonne
With Range("B" & Target.Row)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

' Farvelæg aktiv celle
With Range(Target.Address)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

End If

End Sub
Avatar billede kabbak Professor
23. april 2004 - 16:43 #3
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("D7:I16,K7:L16")) Is Nothing Then
Dim iColor As Integer
'On Error Resume Next
iColor = Target.Interior.ColorIndex

If iColor < 0 Then
    iColor = 36
Else
    iColor = iColor + 1
End If

If iColor = Target.Font.ColorIndex Then iColor = iColor + 1

Cells.FormatConditions.Delete

' Farvelæg B-kolonne
With Range("B" & Target.Row)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

' Farvelæg aktiv celle
With Range(Target.Address)
    .FormatConditions.Add Type:=2, Formula1:="TRUE"
    .FormatConditions(1).Interior.ColorIndex = iColor
End With

End If
End Sub
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Excel kurser for alle niveauer og behov – find det kursus, der passer til dig

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester