在另一个子例程中删除Excel列



我有一个宏,它将两个相邻列中的值连接起来。它工作得很好。我想添加一个命令,然后在连接完成后删除第二列。我可以从现有的子例程/宏内部执行此操作吗?还是需要调用一个单独的函数?这是我所拥有的:

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

更换

Column(CurrCol + 1).Select

带有

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

用这个代替选择/删除行:

  Columns(CurrCol + 1).EntireColumn.Delete 

最新更新