Условное форматирование - подсветка в VBA - PullRequest
0 голосов
/ 29 октября 2019

Я пытаюсь создать VBA, которая выделяет всю строку, если строка содержит слово «Новый».

Применяя диапазон условного форматирования, я пытаюсь поместить, возможно, A1 до AZ2000

причина, у меня также была проблема с настройкой диапазона как всего листа.

Я никогда не изучал VBA, поэтому я получаю информацию из интернета

Я написал это много, но это не работает,Насколько мне известно, это должно работать, но я не знаю, почему это не работает, это расстраивает, как, я исправляю одну, и возникает другая проблема ... LOL

Sub Highlighting()

  'Definining the variables:
  Dim rng As Range
  Dim condition1 As FormatCondition

 'Fixing/Setting the range on which conditional formatting is to be desired
  Set rng = ("A1, AZ2000")

  'To delete/clear any existing conditional formatting from the range
   ws.FormatConditions.delete

  'This is where I get Syntax error, it says "New" needs list separator
  Set condition1 = ws.FormatConditions.Add(xlConditionValueFormula, xlGreater, "=FIND(""New"",$AF1)>0)")

  'Defining and setting the format to be applied for each condition
   With condition1
    .EntireRow.Interior.ColorIndex = 10498160
   End With

End Sub

enter image description here

вот как я хочу настроить его в VBA

1 Ответ

0 голосов
/ 29 октября 2019

Это должно сработать.

Я не знаю, к какой книге вы применяете это, поэтому я дал вам несколько вариантов: Снимите ' до set wb или sheetID в зависимости от того, какие вы 'идти (и добавить его к другим). Если вам интересны более распространенные советы и рекомендации по кодированию с помощью vba , ознакомьтесь с этим ответом.

Sub Highlighting()

'Definining the variables:
Dim rng As String
Dim condition1 As FormatCondition
Dim wb As Workbook
Dim sheetID As String

'Define workbook to run code against, depending on which fits you comment out/in here:
Set wb = ThisWorkbook ' the excel workbook that contains the vba code
'set wb = Workbooks("nameOfExcelWorkbook.xlsx") ' excel workbook that you've defined (must be an open workbook with the name.file ending inside citation marks)
'set wb = ActiveWorkbook ' the currently selected excel workbook (not recommended)
'Set wb = Workbooks.Open("Filename as string.fileEnding")

'Define what sheet to use
sheetID = ActiveSheet.Name 'currently selected worksheet name (not recommended)
'sheetID = "nameOfSheet"
'sheetID = 1 ' indexed version of above

'Fixing/Setting the range on which conditional formatting is to be desired
rng = "A1:AZ2000"


With wb.Sheets(sheetID).Range(rng)

    'To delete/clear any existing conditional formatting from the range
    .FormatConditions.Delete

    'Apply conditional formating
    Set condition1 = .FormatConditions.Add(Type:=xlExpression, Formula1:="=FIND(""New"",$AF1)>0")

End With
'Defining and setting the format to be applied for each condition
With condition1
    .Interior.Color = RGB(112, 48, 160)
End With

End Sub
...