簡體   English   中英

使用 VBA 將整行復制到不同的工作表

[英]Copy entire row to different sheet using VBA

我已附上 Excel 文件以供參考。

在“輸入”表中,有一個將在單擊提交按鈕時生成的生產計划。 同樣的時間表也必須復制到“輸出”表中。 但我無法將其粘貼到所需位置(藍色突出顯示區域)。

另一個挑戰是保留與年份相對應的相關數據並清除其余數據。
這意味着假設這是一個 34 年的時間表。 現在,如果我再次用 20 年生成它,它會將 I 類和 II 類的值保留到 20 年,並清除其余部分。

同樣必須在輸出表中。

Submit_Click 中的代碼:

Sub Submit_Click()
Dim no_years As Integer
Dim i_val As Integer
Range("c10").Activate

ActiveCell.EntireRow.Clear

ActiveCell = Range("b3").Value
no_years = Range("b4").Value
i_val = Range("b5").Value

Do While no_years > 0
    ActiveCell.Offset(0, 1).Activate
    ActiveCell.Value = DateAdd("YYYY", i_val, ActiveCell.Offset(0, -1).Value)

    no_years = no_years - 1
Loop

' copy production schedule calender to all sheets
ActiveCell.EntireRow.Copy Destination:=Sheets("Output").Range("A" & Rows.Count).End(xlUp).Offset(1)

MsgBox ("Production schedule calendar generated")
End Sub

做到這一點的簡單方法:

ActiveSheet.Rows("1:1").Copy
Sheets("Output").Paste

這是你要找的嗎?

我不確定您的意思是想要的位置是什么? 它是 OUTPUT 工作表的第一列嗎? 我只是增強了您當前的代碼。 關於第二個挑戰,我完全不明白你想表達什么。 也許您應該打印屏幕或將 excel 文件發送給我,以便我查看。

Option Explicit
Dim CurrentWorkbook As Workbook
Dim InputWorksheet As Worksheet
Dim OutputWorksheet As Worksheet

Sub Submit_Click()

Set CurrentWorkbook = Workbooks(ActiveWorkbook.Name)
Set InputWorksheet = CurrentWorkbook.Sheets("Input")
Set OutputWorksheet = CurrentWorkbook.Sheets("Output")

Dim NumberOfYear As Long
Dim IntervalOfYear As Long
Dim ColumShift As Long

ColumShift = 1
InputWorksheet.Range("c10").EntireRow.Clear
InputWorksheet.Range("c10").Value = InputWorksheet.Range("B3").Value

NumberOfYear = InputWorksheet.Range("b4").Value
IntervalOfYear = InputWorksheet.Range("b5").Value

Do While NumberOfYear > 0
InputWorksheet.Range("c10").Offset(0, ColumShift).Value = DateAdd("YYYY", IntervalOfYear, InputWorksheet.Range("c10").Value)
NumberOfYear = NumberOfYear - 1
ColumShift = ColumShift + 1
Loop

' copy production schedule calender to all sheets
ActiveCell.EntireRow.Copy Destination:=Sheets("Output").Range("A" & Rows.Count).End(xlUp).Offset(1)

MsgBox ("Production schedule calendar generated")
End Sub

我遇到了類似的問題並找到了解決方案:Acyou can use this range "B§" directly as (我假設您當前的工作表設置為Sht)

Sht.Cells(3, 2).EntireRow.Copy 'or the other way in comments
'ActiveCell = Range("b3").Value  
'ActiveCell.EntireRow.Copy
Sheets("Output")Range("A" & Rows.Count).PasteSpecial Paste:=xlValues

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM