在VBA中在现有列之间插入新列

huangapple go评论124阅读模式
英文:

Inserting a New Column Between Existing Columns in VBA

问题

在VBA中在现有列之间插入新列

我有一个需求,需要在Excel中使用VBA在两个现有列之间插入一个新列。例如,如果我有列A、B和C,我想在B和C之间添加一个新列,结果是列A、B、[新列]、C [旧B]、D [旧C]。

我尝试使用Columns(targetColumn).Insert Shift:=xlToRight的方法,但它会覆盖现有列而不是插入新列。以下是我一直在使用的代码:

Public Sub LastColumn()
    Dim hLink As Hyperlink
    Dim targetColumn As Long
    
    targetColumn = InputBox("Enter the number of the column to which you want to add the new column:", "Add new column")
    
    ThisWorkbook.Sheets("Holiday plans 2023_Team").Activate
    With ThisWorkbook.Sheets("Holiday plans 2023_Team")
        Dim LastCol As Long
        LastCol = targetColumn - 1
        Columns(targetColumn).Insert Shift:=xlToRight
        Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With
    End With
    
    ThisWorkbook.Sheets("Holiday plans 2023_Manager").Activate
    With ThisWorkbook.Sheets("Holiday plans 2023_Manager")
        LastCol = targetColumn - 1
        
        Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
            .Replace What:="!D", Replacement:="!C", LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With
    End With
End Sub

我一直在使用以上代码尝试在两个现有列之间插入新列。然而,这段代码目前正在覆盖现有列而不是插入新列。您知道为什么这不起作用吗?

非常感谢您的帮助!

英文:

Inserting a New Column Between Existing Columns in VBA

I have a requirement to insert a new column between two existing columns using VBA in Excel. For example, if I have columns A, B, and C, I want to add a new column between B and C, resulting in columns A, B, [new column], C [old B], D [old C].

I've tried using the Columns(targetColumn).Insert Shift:=xlToRight approach, but it overwrites the existing column instead of inserting a new one. Here's the code I've been using:

Public Sub LastColumn()
    Dim hLink As Hyperlink
    Dim targetColumn As Long
    
    targetColumn = InputBox("Enter the number of the column to which you want to add the new column:", "Add new column")
    
    ThisWorkbook.Sheets("Holiday plans 2023_Team").Activate
    With ThisWorkbook.Sheets("Holiday plans 2023_Team")
        Dim LastCol As Long
        LastCol = targetColumn - 1
        Columns(targetColumn).Insert Shift:=xlToRight
        Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With
    End With
    
    ThisWorkbook.Sheets("Holiday plans 2023_Manager").Activate
    With ThisWorkbook.Sheets("Holiday plans 2023_Manager")
        LastCol = targetColumn - 1
        
        Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
            .Replace What:="!D", Replacement:="!C", LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With
    End With
End Sub

I've been using the above code to attempt inserting a new column between two existing columns. However, the code is currently overwriting the existing column instead of inserting a new one. Do you have any idea why this is not working?

Thank you in advance for your help!

答案1

得分: 1

我认为这样会没问题:

 ThisWorkbook.Sheets("2023年度假期计划_Team").Activate
    With ThisWorkbook.Sheets("2023年度假期计划_Team")
        Dim LastCol As Long
        LastCol = targetColumn - 1
        .Columns(targetColumn).Insert Shift:=xlToRight
        .Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        .Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With

不带点属性会引用到活动工作表或活动工作簿。

英文:

I think this will be fine:

 ThisWorkbook.Sheets("Holiday plans 2023_Team").Activate
    With ThisWorkbook.Sheets("Holiday plans 2023_Team")
        Dim LastCol As Long
        LastCol = targetColumn - 1
        .Columns(targetColumn).Insert Shift:=xlToRight
        .Columns(LastCol).Copy Destination:=Cells(1, targetColumn)
        .Cells(15, targetColumn).Value = ActiveWorkbook.Worksheets(Sheets("BLANK1").Index - 1).Name
        With .Columns(targetColumn)
            .Replace What:=Cells(15, targetColumn - 1).Value, Replacement:=Cells(15, targetColumn).Value, LookAt:=xlPart, _
                     SearchOrder:=xlByRows, MatchCase:=False, _
                     SearchFormat:=False, ReplaceFormat:=False
        End With

Without the dot properties refer to the Activesheet or ActiveWorkbook accordingly.

huangapple
  • 本文由 发表于 2023年6月5日 15:29:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定