2017-02-09 131 views
-1

兩行我是一個VBA初學者和爲此,下面的查詢可能是你很容易,但我不能讓它複製粘貼下面的宏

什麼我想是這樣的:

構建在一張紙上(現有的)和

  • 副本

    1. 輸入與日期和信用評級一個宏並將其粘貼到一個預定表,並
    2. 每次喲你在輸入欄輸入新數據並點擊宏按鈕,宏應該複製並粘貼到表中,但是在最後使用的單元/行下面有兩行。
    3. 有一個圖表,用於讀取數據粘貼到的範圍數據,並自動將其範圍調整爲新粘貼的值。

    我希望,我已經解釋好我的問題了,以便您可以提供幫助。

    Dim x As Integer 
    Worksheets("Input").Range("D6:D7").Copy 
    x = 2 
    Do 
        x = x + 2 
        Worksheets("Chart").Range("B" & x).PasteSpecial Paste:=xlPasteValues,Transpose:=True, xlPasteValues 
    Loop Until x = 56 
    Application.CutCopyMode = False 
    End Sub 
    

    謝謝!

  • +1

    首先啓動某些功能,顯示不起作用的代碼,我們將幫助您使其工作。 – frenchie

    +0

    這隻會粘貼我的輸入值多次,每隔一行直到循環結束。然而,我想要的是添加新的輸入值,每按一下按鈕,最後一行下面兩行: [code] Dim x As Integer 工作表(「輸入」)。範圍(「D6:D7」)。 X = 2 執行 X = X + 2個 工作表( 「圖」)範圍( 「B」 &X).PasteSpecial粘貼:= xlPasteValues,移調:=真,xlPasteValues 循環,直到X = 56 應用。 CutCopyMode = False End Sub [code] –

    +0

    現在我把這段代碼放在你的問題中,但是將來代碼更容易閱讀。正如你所看到的,它在評論中不可讀。 – Rdster

    回答

    0

    嘗試下面的代碼:

    Option Explicit 
    
    Sub CopyTowRowsbelow() 
    
    Dim x As Long 
    
    Worksheets("Input").Range("D6:D7").Copy 
    
    With Worksheets("Chart") 
        x = .Cells(.Rows.Count, "B").End(xlUp).Row ' find last row in column B 
        .Range("B" & x + 2).PasteSpecial Paste:=xlPasteValues, Transpose:=True ' Paste the value 2 rows below the last cel with data 
    End With 
    Application.CutCopyMode = False 
    
    End Sub 
    
    +0

    hi shai rado。謝謝你的評論。我認爲你誤解了,我不想找到列的最後一行,而是列中的最後一行有一個值,然後在下一行粘貼下一個值。 –

    +0

    @ jules_05這正是我的代碼,它發現列B中最後一行的值(你測試了我的代碼?) –

    +0

    我做到了。謝謝。 我的問題是,我的目標是放置值的位置,每隔一行就有一個值。然後在B2:C2開始的第二行中發佈輸入的值(每次點擊按鈕),然後通過下一次單擊按鈕B4:C4,然後B6:C6等... 謝謝提前.. –

    0

    我找到一份粘貼到通常是一個不好的做法,因爲它使用的系統剪貼板,可以擦除數據,用戶必須在剪貼板。相反,我嘗試顯式設置目標值。如果將代碼粘貼到輸入工作表的代碼模塊中,此代碼應該可以工作。

    Public Sub Worksheet_Change(ByVal Target As Range) 
    'this built in subroutine in excel executes whenever something is changed on the worksheet 
    'we'll use this subroutine to determine range "D6:D7" was part of the sheet that was changed 
    
    'declare the range we want to detect if a change occurred, we'll just monitor range D7 
    Dim rangeMonitorForChange As Range 
    Set rangeMonitorForChange = Worksheets("Input").Range("D7") 
    
    'if the range changed on the sheet and the range were monitoring overlap/intersect then we'll call the method to upadte the chart sheet 
        If Not Application.Intersect(Target, rangeMonitorForChange) Is Nothing Then 
         copyDown 
        End If 
    End Sub 
    
    
    Sub copyDown() 
    'define where to "copy" from 
    Dim rngSource As Range 
    Set rngSource = Worksheets("Input").Range("D6:D7") 
    
    'find the row we're going to "paste" to 
    Dim destRow As Long 
    destRow = Sheets("Chart").Range("B" & Sheets("Chart").Rows.Count).End(xlUp).Row + 2 
    
    'define "paste" destination 
    Dim rngDest As Range 
    Set rngDest = Sheets("Chart").Range("B" & destRow & ":B" & destRow + 1) 
    
    '"paste" the values in 
    rngDest.Value = rngSource.Value 
    
    End Sub 
    
    +0

    感謝perposterer,我會試一試,讓你知道盡快。 –

    +0

    這段代碼對我不起作用,因爲這些值被粘貼在我的表格的底部(請參閱下面的註釋中的解釋),並且這些值不會轉置。你有問題嗎? –