0

摘要:我的公司有两个不同的电子表格,每个电子表格都有许多政策。他们希望我通过策略 ID 匹配策略,并将所有旧笔记从旧电子表格转移到新电子表格。

推理:我的问题不是不了解如何做到这一点,而是做到这一点的最佳方法。自从加入 StackOverflow 以来,我一直被告知我应该做和不应该做的事情。我曾多次被告知最好使用For Each循环而不是简单的Do循环。另外,有人告诉我我不应该.Select大量使用(但我确实这样做了)。

我通常会怎么做:我通常只使用Do循环并浏览数据,只是选择使用.Find和使用的数据ActiveCell,当我想与当前行中的其他列交互时,我只会使用ActiveCell.Offset(). 我倾向于一直喜欢.Select并使用它,但是在这个项目中,我试图让自己跳出框框,也许会改变一些不好的编码习惯并开始使用可能更好的东西。

问题:ActiveCell.Offset()当我使用For Each循环时,我将如何去做相当于 an 的事情?

到目前为止我的代码: **欢迎提出问题/批评

Sub NoteTransfer()

    transferNotes

End Sub
Function transferNotes()

    Dim theColumn As Range
    Dim fromSheet As Worksheet
    Dim toSheet As Worksheet
    Dim cell As Range
    Dim lastRow As Integer

    Set fromSheet = Sheets("NotesFrom")
    Set toSheet = Sheets("NotesTo")

    With fromSheet                      'FINDING LAST ROW
        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
    End With

    Set theColumn = fromSheet.Range("B5:B" & lastRow)

    For Each cell In theColumn          'CODE FOR EACH CELL IN COLUMN

        If cell.Text = "" Then

            'do nothing

        Else

            With toSheet         'WANT TO FIND DATA ON THE toSheet

                Cells.find(What:=cell.Text, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate



            End With


        End If

    Next cell

End Function

例子

示例图片仅供参考

工作表底部

工作表的底部。

4

2 回答 2

2

首先,你的问题:

Question: How would I go about doing the equivalent of an ActiveCell.Offset() when I'm using a For Each loop?

考虑到您发布的代码,这没有多大意义。这是一个非常笼统的问题,需要一些上下文才能更好地理解。这真的取决于你的循环。如果您从 ActiveCell 循环连续范围的单元格,那么您可以说...

For each cel in Range
    myValue = ActiveCell.Offset(,i)
    i = i + 1
Next

获取循环中每个单元格旁边的列。但总的来说,我不会称之为伟大的编程。就像我说的,上下文很重要。

就您的代码而言,看看这是否有意义。我已经编辑和评论来帮助你一点。哦,是的,干得好,不使用Select

Sub transferNotes() '-> first no need for a function, because you are not returning anything...
                       'and no need to use a sub to call a sub here as you don't pass variables,
                       'and you don't have a process you are trying to run

    Dim theColumn As Range, cell As Range '-> just a little cleaner, INMHO
    Dim fromSheet As Worksheet, toSheet As Worksheet '-> just a little cleaner, INMHO
    Dim lastRow As Integer

    Set fromSheet = Sheets("NotesFrom")
    Set toSheet = Sheets("NotesTo")

    With fromSheet ' -> put everything you do in the "fromSheet" in your With block

        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row 'FINDING LAST ROW
        Set theColumn = .Range("B5:B" & lastRow)

        theColumn.AutoFilter 1, "<>"

        Set theColumn = theColumn.SpecialCells(xlCellTypeVisible) '-> now you are only looping through the cells are that are not blank, so it's more efficient

        For Each cell In theColumn

            '-> use of ActiveCell.Offset(), it's not ActiveCell.Offset(), but it uses Offset
            Dim myValue
            myValue = cell.Offset(, 1) '-> gets the cell value in the column to the right of the code


            'WANT TO FIND DATA ON THE toSheet
            toSheet.Cells.Find(What:=cell.Text, After:=ActiveCell, LookIn:=xlFormulas, _
                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate

        Next cell

    End With

End Sub
于 2012-09-24T20:36:47.067 回答
1

这是我目前的建议。

Function transferNotes()

    Dim SourceColumn As Range
    Dim fromSheet As Worksheet
    Dim toSheet As Worksheet
    Dim cell As Range
    Dim lastRow As Long '<--changed to Long

    Set fromSheet = Sheets("NotesFrom")
    Set toSheet = Sheets("NotesTo")

    With fromSheet                      'FINDING LAST ROW
        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
    End With

    Set SourceColumn = fromSheet.Range("B5:B" & lastRow)

    For Each cell In SourceColumn          'CODE FOR EACH CELL IN COLUMN
        If cell.Value = "" Then 'the .Text property can
                                'make for some confusing errors.
                                'Try to avoid it.
            'nothng to search for
        Else
            With toSheet         'WANT TO FIND DATA ON THE toSheet
                Dim destRng As Range

                Set destRng = .Range("A:A").Find(What:=cell.Value)
                If Not destRng Is Nothing Then
                    .Cells(destRng.Row, <your mapped column destination>)
                        = fromSheet.Cells(cell.Row,<your mapped column source>)
    ' you can either repeat the above line for all of your non-contiguous
    'sections of data you want to move from sheet to sheet
    '(i.e. if the two sheets are not arranged the same)
    'if the two sheets are aranged the same then change
    'the .cells call to call a range and include
    'the full width of columns
                Else
                    'nothing was found
                End If

            End With
        End If
    Next cell

End Function
于 2012-09-24T20:43:56.947 回答