简体   繁体   English

仅当第一列不为空白(数据量有所不同)时,才将数据复制到另一张纸的第一空白行

[英]Copy data to first blank row in another sheet, only if first column is not blank (amount of data varies)

I want to be able to press a button to copy some data across from sheet "Data Entry" to the first blank row in another sheet "Database". 我希望能够按下一个按钮,以便将部分数据从工作表“数据输入”复制到另一工作表“数据库”的第一空白行。

However, if the first column is blank, I don't want that row of data to be copied. 但是,如果第一列为空白,则我不希望复制该行数据。 Also, sometimes the "Data Entry" sheet may have 4 rows of data, whilst sometimes it may have 5, 6, 7 or 8. 同样,有时“数据输入”表可能包含4行数据,而有时则可能包含5、6、7或8。

I've attached screenshots below. 我已经附上了下面的截图。

The code I'm using so far is not giving any error, but nothing seems to be happening, either. 到目前为止,我正在使用的代码没有给出任何错误,但是似乎也没有发生任何事情。

Private Sub CommandButton1_Click()

    Dim cl As Range    
    For Each cl In Sheet2.Range("A8:A23")

        If Not IsEmpty(ActiveCell.Value) Then

            Range("A" & ActiveCell.Row & ":R" & ActiveCell.Row).Select
            Selection.Copy
            Sheets("Database").Select
            ActiveCell.End(xlDown).Offset(1, 0).Select
            ActiveSheet.Paste

        End If    
    Next cl
End Sub

图像2图像3

Your current code is constantly referring to ActiveCell (which, after the first iteration [if it ever got that far], is a cell on the "Database" sheet!), not to the cells in range A8:A23 of Sheet2. 您当前的代码一直在引用ActiveCell (在第一次迭代后,如果有的话,它是“数据库”表上的单元格!),而不是Sheet2的A8:A23范围内的单元格。

Refactored code could be: 重构的代码可能是:

Private Sub CommandButton1_Click()
    Dim cl As Range

    For Each cl In Sheet2.Range("A8:A23")
        If Not IsEmpty(cl.Value) Then
            With Worksheets("Database") ' to make it easier to refer to the sheet
                'Find last cell in column A, 
                ' go to the row below,
                ' extend the range to be 18 columns wide,
                ' set values to be values on Sheet2
                .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Resize(1, 18).Value = cl.Resize(1, 18).Value
            End With
        End If
    Next
End Sub

I'd do something simple like this. 我会做这样简单的事情。 This may not be as efficient as some other methods, but it should do what you want it to. 这可能不如其他一些方法有效,但是它应该做您想要的。 Also the range won't be hard coded and will change as the number of rows of data changes. 同样,范围也不会被硬编码,并且会随着数据行数的变化而变化。

Dim lastRowDataEntry As Integer
Dim lastRowDatabase As Integer
Dim a As Integer

'Find the last row of data in each sheet
lastRowDataEntry = Sheets("Data Entry").Range("B" & Rows.Count).End(xlUp).Offset(0).Row


For a = 8 To lastRowDataEntry
    If IsEmplty(Sheets("Data Entry").Cells(a, "A").Value) = True Then GoTo ReadyForNexta
    Row(a).Select
    Selection.Copy
    lastRowDataBase = Sheets("Database").Range("A" & Rows.Count).End(xlUp).Offset(0).Row
    Sheets("Database").Cells(lastRowDatabase, "A").Select
    ActiveSheet.Paste

ReadyForNexta:

Next a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM