在多个Excel文件上运行Excel宏

在多个Excel文件上运行Excel宏,excel,vba,Excel,Vba,我是一个完全的VBA新手,我真的需要一些帮助解决这个问题 我有100个excel文件,每个文件包含5个工作表。我想将第三张工作表(所有具有相同模板的excel文件)的C-24单元格从红色字体更改为负数 单元格C24的红色字体值为负值,其余为正值。(红色字体为负数,绿色字体为正数时,颜色编码逻辑一致) 下面是给我一个错误的代码 Sub ProcessFiles() Dim Filename, Pathname As String Dim wb As Workbook Pat

我是一个完全的VBA新手,我真的需要一些帮助解决这个问题

我有100个excel文件,每个文件包含5个工作表。我想将第三张工作表(所有具有相同模板的excel文件)的C-24单元格从红色字体更改为负数

单元格C24的红色字体值为负值,其余为正值。(红色字体为负数,绿色字体为正数时,颜色编码逻辑一致)

下面是给我一个错误的代码

Sub ProcessFiles()
    Dim Filename, Pathname As String
    Dim wb As Workbook
    Pathname = "C:\CY 2018\12-Dec\"
    Filename = Dir(Pathname & "*.xls*")
    Do While Filename <> ""
        Set wb = Workbooks.Open(Pathname & Filename)
        DoWork wb
        wb.Close SaveChanges:=True
        Doevents
        Filename = Dir()
    Loop
End Sub
Sub DoWork(wb As Workbook)
    With wb.Sheets(3).Select   
    Value = Replace(objXLWs.Cells(24, "C").Text, vbLf, "<br>")
    If Value.Fore.Color.RGB = RGB(255, 0, 0) Then
    'nt.Color = vbRed Then  
    'If Value.Font.Color = vbRed Then
    Value = -(Value)         
    End With
End Sub   
子进程文件()
Dim文件名,路径名为字符串
将wb设置为工作簿
Pathname=“C:\CY 2018\12月”
Filename=Dir(路径名&“*.xls*”)
文件名“”时执行此操作
设置wb=Workbooks.Open(路径名和文件名)
销钉wb
wb.Close SaveChanges:=真
程问题
Filename=Dir()
环
端接头
子工作(wb作为工作簿)
使用工作分解表(3)。选择
Value=Replace(objXLWs.Cells(24,“C”).Text,vbLf,“
”) 如果Value.Fore.Color.RGB=RGB(255,0,0),则 'nt.Color=vbRed那么 '如果Value.Font.Color=vbRed,则 值=-(值) 以 端接头
附件是供参考的模板


我在这里做错了什么。非常感谢您的任何建议。

请用此替换您的定位销接头

Sub DoWork(wb As Workbook)
    Sheets(3).Range("C24").NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
End Sub

不知道你的错误发生在哪里,我一眼就能告诉你

Value = Replace(objXLWs.Cells(24, "C").Text, vbLf, "<br>")
If Value.Fore.Color.RGB = RGB(255, 0, 0) Then
'nt.Color = vbRed Then  
'If Value.Font.Color = vbRed Then
Value = -(Value)         
End With

可能是由于单元格的现有格式。 请尝试下面的代码


    Sub DoWork(wb As Workbook)
        With wb.Sheets(3).Range("C24") 'there is no need to select the sheet
            'include your other code here
            'Add this to address the format
            .NumberFormat = "0.00" 'this will show negative numbers with a minus, make it just 0 if you dont want decimal points
            '.NumberFormat = "0.00;[Red]0.00" 'this will show negative numbers in red font
        End With
    End Sub

编辑 更新代码以解决所有可能性(希望如此) 检查数字格式、条件格式或红色字体

Sub DoWork(wb As Workbook)
    Dim bolMakeNeg As Boolean
    Dim V As Variant
    Dim rng As Range
    bolMakeNeg = False
    'if the cell are merged this will extract the value
    If wb.Sheets("3. NCC's").Range("C24").MergeCells Then
        Set rng = wb.Sheets("3. NCC's").Range("C24").MergeArea
        V = rng.Cells(1, 1).Value
    Else
        Set rng = wb.Sheets("3. NCC's").Range("C24")
        V = rng.Value
    End If

    With rng 'obtain reference to the cell
            'check if the value of the cell is negative
            If V < 0 Then 'if this is a negative value, this will sort out problem if number formatting is making it red
                bolMakeNeg = True
            ElseIf .Font.Color = RGB(255, 0, 0) Or .DisplayFormat.Font.Color = RGB(255, 0, 0) Then 'if there is a conditional formatting the display format should point to the color displayed
                bolMakeNeg = True
            End If
            If bolMakeNeg Then
               'set number format
               .NumberFormat = "#,##0;-#,##0"  'change decimals as required
               If V > 0 Then   'set negative value
                .Value = -1 * V
               End If
               '*************
               'Add other code to set font face etc.
               rng.Font.Name = "Arial"
               rng.Font.Size = 9
               rng.Font.Bold = True
               '*************
            End If
    End With
End Sub
子工作(wb作为工作簿)
Dim bolMakeNeg作为布尔值
Dim V作为变体
变暗rng As范围
bolMakeNeg=False
'如果合并单元格,将提取值
如果wb表格(“3.NCC”)范围(“C24”)合并单元格,则
设置rng=wb.Sheets(“3.NCC”)范围(“C24”)。合并区域
V=rng.单元格(1,1).值
其他的
设置rng=wb.Sheets(“3.NCC”)范围(“C24”)
V=平均值
如果结束
使用rng'获取对单元格的引用
'检查单元格的值是否为负值
如果V<0,则“如果这是一个负值,这将解决数字格式设置为红色时的问题
bolMakeNeg=True
ElseIf.Font.Color=RGB(255,0,0)或.DisplayFormat.Font.Color=RGB(255,0,0),则“如果存在条件格式,则显示格式应指向显示的颜色
bolMakeNeg=True
如果结束
如果是bolMakeNeg那么
'设置数字格式
.NumberFormat=“#,##0;-#,##0”根据需要更改小数
如果V>0,则“设置负值”
.Value=-1*V
如果结束
'*************
'添加其他代码以设置字体等。
rng.Font.Name=“Arial”
rng.Font.Size=9
rng.Font.Bold=True
'*************
如果结束
以
端接头

宏正在运行,没有任何错误。但是工作表中的单元格C-24没有将红色字体值更改为负值。我们可以添加工作表名称,而不是工作表编号。表名是“NCC”,我测试了它,并按照要求得到了结果。如果不清楚哪里出了问题,就很难确定什么地方不适合你。如果工作表名称始终是工作簿的第三张工作表,则该工作表名称无关紧要。可能要确保包含宏的文件不是要循环通过的文件夹中的第一个文件?代码运行正常,但我没有看到负值。我的红色值没有变为负值代码工作正常但我不明白为什么我看不到负值。查看随附的参考表excel(3)-单元格C24I要将字体颜色(红色)更改为负数,而不是单元格颜色。我应该使用.Cells(24,“C”).write(rgb(255,30,30))=rgb(255,0,0)对不起,我在“单元格字体颜色”中漏掉了“字体”一词。这只会改变数字,不是单元格颜色本身。还值得一提的是,如果您的工作表上有条件格式或自定义格式,并且根据单元格值更改格式,则您可能看不到正在查找的结果,并且需要以编程方式禁用。它不会给您错误号或错误信息描述或任何东西?我如何附加示例模板和我在此处使用的代码我已包括数字格式,但我没有看到红色字体值的负数该值没有变为负值,我已尝试了您的代码,附加的是参考表的excel(3)-单元格C24共享excel的感谢,现在更容易找出问题所在。1) 使用的图纸参考(3)与5中的图纸参考不正确。我建议您使用工作表名称,如代码所示。2) 您所引用的单元格是一个合并单元格,这可能是.value不起作用的原因。这已在更新的代码中解决。希望这对您有用。请注意,
Dim Filename,Pathname As String
只声明
Pathname为String
,而
Filename为Variant
。在VBA中,必须为每个变量指定一种类型:
Dim Filename为String,Pathname为String
Sub DoWork(wb As Workbook)
    Dim bolMakeNeg As Boolean
    Dim V As Variant
    Dim rng As Range
    bolMakeNeg = False
    'if the cell are merged this will extract the value
    If wb.Sheets("3. NCC's").Range("C24").MergeCells Then
        Set rng = wb.Sheets("3. NCC's").Range("C24").MergeArea
        V = rng.Cells(1, 1).Value
    Else
        Set rng = wb.Sheets("3. NCC's").Range("C24")
        V = rng.Value
    End If

    With rng 'obtain reference to the cell
            'check if the value of the cell is negative
            If V < 0 Then 'if this is a negative value, this will sort out problem if number formatting is making it red
                bolMakeNeg = True
            ElseIf .Font.Color = RGB(255, 0, 0) Or .DisplayFormat.Font.Color = RGB(255, 0, 0) Then 'if there is a conditional formatting the display format should point to the color displayed
                bolMakeNeg = True
            End If
            If bolMakeNeg Then
               'set number format
               .NumberFormat = "#,##0;-#,##0"  'change decimals as required
               If V > 0 Then   'set negative value
                .Value = -1 * V
               End If
               '*************
               'Add other code to set font face etc.
               rng.Font.Name = "Arial"
               rng.Font.Size = 9
               rng.Font.Bold = True
               '*************
            End If
    End With
End Sub