Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果Sheet1中的单元格与Sheet2中的相同单元格不匹配,Excel vba将插入条件格式_Vba_Excel - Fatal编程技术网

如果Sheet1中的单元格与Sheet2中的相同单元格不匹配,Excel vba将插入条件格式

如果Sheet1中的单元格与Sheet2中的相同单元格不匹配,Excel vba将插入条件格式,vba,excel,Vba,Excel,我尝试对此进行编程,以便在创建保存为xlsx文件的工作簿时,工作簿中的vba将数据从副本条件格式复制到新工作簿的sheet1。我需要它有公式A1和公式2!A1则文本字体为红色。然后下一个单元格将是B1 Sheet2“B1文本字体为红色 我需要将其复制到活动范围。我尝试了此代码,但它对我无效。我花了2周时间试图找到此问题的答案,但毫无乐趣 Application.CutCopyMode =False Selection.FormatConditions.Add Type:=xlExpressio

我尝试对此进行编程,以便在创建保存为xlsx文件的工作簿时,工作簿中的vba将数据从副本条件格式复制到新工作簿的sheet1。我需要它有公式A1和公式2!A1则文本字体为红色。然后下一个单元格将是B1 Sheet2“B1文本字体为红色

我需要将其复制到活动范围。我尝试了此代码,但它对我无效。我花了2周时间试图找到此问题的答案,但毫无乐趣

Application.CutCopyMode =False

Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
    "=A1<>Sheet2!A1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority

With Selection.FormatConditions(1).Font
    .Color =-16776961
    .TintAndShade =0
EndWith

Selection.FormatConditions(1).StopIfTrue =False
Selection.Copy
Range("A1:S200").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
Application.CutCopyMode=False
Selection.FormatConditions.Add类型:=xlExpression,公式1:=_
“=A12!A1“
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
使用Selection.FormatConditions(1).Font
.Color=-16776961
.TintAndShade=0
结束
Selection.FormatConditions(1).StopIfTrue=False
选择,复制
范围(“A1:S200”)。选择
Selection.Paste特殊粘贴:=xlPasteFormats,操作:=xlNone_
SkipBlanks:=False,转置:=False

尝试用以下代码替换所有代码:

Dim newCondition As FormatCondition 'Red text if Sheet1 cell doesn't match Sheet2 cell.
Dim conditionFormula As String      'Formula for the format condition

' Compare each cell using indirect addressing of the current row and column. Result is
' true if the sheet cells don't match, false if they do.
conditionFormula = "=(INDIRECT(ADDRESS(ROW(),COLUMN(),1,1,""Sheet1"")) <>" & _
                   "  INDIRECT(ADDRESS(ROW(),COLUMN(),1,1,""Sheet2"")))"

' Create the condition and get a pointer to it.
Set newCondition = Range("A1:S200").FormatConditions.Add( _
                                    Type:=xlExpression, _
                                    Formula1:=conditionFormula)

' Add the formatting, priority, stop rule to the condition.
With newCondition
    .SetFirstPriority
    With .Font
        .Color = -16776961  ' Red
        .TintAndShade = 0
    End With
    .StopIfTrue = False
End With

' Clear the pointer
Set newCondition = Nothing
然后,您可以将
范围(“A1:S200”)
替换为

newWorkbook.Worksheets(1).Range("A1:S200")
甚至

newWorkbook.Worksheets(1).UsedRange

希望所有这些都有帮助。抱歉,长度太长。

哪张纸需要显示红色文本?第2张还是第1张?第1张纸需要显示红色文本。我希望将第2张纸隐藏起来。当我正在形成答案时,我突然想到问题实际上是对象限定的问题。您正在处理多个工作簿和工作表。当您说
范围(“A1:S200”)
在您的代码中,Excel必须猜测您想要的工作表。我相信Excel会选择ActiveWorksheet。我在回答中添加了一些注释,以帮助您编写代码,准确地告诉Excel哪些工作表应该得到更改。祝你好运。好吧,我想我已经编辑完了。与我最初的答案相比,有很多变化,但大多数变化是澄清和风格变化。核心是一样的,谢谢,效果很好。有人问我是否可以为相同的文本创建条件格式,但在添加新文本的下一行添加边框。因此,如果单元格a11添加了文本,那么单元格a12 b12 c12 d12将自动具有边框。如果a12添加了文本,那么a13 b13 c13 d13将添加黑色boders
newWorkbook.Worksheets(1).UsedRange