Excel加载数据按钮

时间:2017-09-08 14:23:27

标签: excel vba excel-vba

我有一个工作表(存储的工作),我有一个宏将保存所有作业数据。我现在正在尝试创建一个将重新加载已保存数据的宏基于在MHBD工作表上输入F9的作业编号,作业编号位于B列。我需要将f作为作业所在的行号返回这样我就可以将该行的数据复制到MHBD表上的不同单元格中。我不能直接复制行,因为MHBD表上的数据不是分散的。如何让它返回作业所在的行并使用它来复制该行的单元格?

Sub Load_Button()

Dim StoredJobs As Worksheet
Dim MHBD As Worksheet
Dim lastRow As Long
Dim f As Range, theJob

Set StoredJobs = Worksheets("Stored Jobs")
Set MHBD = Worksheets("Manual Hour Break Down")
lastRow = StoredJobs.Range("B" & Rows.Count).End(xlUp).Row

theJob = MHBD.Range("F9").Value

'find the current job if it exists
Set f = StoredJobs.Range("B4:B" & lastRow).Find(what:=theJob, lookat:=xlWhole).Row

'if not found, use the next empty row
If f Is Nothing Then MsgBox "Job has not been entered."

End Sub

1 个答案:

答案 0 :(得分:0)

知道了。 @johnColeman你删除.Row

是正确的

感谢其他人的想法和帮助。

Sub Load_Button()

Dim StoredJobs As Worksheet
Dim MHBD As Worksheet
Dim lastRow As Long
Dim f As Range, theJob

Set StoredJobs = Worksheets("Stored Jobs")
Set MHBD = Worksheets("Manual Hours Break Down")
lastRow = StoredJobs.Range("B" & Rows.Count).End(xlUp).Row

theJob = MHBD.Range("F9").Value

'find the current job if it exists
Set f = StoredJobs.Range("B4:B" & lastRow).Find(what:=theJob, lookat:=xlWhole)

'if not found, error message
If f Is Nothing Then
    MsgBox "Job: " & MHBD.Range("F9").Value & " has not been entered" & vbNewLine & "Try the Quote Number"

'if found, copy data
Else
    StoredJobs.Range("C" & (f.Row)).Copy
    MHBD.Range("B3").PasteSpecial xlPasteValues

End If

End Sub