简体   繁体   中英

Excel VBA - Evaluate Function Returning #NAME? Error

So I wrote a VBA code to calculate the number of blanks, non-blanks and total entries under each header for some input data. I want to add a code that copies and pastes the values from one sheet to another, dedupes the values, gives me the unique list of values under each header, number of unique values, and the number of times those unique values are occurring under the header.

Blanks: I used the countblank function earlier, but it would skip certain empty fields, so I changed it to sumproduct(len(Range)=0)*1) . Non-Blanks: I wrote a similar function and tried to calculate the above.

It turns out VBA is unable to process the Sumproduct function. Here are the approaches I have tried:

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

Below is the code for the macro, I am writing the code on the Input_File, ie, the Input worksheet.

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

These lines don't do what you think they do

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

Rng is always a string containing "Input!Col_Let2:lRow"

What you meant was: (I think) Rng = "Input!" & Col_Let & "2" & ":" & Col_Let & lRow

Secondly Rng exists only within this vba routine - it doesn't mean anything to Excel so you can't use it in an Excel Formula. You need

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

and finally SumProduct doesn't like those sort of tricks in VBA (It relies on excel expanding the 1 into an array automatically). A better solution:

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

I want to add a code that copies and pastes the values from one sheet to another, dedupes the values, gives me the unique list of values under each header, number of unique values, and the number of times those unique values are occurring under the header.

What you have just described there is a PivotTable, with the field of interest in both the Rows area and in the Values area as a Count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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