我是VBA的新手,我确实需要一些有关此问题的帮助。
我有100个excel文件,每个文件包含5个工作表。我想将第三个工作表的C-24单元格(具有相同模板的所有excel文件)从红色字体更改为负数。
单元格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
Sample Excel file 附有参考模板
我在这里做错了什么。任何建议都将不胜感激?
答案 0 :(得分:1)
可能是由于单元格的现有格式。 试试下面的代码
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
答案 1 :(得分:0)
以此替换您的DoWork子
Sub DoWork(wb As Workbook)
Sheets(3).Range("C24").NumberFormat = "#,##0.00_ ;[Red]-#,##0.00 "
End Sub
答案 2 :(得分:0)
不知道发生错误的位置,我一眼就能告诉你
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 If
之前缺少End With
(这确实有助于缩进)。
除此之外,我看不到任何初始化变量objXLws
或Value
的地方,建议将Option Explicit
放在代码模块的顶部。声明变量很重要。
此外,我怀疑您甚至根本不需要Value
或objXLWs
作为变量,因为您所做的只是反转符号并更改颜色。另外,更改单元字体颜色的语法为Cell.Font.Color = RGB()
对您来说可能更合适的事情
Sub DoWork(wb As Workbook)
With wb.Sheets(3)
If .Cells(24, "C").Font.Color = RGB(255, 0, 0) Then
.Cells(24, "C") = .Cells(24, "C") * -1
End if
End With
End Sub