[英]Find Column Header By Name And Select All Data Below Column Header (Excel-VBA)
I'm attempting to create a macro to do the following:我正在尝试创建一个宏来执行以下操作:
For Example:例如:
Visual Spreadsheet Example:可视化电子表格示例:
I've discovered the following code online:我在网上发现了以下代码:
With ActiveSheet.UsedRange
Set c = .Find("Employee ID", LookIn:=xlValues)
If Not c Is Nothing Then
ActiveSheet.Range(c.Address).Offset(1, 0).Select
End If
End With
However, I'm still experiencing some issues.但是,我仍然遇到一些问题。
It is good to avoid looping through all cells.最好避免遍历所有单元格。 If the data set grows the macro can become too slow.
如果数据集增长,宏可能会变得太慢。 Using special cells and paste special operation of multiplying by 1 is an efficient way of accomplishing the task.
使用特殊的单元格和粘贴特殊的乘以 1 的操作是完成任务的有效方法。
This works...这工作...
Dim SelRange As Range
Dim ColNum As Integer
Dim CWS As Worksheet, TmpWS As Worksheet
'Find the column number where the column header is
Set CWS = ActiveSheet
ColNum = Application.WorksheetFunction.Match("Employee ID", CWS.Rows(1), 0)
'Set the column range to work with
Set SelRange = CWS.Columns(ColNum)
'Add a worksheet to put '1' onto the clipboard, ensures no issues on activesheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set TmpWS = ThisWorkbook.Worksheets.Add
With TmpWS
.Cells(1, 1) = 1
.Cells(1, 1).Copy
End With
'Select none blank cells using special cells...much faster than looping through all cells
Set SelRange = SelRange.SpecialCells(xlCellTypeConstants, 23)
SelRange.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply
TmpWS.Delete
CWS.Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
我只是偶然发现了这一点,对我来说答案非常简单,无论如何如果您正在处理ListObject那么这是要走的路:
YOURLISTOBJECT.HeaderRowRange.Cells.Find("A_VALUE").Column
Try this out.试试这个。 Simply add all the column header names you want to find into the collection.
只需将您要查找的所有列标题名称添加到集合中。 I'm assuming you don't have more than 200 columns, if you do simply update the for i = 1 to 200 section to a larger number.
我假设您没有超过 200 列,如果您只是将 for i = 1 到 200 部分更新为更大的数字。
Public Sub FindAndConvert()
Dim i As Integer
Dim lastRow As Long
Dim myRng As Range
Dim mycell As Range
Dim MyColl As Collection
Dim myIterator As Variant
Set MyColl = New Collection
MyColl.Add "Some Value"
MyColl.Add "Another Value"
lastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 1 To 200
For Each myIterator In MyColl
If Cells(1, i) = myIterator Then
Set myRng = Range(Cells(2, i), Cells(lastRow, i))
For Each mycell In myRng
mycell.Value = Val(mycell.Value)
Next
End If
Next
Next
End Sub
Ok, here's a brief way of achieving your goal.好的,这是实现目标的简要方法。 First, locate the column that holds the Employee IDs.
首先,找到保存员工 ID 的列。 Then simply set the entire Column to be formatted as Number instead of Text?
然后只需将整个列设置为数字而不是文本格式?
With Worksheets(1) ' Change this sheet to the one you are using if not the first sheet
Set c = .Find("Employee ID", LookIn:=xlValues)
If Not c Is Nothing Then
' The column we want is c's Column.
Columns(c.Column).NumberFormat = 0
End If
End With
Add a dim for the range that you want:为您想要的范围添加一个暗淡:
Dim MyRng, RngStart, RngEnd as Range
Then change:然后改变:
ActiveSheet.Range(c.Address).Offset(1, 0).Select
to the below so that all data in that column is found.到下面,以便找到该列中的所有数据。
set RngStart = ActiveSheet.Cells(1, c.column)
set RngEnd = ActiveSheet.Cells(rows.count, c.column).end(xlup)
set MyRng = ActiveSheet.Range(RngStart & ":" & RngEnd)
Now you can play about with the data.现在你可以玩弄数据了。 If you want to paste this somewhere which is formatted as number:
如果要将其粘贴到格式为数字的某个位置:
MyRng.copy
Sheets("Wherever").Range("Wherever").pastespecial xlvalues
If you want to change the format of the cells you have now found ( How to format column to number format in Excel sheet? ) that is whole number format, if you want decimal points then use "number" instead of "0":如果要更改现在找到的单元格的格式( 如何在 Excel 工作表中将列格式化为数字格式? ),即整数格式,如果要小数点,请使用“数字”而不是“0”:
MyRng.NumberFormat = "0"
or the new destination:或新目的地:
Sheets("Wherever").Range("Wherever").NumberFormat = "0"
General formatting which matches exactly the convert to number function:与转换为数字函数完全匹配的一般格式:
MyRng.NumberFormat = "General"
MyRng.Value = MyRng.Value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.