0
我有一个工作表,其中包含一个将被分配给宏的按钮的文本框。在此工作簿中有另一个工作表,它将主数据保存在表中。该文本框将有一个工作#。我想要做的是在主表中查找作业#列(列B /命名范围:“JobCol_Master”)中的作业#,如果找到匹配项,则在该行中,验证基于列D中的内容如果为true,则更改该行中特定列的单元格值。如果发现匹配,更改单元格值
我收到Range(Cells(cell, 11)).Value = "Test"
行上的类型不匹配错误。当值不匹配时,我也会遇到错误。
我很欣赏任何人都可以给我的指导。
Option Explicit
Sub IDCloseJob()
Dim ws As Worksheet
Dim MasterData As Range
Dim sourceID As Range
Dim cell As Range, row As Range, JobCol As Range
Dim Txt As String
Txt = ThisWorkbook.Worksheets("ID").TextBoxID.Text
Set MasterData = ThisWorkbook.Worksheets("Jobs").Range("MasterData")
If Txt <> "" Then
With MasterData
For Each cell In .Range("JobCol_Master")
'If job# matches textbox and if job# has value equal to "ID" in Column D then...
If cell.Text = Txt Then
Cells(cell.row, 11).Value = "Test"
Else
MsgBox "Job# not found."
End If
Next cell
End With
End If
End Sub
更新的代码:
Sub IDCloseJob()
Dim MasterData As Range
Dim sourceID As Range
Dim cell As Range, row As Range, JobCol As Range
Dim Txt As String
Txt = ThisWorkbook.Worksheets("ID").TextBoxID.Text
Set MasterData = ThisWorkbook.Worksheets("Jobs").Range("MasterData")
If Txt <> "" Then
With MasterData
For Each cell In .Range("JobCol_Master")
'If job# matches textbox and if job# has value equal to "ID" in Column D then...
If cell.Text = Txt Then
Cells(cell.row, 11).Value = "Test"
Else
MsgBox "Job not found."
Exit Sub
End If
Next cell
End With
End If
End Sub
上面的代码,如果真没有完成真正的声明Cells(cell.row, 11).Value = "Test"
。它调出MsgBox
然后退出子。不知道为什么...代码看起来不错。如果我忽略了错误的陈述,就完成了我的真实陈述。
非常感谢你。 – DigitalSea
一个问题。它将值“Test”放在匹配列下面的行中。我可以通过放置一个-1 .Cells(cell.row - 1,11).Value =“Test”来抵消这一点,但是这会否定匹配规则并在匹配的行内工作。 – DigitalSea
对不起,但我没有得到你:你是什么意思_“匹配列下面的行”_?一行不能列在列...如果你给更多的信息,我可以尝试和帮助你。顺便说一句,只要你的_original_问题(_“我在Range Range(Cells(cell,11))上得到了一个类型不匹配的错误。Value =”Test“”_)我的答案解决了它,所以请将答案标记为接受。谢谢! – user3598756