如何使用VBScript从文件1中删除不存在文件2的列?

时间:2019-06-28 20:46:01

标签: excel vbscript

文件1具有-日期,时间,时区,CustomerFirstName,CustomerLastName,CustomerAddress,电话号码,国家/地区,产品,订单号,CreationDate,BatchNumber

文件2具有-日期,时间,时区,国家/地区,订单号,批次号

从File1中删除不在File2中的列。 我想编程,以便如果文件2更改更多列,相同的代码应该工作。注意:“文件2列”列表始终是文件1的子集。

尝试比较两个Excel文件。

这是我的注释代码

Sub deleteIrrelevantColumns(strXl, strXlTemplate)
    Dim currentColumn
    Dim newCurrCol
    Dim colTemplate
    Dim colXlCount

    Set objExcel = CreateObject("Excel.Application")
    Set objXl = objExcel.Workbooks.Open(strXl)
    Set objXlTemplate = objExcel.Workbooks.Open(strXlTemplate)
    Set objXlTemplateWS =  objXlTemplate.Sheets(1)
    Set objXlWS = objXl.Sheets(1)
    objXlWS.Cells.EntireColumn.AutoFit
    objXlTemplateWS.Cells.EntireColumn.AutoFit

    objExcel.DisplayAlerts = False
    objExcel.ScreenUpdating = False

    colXlCount = objXlWS.UsedRange.Columns.Count 
    colTemplateCount = objXlTemplateWS.UsedRange.Columns.Count
    currentColumn = 1

    'Create an array of the size equal to column count
    ReDim columnHeading(colXlCount)

    'Copy the Column heading from Excel file to an array
    For i  = 0 To Ubound(columnHeading)
        columnHeading(i) = objXlWS.UsedRange.Cells(1, currentColumn).Value
        currentColumn = currentColumn + 1       
    Next
    'Logic to compare and remove columns
End Sub

1 个答案:

答案 0 :(得分:0)

我更改了一些原始代码并添加了注释。我还添加了如何比较和删除模板中未包含的列的逻辑。我敢肯定有更好的方法可以做到,但是我对方法进行了编码以便于理解。

希望有帮助。

Dim currentColumn
Dim newCurrCol
Dim colTemplate
Dim colXlCount
Dim strXl           As String
Dim strXlTemplate   As String

Set objExcel = CreateObject("Excel.Application")
Set objxl = objExcel.Workbooks.Open(strXl)
Set objXlTemplate = objExcel.Workbooks.Open(strXlTemplate)
Set objXlTemplateWS = objXlTemplate.Sheets(1)
Set objxlws = objxl.Sheets(1)
objxlws.Cells.EntireColumn.AutoFit
objXlTemplateWS.Cells.EntireColumn.AutoFit
objExcel.DisplayAlerts = False
objExcel.ScreenUpdating = False

colXlCount = objxlws.UsedRange.Columns.Count
colTemplateCount = objXlTemplateWS.UsedRange.Columns.Count

currentColumn = 1

'Create an array of the size equal to column count
'Note: Changed ReDim columnHeading(colXlCount) to ReDim columnHeading(colTemplateCount-1) _
I want to say you should save the Template Column Name into array so you can compare to the ActiveWorkBook

ReDim columnheading(colTemplateCount - 1)

'Copy the Column heading from Excel file to an array
For i = 0 To UBound(columnheading)
    columnheading(i) = objXlTemplateWS.UsedRange.Cells(1, currentColumn).Value
    currentColumn = currentColumn + 1
Next
'Logic to compare and remove columns

Dim IntCol      As Integer  'for Column reference
Dim EndCol      As Integer  'Last Column Number Reference
Dim BlnDel      As Boolean  'Indicator for whether should a column be deleted.

EndCol = objxlws.Cells(1, Columns.Count).End(xlToLeft).Column
For IntCol = EndCol To 1 Step -1

    BlnDel = True 'set default in the beginning of the loop

    For i = 0 To UBound(columnheading)
        If objxlws.Cells(1, IntCol) = columnheading(i) Then
            BlnDel = False      ' Set to False for any match column to avoid being deleted.
        End If
    Next
        If BlnDel Then
            'objxlws.Columns(IntCol).Delete                      'For Real
            Debug.Print objxlws.Cells(1, IntCol) & " Delete"     'For Test
        End If
Next

objxl.Close
objXlTemplate.Close