根据 2 个单元格之间的差异插入 n 行

问题描述

我正在导出 excel 文件,但通常它们缺少数据。所以我想创建一个脚本或其他东西,为每个丢失的数据在单元格之间插入一个空白行。例如:

[在此处输入图片说明][1]

正如您在单元格 1569-1570 之间看到的那样,每个数据缺少 5 小时/5 分钟=60 个测量值,这意味着我需要 1569 和 1570 之间的 60 个空白行。由于导出格式,2 个连续单元格之间的差异总是0.00347。有人可以帮我吗?

我找到了一个并尝试修改它,但它显示最后一行被 0 除。在搜索之后,我发现在某个地方 VB 不喜欢带小数的 mod。有没有办法解决这个问题?

    Public Sub blank()
    
    
    Dim yourColumn As String
    Dim rowToStart As Integer
    Dim nameOfYourSheet As String
    Dim i As Integer
    Dim k As Double
    
    Dim currentvalue As Date
    Dim prevIoUsvalue As Date
    
    
    'EDIT VALUES HERE
    yourColumn = "B"
    rowToStart = 4
    k = 0.00347
    nameOfYourSheet = "02 February calculations"
    
    With ThisWorkbook.Sheets(nameOfYourSheet)
        'For each cell in the column from the end
        For i = .Cells(.Rows.Count,yourColumn).End(xlUp).Row To rowToStart + 1 Step -1
            currentvalue = (.Range(yourColumn & i).Value)
            prevIoUsvalue = (.Range(yourColumn & i).Offset(-1,0).Value)
    
            'If the difference with the prevIoUs > k
            If ((currentvalue - prevIoUsvalue) > k) Then
                'Add rows
                .Rows(i).EntireRow.Insert Shift:=((currentvalue - prevIoUsvalue) Mod (k))
            End If
        Next i
    End With
    
    End Sub


  [1]: https://i.stack.imgur.com/MZPRS.png

编辑:我一直在挖掘这个,现在它运行了,但它只在所有单元格之间插入了 1 行。我想这是因为 currentvalue 无法正常工作,因为它给出了错误的值。任何帮助将不胜感激。

Public Sub blank()


Dim yourColumn As String
Dim rowToStart As Integer
Dim nameOfYourSheet As String
Dim i As Integer
Dim k As Double
Dim k1 As Double
Dim m As Double
Dim n As Double


Dim currentvalue As Date
Dim prevIoUsvalue As Date


'EDIT VALUES HERE
yourColumn = "B"
rowToStart = 4
k = 0.003472
nameOfYourSheet = "02 February calculations"

With ThisWorkbook.Sheets(nameOfYourSheet)
    'For each cell in the column from the end
    For i = .Cells(.Rows.Count,yourColumn).End(xlUp).Row To rowToStart + 1 Step -1
        currentvalue = (.Range(yourColumn & i).Value)
        prevIoUsvalue = (.Range(yourColumn & i).Offset(-1,0).Value)
        
        m = (currentvalue - prevIoUsvalue) * 100000
        k1 = k * 100000
        
        'If the difference with the prevIoUs > k1
        If (m > k1) Then
            n = m / k1
            'Add rows
            .Rows(i).EntireRow.Insert Shift:=n
        End If
        
    Next i
End With

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)