英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论