我在查找预设范围内的缺失值时遇到问题。例如,我有:
A列:3、5、7、9、10、11 我想要VBA或Excel中的公式返回 B栏:1、2、4、6、8、12、13-> 30
B列中的最终编号将有所不同。我希望它根据用户的判断进行更改。对于我的问题,我每天工作几天。因此,一月,二月和三月的总天数不同。我看过很多示例,但所有示例均基于spreedsheet中的数组。如果我想将数组更改为1-30、1-31或1-28(取决于我正在查看的月份)怎么办?
答案 0 :(得分:0)
这是VBA代码的样子:
Option Explicit
Sub MissingNumbers()
Dim C As Range
Dim LastRow As Long
Dim FirstNumber As Long
Dim LastNumber As Long
Dim i As Long
Dim MyNumbers As New Scripting.Dictionary 'Need Microsoft Scripting Runtime library checked on references
FirstNumber = 1 'here goes your first number to check
LastNumber = 30 'here goes the last number to check
'Assume you have a column A with all the values and column B will hold the missing ones
With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your working sheet name
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row 'Last Row on column A
'Store your existing numbers in the dictionary
For Each C In .Range("A2:A" & LastRow) 'I'm assuming you have headers on row 1
MyNumbers.Add C.Value, 1
Next C
LastRow = 2 'starting on row 2 for column B
'Loop through all the numbers you want to check
For i = FirstNumber To LastNumber
'If the number doesn't exist on the dictionary
If Not MyNumbers.Exists(i) Then
.Cells(LastRow, 2) = i 'write it on the next available row for columnB
LastRow = LastRow + 1 'add 1 number to last row so we don't overwrite results
End If
Next i
End With
End Sub
答案 1 :(得分:0)
这与OP在上面的注释中引用的公式相似,但针对用户定义的数字数量进行了概括:
=IFERROR(SMALL(IF(ISERROR(MATCH(ROW(A$1:INDEX(A:A,C$2)),D$2:INDEX(D:D,COUNT(D:D)+ROW(D$1)),0)),
ROW(A$1:INDEX(A:A,C$2))),ROW()-ROW(A$1)),"")
必须使用 Ctrl Shift Enter
作为数组公式输入答案 2 :(得分:0)
这是一个例子:
B2
中的公式:
=IFERROR(INDEX(ROW($A$1:$A$30),MATCH(0,IFERROR(MATCH(ROW($A$1:$A$30),$A$2:$A$7,0),COUNTIF($B$1:$B1,ROW($A$1:$A$30))),0)),"")
通过 Ctrl Shift Enter
确认您可以根据自己的喜好将此部分ROW($A$1:$A$30)
更改为包含更大范围(或更小)的数字,具体取决于月份。
答案 3 :(得分:0)
您可以尝试:
Option Explicit
Sub test()
Dim LastRowA As Long, LastRowB As Long, i As Long, StartValue As Long, EndValue As Long
Dim rng As Range
StartValue = 1
EndValue = 30
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = .Range("A1:A" & LastRowA)
For i = StartValue To EndValue
If Application.WorksheetFunction.CountIf(rng, i) = 0 Then
LastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row
If LastRowB = 1 And .Range("B" & LastRowB).Value = "" Then
.Range("B1").Value = i
Else
.Range("B" & LastRowB + 1).Value = i
End If
End If
Next i
End With
End Sub
结果:
答案 4 :(得分:0)
可能的formula solution为 ARRAY公式:CTRL + SHIFT + ENTER
{% if variable is even or variable is odd %}
向下复制以获得所有值。