在另一个子程序中删除Excel列?

时间:2013-02-26 15:41:38

标签: excel vba function call

我有一个宏,它连接两个相邻列中的值。它完美地运作。我想添加一个命令,然后在连接完成后删除第二列。我可以从现有的子路由/宏中执行此操作,还是需要调用单独的函数?这就是我所拥有的:

Option Explicit
Sub Doc1_Doc2_Merge()

Dim CurrCol As Integer
Dim NewValue As String

CurrCol = 1

While Cells(1, CurrCol).Address <> "$JL$1"

    'MsgBox Cells(1, CurrCol).Address & " " & Cells(1, CurrCol).Value
    If InStr(Cells(1, CurrCol).Value, "Doc1") > 0 Then
        ' look at next cell
        If InStr(Cells(1, CurrCol + 1).Value, "Doc2") > 0 Then
            If Trim(Cells(2, CurrCol + 1).Value) <> "" Then
                NewValue = Cells(2, CurrCol).Value & ", " & Cells(2, CurrCol + 1)
                'MsgBox "New Value is " & NewValue
                Cells(2, CurrCol).Value = NewValue
            End If
        End If

    End If
    'now delte currCol+1

    'This is the deletion part that isn't working
    Column(CurrCol + 1).Select
    Selection.Delete Shift:=xlToLeft

    'Advance the counter
    CurrCol = CurrCol + 1
Wend

End Sub

2 个答案:

答案 0 :(得分:1)

替换

Column(CurrCol + 1).Select

Range(Columns(CurrCol + 1), Columns(CurrCol + 1)).Select

答案 1 :(得分:0)

尝试使用此代替选择/删除行:

  Columns(CurrCol + 1).EntireColumn.Delete