我有一个包含许多列的大型电子表格。很多这些列都不会被使用,需要删除。当我在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
答案 0 :(得分: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