Excel下面的VB代码检查,类似的主题行并在第一列中创建空范围。一旦宏触发,输入框要求我选择具有相似主题行的范围,然后输入框要求我给出要创建的最小范围数,然后它将第一列中的范围命名为空范围1,2,依此类推。基本上有效地工作这个函数Count如果VB代码中的函数应该处理所有类型的数据,例如数字或文字。我观察到,如果计数函数给出错误值,例如#Value, #N/a or "0"
,那么范围就不会被正确创建。如果你能提出改进建议,那就非常好了。我有两张“Raw_Data”和“Ghost”,后来我打算在隐藏模式下使用Ghost表。
`Sub Assignments()
Dim Vall As String
Dim Colns As Range
Dim Numb As Variant
Dim Cnt As Variant
Sheets("Raw_Data").Activate
On Error Resume Next
Set Colns = Application.InputBox("Select a column which has similar subjects.", "Range Selection", Type:=8)
If Not Colns Is Nothing Then
Colnss = Colns.Column
On Error Resume Next
Numb = Application.InputBox("Enter the number of documents to be assigned.", "Range Selection", Type:=1)
Columns(1).Insert Shift:=xlToRight
Cells(1, 1).Value = "Ranges"
Colmns = Colnss + 1
Thrd = Sheets("Raw_Data").Cells(2, Colmns).End(xlDown).Row
If Numb <> False Then
Range(Sheets("Ghost").Cells(2, 1), Sheets("Ghost").Cells(Thrd, 1)).Value = _
Range(Sheets("Raw_Data").Cells(2, Colmns), Sheets("Raw_Data").Cells(Thrd, Colmns)).Value
Range(Sheets("Ghost").Cells(2, 1), Sheets("Ghost").Cells(Thrd, 1)).RemoveDuplicates Columns:=1, Header:=xlNo
Cntif = 2
ABC = 1
Rais = 0
Do While Sheets("Ghost").Cells(Cntif, 1).Value <> ""
Vall = Sheets("Ghost").Cells(Cntif, 1).Value
'this should not return 0 or Error values
Cnt = Application.WorksheetFunction.CountIf(Sheets("Raw_Data").Columns(Colmns), Vall)
Rais = Rais + Cnt
If Rais > Numb Or Rais = Numb Then
UsrNme = Sheets("Raw_Data").Range("A1048576").End(xlUp).Row + 1
Range(Sheets("Raw_Data").Cells(UsrNme, 1), Sheets("Raw_Data").Cells(UsrNme + (Rais - 1), 1)).Value = "Empty Range " & ABC
Rais = 0
ABC = ABC + 1
End If
Set Cnt = Nothing
Cntif = Cntif + 1
Loop
Frst = Sheets("Raw_Data").Cells(2, 1).End(xlDown).Row + 1
Range(Cells(Frst, 1), Cells(Thrd, 1)).Value = "Last Range"
Columns(1).AutoFit
End If
End If
End Sub`
如果不考虑计数的主题示例,请在下面的单元格B1,B2中粘贴,然后尝试: