【问题标题】:Excel VBA Do until Column EExcel VBA 一直到 E 列
【发布时间】:2018-12-27 10:17:27
【问题描述】:

我有一个代码用于复制 Range(D9:E31) 的所有格式和边框,以及合并的单元格 (D9:E9)。一旦用户按下按钮 Range(D9:E31) 被复制到下一个可用单元格 Range(F9:G31)、Range(H9:I31) 等。

我开发了一个代码,用于以相反的顺序删除复制的单元格 Range(H9:31), Range(F9:G31)... 但是我的核心数据位于 Range(D9:E31) 中,因此不应删除在任何情况下。

如何让我的代码一直运行到 E 列。一旦到达 E 列,它应该停止工作,并且无论按下多少次按钮都不应该执行任何操作。我可以稍后自己添加警告消息。

我试过Do Until 没有成功。但是,我不需要循环直到 E 列。每次按下按钮时我都需要运行 VBA。通过使用循环,它将删除所有内容,直到 E 列?也许在这种情况下应该使用If?如果下一个单元格不在 E 列中,那么运行代码?

我的代码:

        Sub Remove()
    With Worksheets("Price calculation")
'Do Until Columns(4)
        lc = .Cells(9, .Columns.Count).End(xlToLeft).Column
        .Range(.Cells(9, lc - 0), .Cells(9, lc)).MergeArea.UnMerge
        lc = .Cells(11, .Columns.Count).End(xlToLeft).Column
        .Range(.Cells(9, lc - 1), .Cells(31, lc)).ClearContents
        .Range(.Cells(9, lc - 1), .Cells(31, lc)).Interior.ColorIndex = 2
        .Range(.Cells(9, lc - 1), .Cells(31, lc)).Borders.LineStyle = xlNone
    End With
    End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    清除合并列的内容

    由于不清楚是要全部删除还是要一个一个删除,所以我将两者都包括在内。

    一个接一个

    每次运行时,最后 2 列范围都会被“删除”(如果有)。

    Sub RemoveOneByOne()
        Dim lc As Integer
        With Worksheets("Price calculation")
            lc = .Cells(9, .Columns.Count).End(xlToLeft).Column + 1
            If lc > 5 Then
                With .Range(.Cells(9, lc - 1), .Cells(31, lc))
                    .UnMerge
                    .ClearContents
                    .Interior.ColorIndex = 2
                    .Borders.LineStyle = xlNone
                End With
            End If
        End With
    End Sub
    

    全部

    每次运行时,所有 2 列范围都将被“删除”(如果有)。

    Sub RemoveAll()
        Dim lc As Integer
        With Worksheets("Price calculation")
            lc = .Cells(9, .Columns.Count).End(xlToLeft).Column + 1
            With .Range(.Cells(9, 6), .Cells(31, lc))
                .UnMerge
                .ClearContents
                .Interior.ColorIndex = 2
                .Borders.LineStyle = xlNone
            End With
        End With
    End Sub
    

    【讨论】:

      【解决方案2】:

      试试

      Sub Remove()
      
      
          With Worksheets("Price calculation")
          'Do Until Columns(4)
              Do
                  lc = .Cells(9, .Columns.Count).End(xlToLeft).Column
      
                  If lc <= 4 Then Exit Do
      
                  .Range(.Cells(9, lc - 0), .Cells(9, lc)).MergeArea.UnMerge
                  lc = .Cells(11, .Columns.Count).End(xlToLeft).Column
                  .Range(.Cells(9, lc - 1), .Cells(31, lc)).ClearContents
                  .Range(.Cells(9, lc - 1), .Cells(31, lc)).Interior.ColorIndex = 2
                  .Range(.Cells(9, lc - 1), .Cells(31, lc)).Borders.LineStyle = xlNone
              Loop
          End With
      End Sub
      

      【讨论】:

      • 谢谢!这可行,但是每次按下按钮时我都需要运行代码(在单击 E 列之前不要删除所有内容)。一整天都在寻找解决方案=)
      • @user7202022,删除do循环
      【解决方案3】:

      您忘记了循环末尾的 Loop 词。

      Do Until condition
          [ statements ]
          [ Exit Do ]
          [ statements ]
      Loop
      
      Do
          [ statements ]
          [ Exit Do ]
          [ statements ]
      Loop Until condition
      

      根据你的情况:

      sub deleteCol()
          dim i as long
          i = 100 'index of last column, which u want to delete
          do until i = 4
              columns(i).delete
          loop 
      end sub
      

      【讨论】:

      • 感谢您的回复!如何将Do Until condition 设置为直到 4:th E Column?
      • i = 100 直到 i = 4 列 (i)。删除 Shift:=xlToLeft i = i-1 循环
      • 其中 i 是您要删除的最后一列的索引
      【解决方案4】:

      下面提供了一些指导方针,如何循环直到 E 列。代码循环列并将他的值 Test 从 A 列导入到 E 列。

      Option Explicit
      
          Sub test()
      
              Dim i As Long
      
              i = 1
      
              Do Until i = 6
                  Sheet1.Cells(1, i).Value = "Test"
      
              i = i + 1
      
              Loop
      
          End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 2018-03-14
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        相关资源
        最近更新 更多