0

多くの列を持つ大きなスプレッドシートがあります。これらの列の多くは使用されないため、削除する必要があります。VBA で手順を作成したときに、リストされている列を除くすべての列を削除する手順を作成しました。VB.net でも同じことができますか?

私はウェブを研究し、列A、列Bなどを使用してそれを行う方法を見ました。これはエクスポート ファイルであり、列の順序が毎回変わるため、この方法では実行できません。唯一の定数は列名で、変更されません。

以下は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
4

1 に答える 1

0

私はこれを行う方法を理解することができました:

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
于 2013-09-13T04:08:24.703 に答える