问题描述
我有一个带有锁定单元格的Excel工作表。一些单元格被解锁为输入值。有很多值要输入,因此我试图编写一个宏,该宏将:在活动工作表中选择未锁定的单元格,并用Vlookup公式填充所选单元格,该公式根据第一列和其中的相关行查找输入值一张纸上的同一张纸。我尝试了以下方法:
Sub SelectUnlockedCells()
'Update 20130830
Dim WorkRng As Range
Dim OutRng As Range
Dim Rng As Range
On Error Resume Next
Set WorkRng = Application.ActiveSheet.UsedRange
Application.ScreenUpdating = False
For Each Rng In WorkRng
If Rng.Locked = False Then
If OutRng.Count = 0 Then
Set OutRng = Rng
Else
Set OutRng = Union(OutRng,Rng)
End If
End If
Next
If OutRng.Count > 0 Then OutRng = Application.WorksheetFunction.VLookup("A" & ActiveRow.Value,Worksheets(2).Columns("A:C").Select,Worksheets(2).Columns(3).Select,False)
Application.ScreenUpdating = True
End Sub
我知道我的问题出现在Vlookup工作表函数的最后4行中,因为如果我说:
If OutRng.Count > 0 Then OutRng = 1 + 1
未正确输入未锁定的输入单元格为2。因此,我怀疑我的Vlookup对象选择不正确。
任何帮助都会很棒,谢谢!
解决方法
您根本没有在这些单元格中添加公式,而是试图在其中添加值,并且您所拥有的语法还是完全错误的。试试:
If OutRng.Count > 0 Then OutRng.FormulaR1C1 = "=VLookup(RC1,'" & worksheets(2).name & "'!C1:C3,3,FALSE)"
,
我发现一个简单的解决方案是我所怀疑的-vlookup内部的对象引用是错误的。同样,在这种情况下,应用程序工作表功能是多余的。最后第三行的解决方案是:
class DataBindingListAdapter<T>(@LayoutRes private val itemLayout: Int,diffCallback: DiffUtil.ItemCallback<T>) : ListAdapter<T,DataBindingListAdapter<T>.ViewHolder>(diffCallback) {
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater,itemLayout,parent,false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder,position: Int) {
holder.bind(getItem(position))
}
inner class ViewHolder(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(model: T?) {
binding.setVariable(BR.model,model)
binding.executePendingBindings()
}
}
}