比较两个值并生成一个百分比(excel)

时间:2015-08-05 10:08:54

标签: excel excel-vba percentage vba

我目前正在尝试创建一个电子表格,该电子表格会跟踪质量检查的文件数量与尚未检查的数量,然后显示剩余的数量作为百分比进行检查。

目前在打开时,电子表格会从已检查的文件夹和要检查的工作文件夹中提取详细信息,如下所示: -

Private Sub pdf_loading()

Range("M5").Clear

Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\path to folder\"
' looks in spercific folder
path = FolderPath & "*.pdf"
' for file type this time it is pdf files, though if you change this is could be word files, or psd's
Filename = Dir(path)

Do While Filename <> ""
' checks for filename <less than or >greater than "filename" as "" is empty does not look for spercific file
    count = count + 1
' counts amount of pdf files, add 1 to the last known number
    Filename = Dir()
' contiunes count until it reaches the end of the directory
Loop


Range("M5").Value = count
' puts final count value in cell

For Each Cell In [M:M]
If Cell.Value = "0" Then
Cell.ClearContents
ElseIf Range("M5").Value >= 1 Then
End If
Next Cell

End Sub

然后对于选中的文件夹: -

Private Sub checked_loading()

Range("M6").Clear

Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\path to folder\"

path = FolderPath & "*.pdf"

Filename = Dir(path)

Do While Filename <> ""
    count = count + 1
        Filename = Dir()
Loop

Range("M6").Value = count

For Each Cell In [M:M]
If Cell.Value = "0" Then
Cell.ClearContents
ElseIf Range("M5").Value >= 1 Then
End If
Next Cell

End Sub

这很好用,虽然目前我试图生成百分比的公式如下: -

=IF(M5=M6,"50%",IF(M5=0,"100%",IF(M6=0,"0%",SUM(M5*M6/100*1))))

如果要检查的文件结果为9且检查的文件结果为16,则会返回错误的结果,如144.00%。

我希望百分比计算在vba中,以便最终用户不会意外删除基础公式。

有关此问题的任何帮助,或者如果有更高效的代码结构,将非常感谢。

1 个答案:

答案 0 :(得分:0)

不用担心我找到了一种可以治疗的解决方案。现在,上面的代码对于要检查的工作看起来像这样: -

Private Sub pdf_loading()

Range("K5:L6").ClearContents
Range("M5").ClearContents
' Clear cell contents on open

Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\filepath\folder\"
' looks in spercific folder
path = FolderPath & "*.pdf"
' for file type this time it is pdf files, though if you change this is could be word files, or psd's
Filename = Dir(path)

Do While Filename <> ""
' checks for filename <less than or >greater than "filename" as "" is empty does not look for spercific file
    count = count + 1
' counts amount of pdf files, add 1 to the last known number
    Filename = Dir()
' contiunes count until it reaches the end of the directory
Loop


Range("M5").Value = count
' puts final count value in cell




End Sub

和work checked文件夹现在是这样的: -

Private Sub checked_loading()

Range("M6").ClearContents

Dim FolderPath As String, path As String, count As Integer
FolderPath = "C:\filepath\folder\"

path = FolderPath & "*.pdf"

Filename = Dir(path)

Do While Filename <> ""
    count = count + 1
        Filename = Dir()
Loop

Range("M6").Value = count


Range("N5").Formula = "=Sum(M5,M6)"
Range("K5").Formula = "=SUM(M6/N5*1)"
' adds formulas to selected cells to give percentage
End Sub