2012-08-08 60 views
0

我每天都會從外部來源輸入數據。在一張紙上,我有一個股票代碼列表(按字母順序排列),並在該行中繼續顯示相應的數據。記錄宏(Excel 2003)按行有條件地複製粘貼

在另一張紙上,我有股票按其相應的部門組織,而不是按字母順序排列。

我試圖開發一個宏,使第一張工作表中的信息自動粘貼到第二張工作表中,方法是識別代碼並粘貼到相應的行中。

這裏是到目前爲止所使用的代碼,但它沒有工作打算的方式:

Dim LSymbol As String 
    Dim LRow As Integer 
    Dim LFound As Boolean 

    On Error GoTo Err_Execute 

    'Retrieve symbol value to search for 
    LSymbol = Sheets("Portfolio Update").Range("B4").Value 

    Sheets("Test").Select 

    'Start at row 2 
    LRow = 2 
    LFound = False 

    While LFound = False 

     'Encountered blank cell in column B, terminate search 
     If Len(Cells(2, LRow)) = 0 Then 
      MsgBox "No matching symbol was found." 
      Exit Sub 

     'Found match in column b 
     ElseIf Cells(2, LRow) = LSymbol Then 

      'Select values to copy from "Portfolio Update" sheet 
      Sheets("Portfolio Update").Select 
      Range("B5:V5").Select 
      Selection.Copy 

      'Paste onto "Test" sheet 
      Sheets("Test").Select 
      Cells(3, LRow).Select 
      Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _ 
      False, Transpose:=False 

      LFound = True 
      MsgBox "The data has been successfully copied." 

     'Continue searching 
     Else 
      LRow = LRow + 1 
     End If 

    Wend 

    On Error GoTo 0 

    Exit Sub 

Err_Execute: 
    MsgBox "An error occurred." 

End Sub 

感謝。

+0

它做什麼,而不是按照預期工作? – 2012-08-08 18:40:26

+2

應該是'.Cells(row,col)'不是'.Cells(col,row)' – 2012-08-08 18:54:18

+0

@TimWilliams:這是一個有效的答案;)(提示提示)@EBB:避免使用'.Select'看這個鏈接http ://stackoverflow.com/questions/10714251/excel-macro-avoiding-using-select – 2012-08-09 01:10:12

回答

0

應該是.Cells(row,col) not .Cells(列,行)`

但是,您可以通過使用find()避免循環 -

Sub Tester() 

    Dim LSymbol As String 

    Dim shtPU As Worksheet 
    Dim shtTest As Worksheet 
    Dim f As Range 
    Dim c As Range 

    Set shtPU = Sheets("Portfolio Update") 
    Set shtTest = Sheets("Test") 

    On Error GoTo Err_Execute 

    For Each c In shtPU.Range("B4:B50").Cells 

     LSymbol = c.Value 'Retrieve symbol value to search for 

     If Len(LSymbol) > 0 Then 
      Set f = shtTest.Columns(2).Find(LSymbol, , xlValues, xlWhole) 
      If Not f Is Nothing Then 
       'was found 
       With c.Offset(0, 1).Resize(1, 21) 
        f.Offset(0, 1).Resize(1, .Columns.Count) = .Value 
       End With 
       c.Interior.Color = vbGreen 
       'MsgBox "The data has been successfully copied." 
      Else 
       'not found 
       c.Interior.Color = vbRed 
       'MsgBox "No matching symbol was found." 
      End If 
     End If 

    Next c 

    Exit Sub 

Err_Execute: 
    MsgBox "An error occurred:" & Err.Description 

End Sub 

編輯 - 通過符號

列表添加循環
+0

蒂姆威廉斯,@SiddharthRout,真棒。感謝您的幫助和迅速回復。我現在無法訪問它,但是一旦我做了,我將使用您的建議,並讓您知道它是如何發生的。再次感謝。 – EBB 2012-08-09 16:16:17

+0

Tim Williams,@SiddharthRout,我能夠測試它,並且宏能夠工作,但它只搜索,複製和粘貼第一個代碼的數據到'測試'表中。我試圖讓宏查找所有40個代碼並將相應的數據粘貼到「測試」表中。如有任何幫助,我們將不勝感激。 – EBB 2012-08-16 12:07:23

+0

查看我編輯的代碼 – 2012-08-16 16:14:58