将变量设置为 Header 文本列
Set Variable To Header Text Column
我有一本从未收到过相同格式的工作簿。为了防止手动干预,我需要捕获文本 employee
所在的列。例如,如果文本在 O 列中 - 我将执行以下操作,但我需要 Cells(i,"O")
根据包含文本 employee
的单元格进行更改
Sub DoThis()
Application.ScreenUpdating = False
Dim i As Long
For i = Range("A" & Rows.Count).End(3).Row To 2 Step -1
If Not IsEmpty(Cells(i, "O").Value) Then
'stuff here
End If
Next i
End Sub
使用find
方法
Cells.Find("employee")
这将找到指定范围内的单元格(这里我使用 Cells
但我会将其缩小到您的范围)并且它将 return 包含文本的单元格"employee"。然后您可以将其作为 Range
对象引用,即使用 .Row
获取行号或使用 .Column
获取列号
您可以使用 Find
方法并获取找到 employee
的单元格的列以在 Cells
中使用:
Option Explicit
Sub DoThis()
Dim i As Long
Dim lngCol As Long
With Worksheets("Sheet1") '<-- change to your sheet
lngCol = .Rows(1).Find("employee").Column '<-- assumes header in Row 1
For i = .Range("A" & .Rows.Count).End(3).Row To 2 Step -1
If Not IsEmpty(.Cells(i, lngCol).Value) Then
'stuff here
End If
Next i
End With
End Sub
我有一本从未收到过相同格式的工作簿。为了防止手动干预,我需要捕获文本 employee
所在的列。例如,如果文本在 O 列中 - 我将执行以下操作,但我需要 Cells(i,"O")
根据包含文本 employee
Sub DoThis()
Application.ScreenUpdating = False
Dim i As Long
For i = Range("A" & Rows.Count).End(3).Row To 2 Step -1
If Not IsEmpty(Cells(i, "O").Value) Then
'stuff here
End If
Next i
End Sub
使用find
方法
Cells.Find("employee")
这将找到指定范围内的单元格(这里我使用 Cells
但我会将其缩小到您的范围)并且它将 return 包含文本的单元格"employee"。然后您可以将其作为 Range
对象引用,即使用 .Row
获取行号或使用 .Column
获取列号
您可以使用 Find
方法并获取找到 employee
的单元格的列以在 Cells
中使用:
Option Explicit
Sub DoThis()
Dim i As Long
Dim lngCol As Long
With Worksheets("Sheet1") '<-- change to your sheet
lngCol = .Rows(1).Find("employee").Column '<-- assumes header in Row 1
For i = .Range("A" & .Rows.Count).End(3).Row To 2 Step -1
If Not IsEmpty(.Cells(i, lngCol).Value) Then
'stuff here
End If
Next i
End With
End Sub