VBA 单元格范围地址语法(查找 LastColumn 的特定值)
VBA Cells range address syntax (FIND specific value to LastColumn)
我对我遇到的错误有疑问,因为我似乎无法弄清楚。
我有:
With Worksheets("Machine Specification")
Range("C138", .Cells(Cells.Find("Norm. Current (A)").Row, LastColumn)).Value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End With
我需要更改为根据
中的值找到该行而不是 C138
的行
列 C
。所以在我看来,代码应该是:
With Worksheets("Machine Specification")
Cells(Cells.Find("Norm. Current (A)").Row, 3), .Cells(Cells.Find("Norm. Current (A)").Row, LastColumn)).Value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End With
还有VBA
returns一条红线。
我犯了什么错误,有人可以帮忙吗?
Range(.Cells(.Cells.Find("Norm. Current (A)").Row, 3), .Cells(.Cells.Find("Norm. Current (A)").Row, LastColumn)).value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
但看起来更简单:
iRow = .Cells.Find("Norm. Current (A)").Row
Range(.Cells(iRow, 3), .Cells(iRow, LastColumn)).value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
您的第二个示例将不起作用,因为 Cells
引用单个单元格,而 'Range' 可以引用一个或多个单元格。
我猜你这行代码应该写成:
.Range(.Cells(.Cells.Find("Norm. Current (A)").Row, 3), .Cells(.Cells.Find("Norm. Current (A)").Row, LastColumn)) = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
问题是,正如@SJR 评论的那样,您将可能一起失败的命令链接在一起。如果 sheet 上不存在“Norm. Current (A)”,您将收到一条错误消息。
此外,您的代码行仅部分使用了 With
命令。
Cells(Cells.Find("Norm. Current (A)").Row, 3), .Cells(Cells.Find
在上面的示例中,只有倒数第二个 Cells
查看 机器规格 sheet 因为它之前有 .
它。所有其他 Cells
正在查看当前处于活动状态的 sheet - 如果它不是正确的 sheet 你会得到一个错误,因为你无法构建一个看起来不同的范围参考sheets.
我会把代码写成:
Sub Test()
'Don't know where you get your LastColum figure from, so made it up.
Dim LastColumn As Long
LastColumn = 5
Dim NormCurrentA As Range
With Worksheets("Machine Specification")
'Find remembers the last settings you used, so best to set them all.
Set NormCurrentA = .Cells.Find( _
What:="Norm. Current (A)", _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)
'Check that something was found before trying to use it.
If Not NormCurrentA Is Nothing Then
'All Ranges and Cells must be preceded by a '.' to work with the With....End With command.
.Range(.Cells(NormCurrentA.Row, 3), .Cells(NormCurrentA.Row, LastColumn)) = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End If
End With
End Sub
ThisWorkbook.Worksheets("Machine Specification").Range(Cells(Cells.Find("Lower Film Width (mm)").Row, 3), Cells(Cells.Find("Lower Film Width (mm)").Row, LastColumn)).Select
我对我遇到的错误有疑问,因为我似乎无法弄清楚。 我有:
With Worksheets("Machine Specification")
Range("C138", .Cells(Cells.Find("Norm. Current (A)").Row, LastColumn)).Value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End With
我需要更改为根据
中的值找到该行而不是 C138
的行
列 C
。所以在我看来,代码应该是:
With Worksheets("Machine Specification")
Cells(Cells.Find("Norm. Current (A)").Row, 3), .Cells(Cells.Find("Norm. Current (A)").Row, LastColumn)).Value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End With
还有VBA
returns一条红线。
我犯了什么错误,有人可以帮忙吗?
Range(.Cells(.Cells.Find("Norm. Current (A)").Row, 3), .Cells(.Cells.Find("Norm. Current (A)").Row, LastColumn)).value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
但看起来更简单:
iRow = .Cells.Find("Norm. Current (A)").Row
Range(.Cells(iRow, 3), .Cells(iRow, LastColumn)).value = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
您的第二个示例将不起作用,因为 Cells
引用单个单元格,而 'Range' 可以引用一个或多个单元格。
我猜你这行代码应该写成:
.Range(.Cells(.Cells.Find("Norm. Current (A)").Row, 3), .Cells(.Cells.Find("Norm. Current (A)").Row, LastColumn)) = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
问题是,正如@SJR 评论的那样,您将可能一起失败的命令链接在一起。如果 sheet 上不存在“Norm. Current (A)”,您将收到一条错误消息。
此外,您的代码行仅部分使用了 With
命令。
Cells(Cells.Find("Norm. Current (A)").Row, 3), .Cells(Cells.Find
在上面的示例中,只有倒数第二个 Cells
查看 机器规格 sheet 因为它之前有 .
它。所有其他 Cells
正在查看当前处于活动状态的 sheet - 如果它不是正确的 sheet 你会得到一个错误,因为你无法构建一个看起来不同的范围参考sheets.
我会把代码写成:
Sub Test()
'Don't know where you get your LastColum figure from, so made it up.
Dim LastColumn As Long
LastColumn = 5
Dim NormCurrentA As Range
With Worksheets("Machine Specification")
'Find remembers the last settings you used, so best to set them all.
Set NormCurrentA = .Cells.Find( _
What:="Norm. Current (A)", _
After:=.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=True)
'Check that something was found before trying to use it.
If Not NormCurrentA Is Nothing Then
'All Ranges and Cells must be preceded by a '.' to work with the With....End With command.
.Range(.Cells(NormCurrentA.Row, 3), .Cells(NormCurrentA.Row, LastColumn)) = UserForm_ConnectionParameters.NormCurrent_ComboBox.Text
End If
End With
End Sub
ThisWorkbook.Worksheets("Machine Specification").Range(Cells(Cells.Find("Lower Film Width (mm)").Row, 3), Cells(Cells.Find("Lower Film Width (mm)").Row, LastColumn)).Select