2017-04-13 1 views
0

J'ai un code qui fonctionne quand une cellule change dans la colonne B.Ajout d'une bordure à la dernière ligne utilisée via VBA?

code:

'Insert Depot Memo Data for user 
Dim oCell As Range, targetCell As Range 
    Dim ws2 As Worksheet 

    If Not Intersect(Target, Range("B:B")) Is Nothing Then ' <-- run this code only if a value in column B has changed 
     If ActiveCell.Value <> "" Then 
     If Not GetWb("Depot Memo", ws2) Then Exit Sub 

     With ws2 
      For Each targetCell In Target 
       Set oCell = .Range("J1", .Cells(.Rows.Count, "J").End(xlUp)).Find(What:=targetCell.Value, LookIn:=xlValues, LookAt:=xlWhole) 
       If ActiveCell.Value <> "" Then 
       If Not oCell Is Nothing Then 
        Application.EnableEvents = False 

        'Set Format of cell 
        targetCell.ClearFormats 
        targetCell.Font.Name = "Arial" 
        targetCell.Font.Size = "10" 
        targetCell.Font.Color = RGB(128, 128, 128) 
        targetCell.HorizontalAlignment = xlCenter 
        targetCell.VerticalAlignment = xlCenter 
        targetCell.Borders(xlEdgeBottom).LineStyle = xlContinuous 
        targetCell.Borders(xlEdgeTop).LineStyle = xlContinuous 
        targetCell.Borders.Color = RGB(166, 166, 166) 
        targetCell.Borders.Weight = xlThin 

        targetCell.Offset(0, -1).Value = Now() 
        targetCell.Offset(0, 1).Value = oCell.Offset(0, 1) 
        targetCell.Offset(0, 2).Value = oCell.Offset(0, -2) 
        targetCell.Offset(0, 3).Value = oCell.Offset(0, -7) 

        With Range("A9:P1048576") 
        .FormatConditions.Delete 
        .FormatConditions.Add Type:=xlExpression, Formula1:="=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))" 

        With .FormatConditions(1).Borders 
        .LineStyle = xlContinuous 
        .Color = vbRed 
        .Weight = xlThin 
        End With 


        End With 


        Application.EnableEvents = True 
       End If 
       End If 
      Next 
     End With 
    End If 
    End If 

La plupart du temps le code fonctionne très bien. Sauf que je veux ajouter une mise en forme conditionnelle à une plage lorsque la cellule a changé.

Cela fonctionne également et ajoute une bordure rouge autour de la prochaine disponible (ligne vide). Cependant, je ne veux pas de bordure rouge autour de la ligne entière. Je veux juste des bordures supérieures et inférieures.

donc je suis en train de le faire:

With .FormatConditions(1).Borders(xlEdgeTop) 
         .LineStyle = xlContinuous 
         .Color = vbRed 
         .Weight = xlThin 
         End With 

Mais mon code cesse de fonctionner et je reçois cette erreur

enter image description here

S'il vous plaît quelqu'un peut me montrer où je me trompe?

Répondre

0

ici, il est ce que votre code doit

With Range("A9:P1048576") 
        .FormatConditions.Delete 
        .FormatConditions.Add Type:=xlExpression,Formula1:="=ROW(B9)=ROW(OFFSET($B$9,COUNTA($B:$B)-2,0))" 
        .Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous 
        .Borders.Color = ThisWorkbook.Colors(3) 
    end with 
0

Avez-vous essayé de définir explicitement les autres bordures?

.Borders(xlDiagonalDown).LineStyle = xlNone 
.Borders(xlDiagonalUp).LineStyle = xlNone 
.Borders(xlEdgeLeft).LineStyle = xlNone 
    With Selection.Borders(xlEdgeTop) 
     .LineStyle = xlContinuous 
     .ColorIndex = vbRed 
     .TintAndShade = 0 
     .Weight = xlThin 
    End With 
+0

merci, mais n'a pas résolu ce – user7415328

0

Essayez ceci. il devrait colorer seulement le haut et le bas d'une gamme de cellules. Vous pouvez l'adapter pour la rangée, si vous avez besoin d'aide faites le moi savoir.

Dim rRng As Range 
Set rRng = Sheet1.Range("Z15:Z50000") 
rRng.Select 
rRng.Borders(xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous 
+0

@lonut merci, mais n'a pas résolu ce – user7415328

+0

quelle est la plage que vous voulez colorer les frontières? – Ionut

+0

.FormatConditions (1) – user7415328