2015-11-04 94 views
0

我在PowerPoint中有一個表格,它是使用VBA從宏代碼生成的。我想改變負數的顏色爲紅色,正數的顏色爲綠色(需要改變顏色的值在第三列)。生成表,並把所有的數字變成了確切的行和列的代碼如下:更改PowerPoint中表格中特定值的顏色

Sub RangeTransferToTable102() 
' 
' Copy each data cell in Excel range to the PowerPoint Slide 46 Shape 102 Table 
' 
Dim pptApp As PowerPoint.Application 
Dim oPPTShape As PowerPoint.Shape 
Dim rng As Excel.Range 
Dim frmt As Variant 
' 
' Set oPPP to PowerPoint by creating a new instance of PowerPoint. 
' If PowerPoint is already open, you would instead use the GetObject 
' method instead. 
' 
Set pptApp = GetObject(, "PowerPoint.Application") 
' 
' Set PowerPoint to be Visible. 
' 
pptApp.Visible = msoTrue 
pptApp.ActivePresentation.Slides("Slide310").Select 
pptApp.Activate 
Worksheets("Switch_CS").Activate 
Set rng = Range("GR2:GV11") 

For rw = 1 To 10 
For cl = 1 To 5 
Data = rng.Cells(rw, cl).Value 

If Not (IsEmpty(rng.Cells(rw, cl))) Then 
If IsNumeric(rng.Cells(rw, cl)) Then 'Convert numeric value to text using number format 
frmt = rng.Cells(rw, cl).NumberFormat 
Data = WorksheetFunction.Text(rng.Cells(rw, cl).Value, frmt) 
End If 
Else 
Data = rng.Cells(rw, cl).Value 
End If 
With pptApp.ActivePresentation.Slides("Slide310").Shapes("Table 102").table.cell(rw + 1, cl) 
.Shape.TextFrame.TextRange.Delete 
.Shape.TextFrame.TextRange.Text = Data 
End With 
Next cl 
Next rw 

End Sub 

回答

0

試試這個:

Sub RangeTransferToTable102() 
' 
' Copy each data cell in Excel range to the PowerPoint Slide 46 Shape 102 Table 
' 
Dim pptApp As PowerPoint.Application 
Dim oPPTShape As PowerPoint.Shape 
Dim rng As Excel.Range 
Dim frmt As Variant 
' 
' Set oPPP to PowerPoint by creating a new instance of PowerPoint. 
' If PowerPoint is already open, you would instead use the GetObject 
' method instead. 
' 
Set pptApp = GetObject(, "PowerPoint.Application") 
' 
' Set PowerPoint to be Visible. 
' 
pptApp.Visible = msoTrue 
pptApp.ActivePresentation.Slides("Slide310").Select 
pptApp.Activate 
Worksheets("Switch_CS").Activate 
Set rng = Range("GR2:GV11") 

For rw = 1 To 10 
For cl = 1 To 5 
Data = rng.Cells(rw, cl).Value 

If Not (IsEmpty(rng.Cells(rw, cl))) Then 
If IsNumeric(rng.Cells(rw, cl)) Then 'Convert numeric value to text using number format 

If rng.Cells(rw, cl).value >= 0 Then 
rng.Cells(rw, cl).Font.Color = -11489280 
Else 
rng.Cells(rw, cl).Font.Color = -16776961 
End If 

frmt = rng.Cells(rw, cl).NumberFormat 
Data = WorksheetFunction.Text(rng.Cells(rw, cl).Value, frmt) 
End If 
Else 
Data = rng.Cells(rw, cl).Value 
End If 
With pptApp.ActivePresentation.Slides("Slide310").Shapes("Table 102").table.cell(rw + 1, cl) 
.Shape.TextFrame.TextRange.Delete 
.Shape.TextFrame.TextRange.Text = Data 
End With 
Next cl 
Next rw 

End Sub 
+0

它不工作 - 代碼工作,但對顏色細胞在功率點上根本不會改變。謝謝! – Panayotova