簡體   English   中英

如何在不刪除工作表2上的數據的情況下將單元格從工作表1復制到工作表2

[英]how to copy cells from sheet 1 to sheet 2 without removing data on sheet 2

正如標題所示,我需要執行以下任務的代碼。 我已經嘗試了很多不同的代碼,但是仍然無法正常工作。 在此處輸入圖片說明 我只需要使用命令按鈕將2列“ SKU”和“折扣”移到sheet2並立即將其刪除。

我已經可以使用此編碼了。 但是,問題只是開始。 在此處輸入圖片說明 當我成功移動了第一個數據並嘗試移動第二個數據時,第一個數據消失了。

我已經嘗試了很多方法,但是仍然無法弄清楚代碼出了什么問題。

請檢查以下代碼:

Sub OUTGOING_GOODS()
function1
function2
clear
Range_End_Method
End Sub

Sub function1()
Sheets("Invoice Print").Range("B21:B27").Copy Destination:=Sheets("Outgoing Goods").Range("D4")
End Sub

Sub function2()
Sheets("Invoice Print").Range("D21:D27").Copy Destination:=Sheets("Outgoing Goods").Range("L4")
End Sub

Sub clear()
Range("B21:B27").clear
End Sub

我還需要更改輸入數據的范圍。 如您所見,Range僅從D21:D27定義,但是我需要多於第27行,以防輸入其他數據。

已經嘗試了以下代碼:

 With Worksheets("Sheet2")
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
    LastRow = .Cells(.Rows.Count, "L").End(xlUp).Row
    For Each cell In Range("D4:D" & LastRow)
    DestinationRow = LastRow + 1
    Next
    For Each cell In Range("L4:L" & LastRow)
    DestinationRow = LastRow + 1
    Next
End With

Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 1 To InputData
            Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
            For j = 1 To 3
                    .Cells(lastrow + 1, j).Value = InputData(i, j)
            Next j
        Next i
    End With

這仍然無法正常工作。

根據到目前為止的討論,我提出以下建議:

Sub Outgoing_Goods_New()
'
Dim Outgoing As Worksheet 'Generally it's better to use Worksheet variables. Saves the trouble of having to re-type the sheet name each time you reference the sheet
Dim Invoice As Worksheet
Dim LastRow_Invoice As Long
Dim LastRow_Outgoing As Long
Set Outgoing = ActiveWorkbook.Worksheets("Outgoing Goods")
Set Invoice = ActiveWorkbook.Worksheets("Invoice Print")

'Find the last row of Outgoing column D that's used so we know where to paste the new set of outgoing goods
LastRow_Outgoing = Outgoing.Range("D1048576").End(xlUp).Row

'Make sure column L of Outgoing ends at the same point
If Outgoing.Range("L1048576").End(xlUp).Row > LastRow_Outgoing Then
    LastRow_Outgoing = Outgoing.Range("L1048576").End(xlUp).Row
End If 'else column L's last used row is farther up the worksheet or the same row. Either way no need to update the value

'Determine how much data to copy
LastRow_Invoice = Invoice.Range("B1048576").End(xlUp).Row 'I'm assuming Column D of Invoice Print has to end at the same row. If not, use the same IF statement as above, but
    'checking column D of Invoice

'Copy the data from column B
Invoice.Range("B2:B" & LastRow_Invoice).Copy

'Paste to Outgoing Goods
Outgoing.Range("B" & LastRow_Outgoing).PasteSpecial xlPasteAll

'Copy Column D of Invoice
Invoice.Range("D2:D" & LastRow_Invoice).Copy
Outgoing.Range("L" & LastRow_Outgoing).PasteSpecial xlPasteAll

'Clear the data from Invoice print
Invoice.Range("B2:B" & LastRow_Invoice).ClearContents 'Removes the Value, but leaves formatting, comments, etc. alone

End Sub

這主要是您已經擁有的邏輯,但是我做了一些清理以消除歧義並稍微泛化了邏輯。 另外,請注意,我沒有保留單獨的Sub。 只需花很少的精力,解析邏輯就沒有任何好處,尤其是沒有任何代碼被重用。

最后,我沒有刪除Invoice Print上的D列,假設這些單元格只是保存了根據B列中的值提取新數據的公式。如果不是這種情況,似乎您應該添加第二個ClearContents來刪除Column D也是如此,但是鑒於用例的含糊性,這還不確定。

暫無
暫無

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

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