繁体   English   中英

Excel VBA-评估函数是否返回#NAME? 错误

[英]Excel VBA - Evaluate Function Returning #NAME? Error

因此,我写了一个VBA代码来计算某些输入数据在每个标题下的空白,非空白和总数项。 我想添加一个代码,将值从一张纸复制并粘贴到另一张纸上,对值进行重复数据删除,为我提供每个页眉下的值的唯一列表,唯一值的数量以及这些唯一值在“页眉”下出现的次数。标头。

空白:我之前使用过countblank函数,但是它会跳过某些空字段,因此我将其更改为sumproduct(len(Range)=0)*1) 非空白:我写了一个类似的函数,并试图计算上述结果。

事实证明,VBA无法处理Sumproduct函数。 这是我尝试过的方法:

 1. Application.WorksheetFunction.Sumproduct(...)
 2. ..Number.. = "=Sumproduct(...)"
 3. ..Number.. = Evaluate("Sumproduct(...)")
 4. ..Number.. = Worksheet.Evaluate("Sumproduct(...)")

下面是宏的代码,我正在将代码写在Input_File(即Input工作表)上。

Sub Dedupe()
ThisWorkbook.Worksheets("Control_Totals").Cells.ClearContents

Dim lRow As Long
Dim lCol As Long
Dim i As Long
Dim j As Long
Dim Input_File As Worksheet
Dim Output_File As Worksheet
Dim Dedup_File As Worksheet
Dim Col_Let As String
Dim Rng As String
Dim blank As String
Dim non_blank As String

Set Input_File = ThisWorkbook.Worksheets("Input")
Set Output_File = ThisWorkbook.Worksheets("Control_Totals")
Set Dedup_File = ThisWorkbook.Worksheets("Deduped")

With Output_File
        .Cells(1, 1) = "Field_Name"
        .Cells(1, 2) = "Blanks"
        .Cells(1, 3) = "Non-Blanks"
        .Cells(1, 4) = "Total"
End With

'Finding the last row among all entries, including the blank ones
lRow = Input_File.Cells.Find(What:="*", _
                    After:=Range("A1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row

MsgBox "Last Row: " & lRow

'Finding the last column header/field
lCol = Input_File.Cells.Find(What:="*", _
                  LookAt:=xlPart, _
                  LookIn:=xlFormulas, _
                  SearchOrder:=xlByColumns, _
                  SearchDirection:=xlPrevious, _
                  MatchCase:=False).Column

MsgBox "Last Column: " & lCol

i = 1

'Finding the number of blank and non-blank entries for all the fields
Do While i < lCol + 1
Col_Let = ColumnLetter(i)
Rng = "Input!" & "Col_Let" & "2" & ":" & "lRow"


    Output_File.Cells(i + 1, 1) = Input_File.Cells(1, i)

         blank = "SumProduct((Len(Rng) = 0) * 1)"
         non_blank = "SumProduct((Len(Rng) > 0) * 1)"

    Output_File.Cells(i + 1, 2).Value = Evaluate(blank)
    Output_File.Cells(i + 1, 3).Value = Evaluate(non_blank)
    Output_File.Cells(i + 1, 4) = lRow - 1


    'Deduping the data under the headers
    j = 0
    For j = 1 To lRow
    Dedup_File.Cells(j, i).Value = Input_File.Cells(j, i).Value
    j = j + 1
    Next
    Dedup_File.Range(Cells(1, i), Cells(lRow, i)).RemoveDuplicates Columns:=1, _ 
    Header:=xlYes

        i = i + 1

Loop

End Sub

这些行没有按照您的想法做

Col_Let = ColumnLetter(i)
Rng = "Input!" & "Col_Let" & "2" & ":" & "lRow"

Rng始终是包含“ Input!Col_Let2:lRow”的字符串

您的意思是:(我认为)Rng =“输入!” &Col_Let&“ 2”&“:”&Col_Let&lRow

其次,Rng仅存在于此vba例程中-对Excel而言没有任何意义,因此您不能在Excel公式中使用它。 你需要

blank = "SumProduct((Len(" & Rng.address & ") = 0) * 1)"

最后,SumProduct不喜欢VBA中的这类技巧(它依靠excel自动将1扩展到数组中)。 更好的解决方案:

Dim cBlank as long
Dim cNonBlank as long
Dim r as range
For each r in rng
if r.text = "" then 
   cBlank= cBlank+1
else 
   cNonBlank = cNonBlank +1
 end if
 next r

我想添加一个代码,将值从一张纸复制并粘贴到另一张纸上,对值进行重复数据删除,为我提供每个页眉下的值的唯一列表,唯一值的数量以及这些唯一值在“页眉”下出现的次数。标头。

您刚刚描述的是一个数据透视表,其中“行”区域和“值”区域中的关注字段都作为“计数”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM