简体   繁体   English

使用vb.net在excel中按名称删除多列

[英]delete multiple columns by name in excel using vb.net

I have a large spreadsheet with many columns. 我有一个包含许多列的大型电子表格。 A lot of those columns will not be used and need to be deleted. 其中许多列将不使用,需要删除。 When I wrote the procedure in VBA, I wrote a procedure that deleted all columns except for the ones listed. 当我在VBA中编写该过程时,我编写了一个过程,该过程删除了除列出的列以外的所有列。 Can I do the same in VB.net? 我可以在VB.net中做同样的事情吗?

I researched the web and saw how to do it by using column A, Column B and so on. 我研究了网络,并看到了如何使用A列,B列等进行操作。 I cannot do it this way because this is an export file and the column order changes every time. 我无法这样做,因为这是导出文件,并且列顺序每次都会更改。 The only constant is the column names, those do not change. 唯一的常数是列名称,这些名称不会更改。

Here is what I wrote in VBA. 这是我在VBA中编写的内容。

    Sub prepareExports()

'This is Step 3 when the frmDataImportSplash is activated.

'This procedure deletes the first 7 rows of the sheets and
'deletes all columns that won't be used during the process.

Application.ScreenUpdating = False


For Each WS In Sheets(Array("byEmployee", "byPosition"))

With WS



    k = .UsedRange.Columns.Count

    'For the referenced sheets above delete all columns except for the ones listed below.  These columns the reference column for the entire workbook.

    For i = k To 1 Step -1
        Select Case LCase$(.UsedRange.Cells(1, i))
           Case "#ees", "annu. base at target", "annualized fte base pay", "annu. base mkt - 10th", "annu. base mkt - 25th", "annu. base mkt - 50th", "annu. base mkt - 75th", _
            "annu. base mkt - 90th", "annual base max", "annual base mid", "annual base min", "annualized base max", "annualized base mid", "annualized base min", _
            "annualized compa-ratio", "annualized range penetration", "employee dept", "employee id", "employee name", _
            "functional area", "hourly at target", "hourly base max", "hourly base mid", "hourly base min", "hourly compa-ratio", "hourly mkt - 10th", _
            "hourly mkt - 25th", "hourly mkt - 50th", "hourly mkt - 75th", "hourly mkt - 90th", "hourly range penetration", "hourly rate", "job code", _
            "market percentile of annu. base", "market percentile of hourly rate", "market percentile of salary mid", _
            "mid to annu. target delta | %", "mid to hourly target delta | %", "internal title", "salary", "salary at target", "salary compa - ratio", _
            "salary range penetration", "target market-ratio", "market percentile of annu. base", "Annu. Base Delta $ | %", "exemption", _
            "salary mkt - 10th", "salary mkt - 25th", "salary mkt - 50th", "salary mkt - 75th", "salary mkt - 90th", "salary at target", "mid to salaried target delta | %", _
            "tcc mkt - 10th", "tcc mkt - 25th", "tcc mkt - 50th", "tcc mkt - 75th", "tcc mkt - 90th", "grade"


                    'do nothing
            Case Else
                .UsedRange.Columns(i).Delete

                Application.ScreenUpdating = True

        End Select
    Next i
  End With
Next WS

End Sub

I was able to figure out how to do this: 我能够弄清楚如何做到这一点:

Dim xlWB As Excel.Workbook = CType(Globals.ThisWorkbook.Application.ActiveWorkbook, Excel.Workbook)
Dim xlWSPosition As Excel.Worksheet = CType(CType(xlWB.Sheets("byPosition"), Excel.Worksheet), Excel.Worksheet)
Dim xlWSEmployee As Excel.Worksheet = CType(CType(xlWB.Sheets("byEmployee"), Excel.Worksheet), Excel.Worksheet)      

Dim xlSheets As Object
        Dim xlSheetsArray(0 To 1) As Excel.Worksheet
        Dim k As Long
        Dim i As Long

        xlSheetsArray(0) = xlWSPosition
        xlSheetsArray(1) = xlWSEmployee


        For Each xlSheets In xlSheetsArray

            With xlSheets


                'With the above sheets, replace the first 7 rows. They only contain Payscale generic information so we can get rid of it.
                .Rows("1:7").Delete(Excel.XlDirection.xlUp)


                k = .UsedRange.Columns.Count

                'For the referenced sheets above delete all columns except for the ones liste below.  These columns the reference column for the entire workbook.

                For i = k To 1 Step -1
                    Select Case LCase(.UsedRange.Cells(1, i).Value)

                        'Keep these columns
                        Case "#ees", "annu. base at target", "annualized fte base pay", "annu. base mkt - 10th", "annu. base mkt - 25th", "annu. base mkt - 50th", "annu. base mkt - 75th", _
                         "annu. base mkt - 90th", "annual base max", "annual base mid", "annual base min", "annualized base max", "annualized base mid", "annualized base min", _
                         "annualized compa-ratio", "annualized range penetration", "employee dept", "employee id", "employee name", _
                         "functional area", "hourly at target", "hourly base max", "hourly base mid", "hourly base min", "hourly compa-ratio", "hourly mkt - 10th", _
                         "hourly mkt - 25th", "hourly mkt - 50th", "hourly mkt - 75th", "hourly mkt - 90th", "hourly range penetration", "hourly rate", "job code", _
                         "market percentile of annu. base", "market percentile of hourly rate", "market percentile of salary mid", _
                         "mid to annu. target delta | %", "mid to hourly target delta | %", "internal title", "salary", "salary at target", "salary compa - ratio", _
                         "salary range penetration", "target market-ratio", "market percentile of annu. base", "Annu. Base Delta $ | %", "exemption", _
                         "salary mkt - 10th", "salary mkt - 25th", "salary mkt - 50th", "salary mkt - 75th", "salary mkt - 90th", "salary at target", "mid to salaried target delta | %", _
                         "tcc mkt - 10th", "tcc mkt - 25th", "tcc mkt - 50th", "tcc mkt - 75th", "tcc mkt - 90th", "grade"

                        Case Else

                            'Delete all others not listed above
                            .UsedRange.Columns(i).Delete()

                    End Select
                Next i

            End With

        Next xlSheets

    End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM