[英]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.