4Manuals

  • PDF Cloud HOME

Excel VBA遍历一个范围以在找到数字时复制相应列中的单元格 Download

    在多个XML中导出Excel 为什么Excel因自动化错误而崩溃? EXCEL评估函数结果不一致 宏VBA-具有2个Y轴的xlColumnClustered Excel VBA用户表单文本框十进制格式 优化刮取和循环 如何调用带有组名或标签的复选框? Excel,XPATH函数和数字格式 VBA:文本框搜索栏和列表框 在用户窗体VBA中格式化下拉列表

我已经很长时间没有使用VBA了,我正在尝试创建一个用于制作时序图的用户界面类型的东西。下面您会看到我有一个进程列表以及每个进程需要多长时间。用户将在右侧的单元格中输入从1开始的数字

order of operations chart

我要编写的程序需要执行以下操作: 任何有数字的行都会复制B和F列中的信息,然后将其粘贴到第39行+发现的任何数字中。这将在图表中创建一个顺序正确的列表,如下所示。

output chart

我试图编写一个程序,该程序循环遍历该数字输入区域中每一列的每一行,但是我的VBA达不到标准,并且出现了问题。任何帮助将不胜感激。下面是到目前为止我没有成功使用的代码

# bad - uses DateTime for current time
DateTime.now

# good - uses Time for current time
Time.now

# bad - uses DateTime for modern date
DateTime.iso8601('2016-06-29')

# good - uses Date for modern date
Date.iso8601('2016-06-29')

# good - uses DateTime with start argument for historical date
DateTime.iso8601('1751-04-23', Date::ENGLAND)

经过编辑以反映删除了“ range(”

1 个答案:

答案 0 :(得分:2)

要获取数字,将其放在A39中并抄下来:

=IFERROR(SMALL($M$4:$AG$33,ROW(A1)),"")

要获取操作,请将其放在B29中并复制下来:

=IF(A39<>"",INDEX(B:B,AGGREGATE(15,7,ROW($M$4:$AG$33)/($M$4:$AG$33=A39),COUNTIF($A$39:A39,A39))),"")

要获取时间,请将其放在F39中并抄下来:

=IF(A39<>"",INDEX(F:F,MATCH(B39,$B:$B,0)),"")

因此,数据如下:

enter image description here

使用这些公式,我们得到:

enter image description here

如果您真的想要vba,请忘记使用offset,而只需参考以下列:

Sub TimingChart()
    With ThisWorkbook.Worksheets("Sheet1")

        Dim rng As Range
        Set rng = .Range("M4:AG33")

        'loop columns
        Dim col As Range
        For Each col In rng.Columns

        'loop rows of that column
            Dim cel As Range
            For Each cel In col.Cells
        'skip cells with nothing in them
                If cel.Value = "" Then

        'any cell with a number copy the data in row B and row F and
        'paste in the chart below the entry field starting in row 40
        'the data should copy into row 39 + x putting the operations in order
                Else
                    'x is the variable for row adjustment to determine what cell to paste into
                    Dim x As Long
                    x = cel.Value
                    .Cells(cel.Row, 2).Copy .Cells(39 + x, 2)
                    .Cells(cel.Row, 6).Copy .Cells(39 + x, 6)
                    cel.Copy .Cells(39 + x, 1)

                End If
            Next cel
        Next col
    End With
End Sub



Similar searches
    R中的here包如何解决路径问题? CUDA函数不执行内核,而其他函数执行 为什么Google附近的连接受到严重限制? 在Stackblitz上将MapBoxDirections添加到Angular Application中不起作用 有关提供map的parseInt的问题