我可以根据列号将选定的列从一个工作表复制到另一个工作表。但是有一天我可能会决定在源文件的中间某处添加一列。如果我根据列名复制列,这将不是问题。以下是我的代码。注释部分是根据列号完成实际复制的地方,我希望将其替换为列标签。列标签让我们说是Price Number
、House Price
和:Address
Cost
Sub CommercialView()
Dim wrkbk, sourceBk As Workbook
Set sourceBk = Application.ActiveWorkbook
'Clear Filter for all Columns START
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
'Clear Filter from all Columns END
'Copy the required columns and add them to the destination spreadsheet START
Workbooks.Add
Set wrkbk = Application.ActiveWorkbook
sourceBk.Activate
wrkbk.Activate
sourceBk.Activate
Range("A1,B1,C1,D1,E1,G1,H1,I1,R1,V1,W1,X1").EntireColumn.Select 'BASED ON COLUMN NO.
Selection.Copy
Range("A2").Select
wrkbk.Activate
ActiveSheet.Paste
Selection.AutoFilter
'Copy the required columns and add them to the destination spreadsheet END
'To remove data validation START
Cells.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateInputOnly, AlertStyle:=xlValidAlertStop, Operator _
:=xlBetween
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
'To remove data validation END
wrkbk.Activate
wrkbk.Sheets("Sheet1").Select
ActiveSheet.Range("$A$1:$L$4000").AutoFilter Field:=10, Criteria1:= _
"Completed - Requires Review from Pricing"
'Copy the Status Definitions tab to the new worksheet START
sourceBk.Sheets("2. Status Definitions").Copy _
after:=ActiveWorkbook.Sheets("Sheet1")
'Copy the Status Definitions tab to the new worksheet END
wrkbk.Sheets("Sheet1").Select
Range("A5").Select
ActiveWorkbook.SaveAs ("C:\Users\test\Desktop\DOD\Change Status Request Report\Commercial View\Internal Change Status Request Report - Commercial View - " & Format(Now, "yyyy-mm-dd"))
ActiveWorkbook.Close
End Sub