Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel中使用VBA在列表中插入缺少的值_Excel_Vba - Fatal编程技术网

在Excel中使用VBA在列表中插入缺少的值

在Excel中使用VBA在列表中插入缺少的值,excel,vba,Excel,Vba,我是VBA的新手,所以请容忍我 我有一个缺少一些值的连续整数列表,还有一个缺少值的列表。这些是A列和B列。我希望能够在列表中搜索比所选单元格中的值小一的数字,在该数字下方插入一个单元格,然后将所选值放入新单元格,然后从“缺少的值”列表中删除 例如: 假设我在A栏中有以下列表:(12356910) B栏:(4 7 8) 我希望能够在B列中选择包含“4”的单元格,并有一个子单元,该子单元将: 1.搜索列A中的列表以查找包含“3”的单元格 2.在包含“3”的单元格下方的a列中插入一个单元格,将其余单元

我是VBA的新手,所以请容忍我

我有一个缺少一些值的连续整数列表,还有一个缺少值的列表。这些是A列和B列。我希望能够在列表中搜索比所选单元格中的值小一的数字,在该数字下方插入一个单元格,然后将所选值放入新单元格,然后从“缺少的值”列表中删除

例如: 假设我在A栏中有以下列表:(12356910) B栏:(4 7 8) 我希望能够在B列中选择包含“4”的单元格,并有一个子单元,该子单元将: 1.搜索列A中的列表以查找包含“3”的单元格 2.在包含“3”的单元格下方的a列中插入一个单元格,将其余单元格向下移动 3.将“4”放入新的A列单元格 4.从B列中删除包含“4”的单元格,将剩余部分上移

我把它简化了。。。我想用代码来实现这一点,因为实际上我的列A列表有近10000个条目,而缺少值的列B列表包含几百个条目

我也许可以自己去想,但我会很感激一些我不需要花几个星期来调整的东西。似乎如果我知道更多VBA就不会那么难了。。。总有一天

这对我来说很有用,使用您的指导原则,您建议的数据:
工作起来很有魅力!感谢你没有简单地否决这个问题,继续前进。我也会学习代码,学习我能为将来做的,非常有帮助!我不知道。。。可能是别人干的。不是我,我试着提高投票率,但很明显我来的时间不够长。。。没有足够的“声誉”?我不知道。无论如何,要知道你的代码仍然工作得很好,我首先感谢你的帮助!别担心。很乐意帮忙!已完成投票:)
Private Sub fixList()
  ' Get reference to selected source cell
  Dim criteriaCell As Range
  Set criteriaCell = ActiveCell

  ' Get value of that cell to be found in search column
  Dim valueToLookFor As Long
  valueToLookFor = criteriaCell.Value - 1

  ' Get reference to matched cell in search column
  Dim foundCell As Range
  Set foundCell = Range("A:A").Find(valueToLookFor, , xlValues, xlWhole, , , False)

  ' If the search didn't come back with nothing, the search criteria was found
  If Not foundCell Is Nothing Then

    ' Insert a cell below the found cell and populate it with the search data
    foundCell.Select
    foundCell.Offset(1, 0).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    foundCell.Offset(1, 0).Value = criteriaCell.Value

    ' Delete the original criteriaCell
    criteriaCell.Select
    criteriaCell.Delete xlUp
  End If
End Sub
Sub Interpolate() 
    Dim FirstR As Integer, FirstC As Integer 
    Dim r As Integer, c As Integer 
    FirstR = ActiveCell.Row 
    FirstC = ActiveCell.Column 
    For c = 0 To Selection.Columns.Count - 1 
        For r = 0 To Selection.Rows.Count - 1 
            If Len(Cells(FirstR + r, FirstC + c)) = 0 Then 
                Cells(FirstR + r, FirstC + c).Value = _ 
                (Cells(FirstR + r - 1, FirstC + c) + _ 
                Cells(FirstR + r + 1, FirstC + c)) / 2 
                With Cells(FirstR + r, FirstC + c) 
                    .Font.Bold = True 
                    .Font.ColorIndex = 3 
                End With 
            End If 
        Next r 
    Next c 
End Sub