首页 > 解决方案 > 根据给定列的值选择 Microsoft Excel 中的单元格

问题描述

首先,我是VBA的完全新手,我的以下考虑是基于我对该主题的搜索

这是我的问题:我需要为每一行删除以紫色表示的单元格数量。我正在尝试编写一个小型 VBA 脚本,让我可以自动执行此操作。由于这个范围是连续的(我必须删除从 F 列到右侧的所有单元格),我知道使用 VBA,我可以使用Range().Select

理想情况下,我认为工作流程是这样的:

    for each line in {2,N}
    do 
    fcol = 6 + "$EN" # "$EN" means the value of the cell located in fixed column E, N line; fcol is the final column number
    Range(6,N:fcol,N).Select # from six column (F) to end column, "fcol"
enddo

但我不知道如何在 VBA 中实现它:| 任何帮助都将受到欢迎!

在此处输入图像描述

标签: excelvba

解决方案


试试这个

Sub test()
    Dim cel As Range

    For Each cel In Range("E2", Cells(Rows.Count, 5).End(xlUp))
        If cel.Value > 0 Then cel.Offset(, 1).Resize(, cel.Value).ClearContents ' this will delete cell content only
        If cel.Value > 0 Then cel.Offset(, 1).Resize(, cel.Value).Delete xlToLeft ' this will delete cells and shifting left those at the right of deleted ones
    Next
End Sub

只需根据我留下的评论删除您不需要的行


推荐阅读