Excel VBA,循环记录答案

Excel VBA,循环记录答案,vba,excel,loops,Vba,Excel,Loops,我试图编写一些代码,使用从列中读取的班次持续时间的输入数据来选择班次的开始时间和结束时间(如果单元格不为空,则报告时间) 不过,在我尝试硬编码变量和范围之前,我只是在调试 我的 如果单元格(i,j).值为“”,则 移位持续时间=单元格(i,j).值*(0.5/12) 线路工作不正常,测试时返回0值,因为未记录它们的值。因此,它不考虑班次的实际开始时间,即(班次结束时间-班次持续时间) 子测试2() 表格(“测试”)。选择 我和我一样长,我和我一样长 暗移持续时间 变暗换档启动时间尽可能长 对于

我试图编写一些代码,使用从列中读取的班次持续时间的输入数据来选择班次的开始时间和结束时间(如果单元格不为空,则报告时间)

不过,在我尝试硬编码变量和范围之前,我只是在调试

我的

如果单元格(i,j).值为“”,则
移位持续时间=单元格(i,j).值*(0.5/12)
线路工作不正常,测试时返回0值,因为未记录它们的值。因此,它不考虑班次的实际开始时间,即(班次结束时间-班次持续时间)

子测试2()
表格(“测试”)。选择
我和我一样长,我和我一样长
暗移持续时间
变暗换档启动时间尽可能长
对于i=31到44
对于j=4到4
如果单元格(i,j).的值为“”,则
移位持续时间=单元格(i,j).值*(0.5/12)
nextshift=单元格(i+1,1).值
换档开始范围=下一个换档-换档持续时间
单元格(1,21)=下一个移位
单元(1,22)=移位持续时间
单元(i,8)=移位起始范围
其他的
如果结束
下一个j
接下来我
端接头
我所有的代码


谢谢

我将
Shiftduration
ShiftStart
Long
更改为
Double
。 我会指定您使用的工作表

让我知道它是否像你想要的那样工作

     Sub Test2()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim i As Long, j As Long
        Dim Shiftduration As Double
        Dim ShiftStart As Double

        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Test")

        For i = 31 To 44
                 For j = 4 To 4
                If ws.Cells(i, j).Value <> "" Then

                    Shiftduration = ws.Cells(i, j).Value * (0.5 / 12)

                 nextshift = ws.Cells(i + 1, 1).Value

                ShiftstartRange = nextshift - Shiftduration

                ws.Cells(1, 21) = nextshift
                ws.Cells(1, 22) = Shiftduration
                ws.Cells(i, 8) = ShiftstartRange

             Else

            End If

        Next j
   Next i


End Sub
子测试2()
将wb设置为工作簿
将ws设置为工作表
我和我一样长,我和我一样长
暗移持续时间为双
变暗换档启动为双档
设置wb=ThisWorkbook
设置ws=wb.Sheets(“测试”)
对于i=31到44
对于j=4到4
如果ws.Cells(i,j).Value为“”,则
移位持续时间=ws.单元格(i,j).值*(0.5/12)
nextshift=ws.Cells(i+1,1).Value
换档开始范围=下一个换档-换档持续时间
ws.Cells(1,21)=下一个移位
ws.Cells(1,22)=移位持续时间
ws.Cells(i,8)=移位起始范围
其他的
如果结束
下一个j
接下来我
端接头

请帮我们(和您自己!)和您自己一个忙,并正确缩进您的代码!(如我的编辑所示)。要调试,请在
Shiftduration=…
之后添加
debug.Print i,j,Cells(i,j).Value,Shiftduration
。你在眼前的窗口里看到了什么?谢谢你,值得注意!干得好!谢谢
Sub Test2()
    Sheets("Test").Select

    Dim i As Long, j As Long
    Dim Shiftduration As Long
    Dim ShiftStart As Long

    For i = 31 To 44 
    For j = 4 To 4 
        If Cells(i, j).Value <> "" Then
            Shiftduration = Cells(i, j).Value * (0.5 / 12)
            nextshift = Cells(i + 1, 1).Value
            ShiftstartRange = nextshift - Shiftduration
            Cells(1, 21) = nextshift
            Cells(1, 22) = Shiftduration
            Cells(i, 8) = ShiftstartRange
        Else
        End If
    Next j
    Next i
End Sub
     Sub Test2()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim i As Long, j As Long
        Dim Shiftduration As Double
        Dim ShiftStart As Double

        Set wb = ThisWorkbook
        Set ws = wb.Sheets("Test")

        For i = 31 To 44
                 For j = 4 To 4
                If ws.Cells(i, j).Value <> "" Then

                    Shiftduration = ws.Cells(i, j).Value * (0.5 / 12)

                 nextshift = ws.Cells(i + 1, 1).Value

                ShiftstartRange = nextshift - Shiftduration

                ws.Cells(1, 21) = nextshift
                ws.Cells(1, 22) = Shiftduration
                ws.Cells(i, 8) = ShiftstartRange

             Else

            End If

        Next j
   Next i


End Sub