2016-11-23 33 views
0

我試圖讓這個子程序工作。 (ws).Range(「E5」)。end(xldown).offset(1,0,0)如果數據不是0到循環並複製數據,我有一張數據(ws1) 0)VBA Two for Loop並將值收集到第二張

請幫


Sub Test() 

Dim r As Integer 
Dim c As Integer 
Dim amount As Long 
Dim account As Variant 
Dim Acct As Range 
Dim Amt As Range 
Dim ws1 As Worksheet 
Dim target As Range 
Dim ws As Worksheet 
Set ws1 = ActiveSheet 
Set ws = Worksheets("Updated") 
Set target = ws.Range("E5:E" & Range("E5").End(xlDown)).Select 

    ws1.Activate 

    Range("A8").Select 

    For r = 8 To ActiveCell.End(xlDown).Row 

    Cells(r, 1).Select 
    account = ActiveCell.Value 

    For c = 2 To ActiveCell.End(xlToRight).Column 

    ActiveCell.Offset(0, 1).Select   
    amount = ActiveCell.Value 

    If ActiveCell.Value <> 0 Then 

    target.Offset(1, 0).Value = amount  
    target.Range("E5").End(xlDown).Offset(0, 1) = account 

    End If 

    Next  
     Next 

    ActiveCell.Offset(1, 0).Select 

    End Sub 
+0

您有什麼問題? –

+0

我想將Amount和Account變量的值複製到表單ws中,因爲它會循環顯示錶單ws應該一個接一個地獲取這些值的所有值 –

+0

金額優先後跟帳戶? –

回答

0

試試這個:

Sub Test() 

Dim source As Worksheet, target As Worksheet, rng As Range, col As Integer, account As Range 

Set source = ActiveSheet 
Set target = Worksheets("Updated") 

For Each rng In source.Range("A8:A" & Range("A8").End(xlDown).Row) 

    Set account = rng 

    For col = 2 To rng.End(xlToRight).Column 

     If Cells(rng.Row, col) <> 0 Then 

      target.Range("E5").End(xlDown).Offset(1, 0) = Cells(rng.Row, col) 
      target.Range("E5").End(xlDown).Offset(0, 1) = account 

     End If 

    Next col 

Next rng 

End Sub 
+0

獲取應用程序定義或對象定義的錯誤。 –

+0

我有多個值在每個帳戶的金額 –

+0

哪一行給出錯誤?此外,我認爲每個帳戶都有多個值,您希望他們列出,一個在另一個之上? –

相關問題