在不同的工作表中查找一些值

时间:2014-03-11 14:22:38

标签: excel vba excel-vba

我试图读取一张表格中的每一行(Get_Command)并在另一张表格中查找第一列中的值(Command_List);如果此值在Command_List中,我想将行(删除一些列)复制到第三个表(Set_Command)。

Sub Macro1()
'
' Macro1 Macro
'
Dim fnFormat As Range
Dim c As Long
Dim MyCol As Long
Dim fCommand As Range
Dim Command As String

With Sheets("Get_Command")
.Select

 Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

For Lrow = 1 To Lastrow Step 1

Command = Cells(Lrow, 1).Value

    Set fCommand = Worksheets("Command_List").Columns("A:A").Find(Command, LookIn:=xlValues)

        If Not fCommand Is Nothing Then

            Lastcolumn = .Cells(Lrow, .Columns.Count).End(xlToLeft).Column

                Range("A" & Lrow).Select
                Selection.Copy
                Sheets("Set_Command").Select
                Range("A" & Lrow).Select
                ActiveSheet.Paste
                ActiveSheet.Columns("A").Replace What:="Get:", Replacement:="Set", LookAt:=xlPart, SearchOrder:=xlByColumns
                Application.CutCopyMode = False

                Sheets("Get_Command").Select

                Set fnFormat = Range(Cells(Lrow, 5), Cells(Lrow, Lastcolumn)).Find("nFormat", LookIn:=xlValues)

                    If fnFormat Is Nothing Then 'If it is not found

                        c = 1

                        For Lcolumn = 5 To Lastcolumn Step 2

                            Cells(Lrow, Lcolumn).Select
                            Selection.Copy
                            Sheets("Set_Command").Select
                            c = c + 1
                            Cells(Lrow, c).Select
                            ActiveSheet.Paste
                            Application.CutCopyMode = False
                            Sheets("Get_Command").Select

                        Next Lcolumn

                    Else

                        c = 1

                        'It should remove "(", ")", "," and the columns that a don't need

                        For Lcolumn = 5 To fnFormat.Column - 3 Step 2
                            Cells(Lrow, Lcolumn).Select
                            Selection.Copy
                            Sheets("Set_Command").Select
                            c = c + 1
                            Cells(Lrow, c).Select
                            ActiveSheet.Paste
                            Application.CutCopyMode = False
                            Sheets("Get_Command").Select

                        Next Lcolumn

                        For Lcolumn = fnFormat.Column + 3 To Lastcolumn Step 2
                            Cells(Lrow, Lcolumn).Select
                            Selection.Copy
                            Sheets("Set_Command").Select
                            c = c + 1
                            Cells(Lrow, c).Select
                            ActiveSheet.Paste
                            Application.CutCopyMode = False
                            Sheets("Get_Command").Select

                        Next Lcolumn

                    End If

        End If

Next Lrow

End With

End Sub

问题在于:

Command = Cells(Lrow, 1).Value

    Set fCommand = Worksheets("Command_List").Columns("A:A").Find(Command, LookIn:=xlValues)

Command正在保存我想要的东西,但fCommand总是返回Nothing。

有人可以帮我找到我的错误吗?

谢谢! =)

1 个答案:

答案 0 :(得分:0)

如果找不到匹配单元格,则返回Nothing。因此,您使用的数据存在问题。问题的潜在来源:

  • 您的命令或可能的命令列表中有前导或尾随空格
  • 您在Find
  • 中引用了错误的范围