我正在创建一个带有数据输入页面的数据库,然后使用一个按钮将其粘贴到主表中。
我习惯于处理代码和编辑现有内容,但这是我第一次从头开始。
我想要做的是复制数据输入单元格并粘贴到母版表的相应列中,每次都开始新的一行。
例如数据输入表上的单元格C5复制到主文件上的A列,C7复制到B列等。
我设法将其复制,但仅逐个处理每个单元格(理想情况下,id希望一次复制并粘贴所有内容,而不是逐个逐个粘贴),但是无论我如何尝试所有页面在这里,我无法让它每次都转到下一行。
复制并粘贴有效:
Sheets("RAW DATA").Range("Ak2").PasteSpecial Paste:=xlPasteValues
Sheets("Enter Accident").Range("Q19").Copy
Sheets("RAW DATA").Range("AL2").PasteSpecial Paste:=xlPasteValues
Sheets("Enter Accident").Range("Q21").Copy
Sheets("RAW DATA").Range("AM2").PasteSpecial Paste:=xlPasteValues
Sheets("Enter Accident").Range("Q23").Copy
Sheets("RAW DATA").Range("AN2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
我尝试过的方法没有起作用:
Sheets("Enter Accident").Range("C3, C5, C7, C9, C11, C13 ,C15, C17, C19, C21, C23, C25, C27, C29, C31, G3, G11, G13, G15, G17, G19, G21, G23, G25, G31, L3, L11, L13, L15, L19, L21, Q3, Q5, Q7, Q9, Q11, Q15, Q17, Q19, Q21, Q23").Copy
Sheets("RAW DATA").Range("A2, B2, C2, D2, E2, F2, G2, H2, I2, J2, K2, L2, M2, N2, O2, P2, Q2, R2, S2, T2, U2, V2, W2, Z2, Y2, AA2, AB2, AC2, X2, AD2, AE2, AF2, AG2, AH2, AI2, AJ2, AK2, AL2, AM2, AN2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
这两种方法都不能与添加最后一行进行任何交互,例如:
Dim lastrow As Range
Set lastrow = Sheets("RAW DATA").Cells.SpecialCells(xlCellTypeLastCell).EntireRow + 1
答案 0 :(得分:0)
一种方法。如果您的范围可能会有所不同,则最好使用OFFSET和循环。
Sub x()
Dim vCopy, vPaste, i As Long
vCopy = Array("C3", "C5", "C7", "C9", "C11", "C13 ,C15", "C17", "C19", "C21", "C23", "C25", "C27", "C29", "C31", "G3", "G11", "G13", "G15", "G17", "G19", "G21", "G23", "G25", "G31", "L3", "L11", "L13", "L15", "L19", "L21", "Q3", "Q5", "Q7", "Q9", "Q11", "Q15", "Q17", "Q19", "Q21", "Q23")
vPaste = Array("A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2", "I2", "J2", "K2", "L2", "M2", "N2", "O2", "P2", "Q2", "R2", "S2", "T2", "U2", "V2", "W2", "Z2", "Y2", "AA2", "AB2", "AC2", "X2", "AD2", "AE2", "AF2", "AG2", "AH2", "AI2", "AJ2", "AK2", "AL2", "AM2", "AN2")
For i = LBound(vCopy) To UBound(vCopy)
Sheets("RAW DATA").Range(vPaste(i)).Value = Sheets("Enter Accident").Range(vCopy(i)).Value
Next i
End Sub
为避免覆盖,请找到最后一行并向下移动一个单元格。
Sub x()
Dim vCopy, i As Long, rPaste As Range
vCopy = Array("C3", "C5", "C7", "C9", "C11", "C13 ,C15", "C17", "C19", "C21", "C23", "C25", "C27", "C29", "C31", "G3", "G11", "G13", "G15", "G17", "G19", "G21", "G23", "G25", "G31", "L3", "L11", "L13", "L15", "L19", "L21", "Q3", "Q5", "Q7", "Q9", "Q11", "Q15", "Q17", "Q19", "Q21", "Q23")
Set rPaste = Sheets("RAW DATA").Cells(Rows.Count, 1).End(xlUp)(2)
For i = LBound(vCopy) To UBound(vCopy)
rPaste.Value = Sheets("Enter Accident").Range(vCopy(i)).Value
Set rPaste = rPaste.Offset(, 1)
Next i
End Sub
答案 1 :(得分:0)
您是许多人中的一员,如果确实不需要将数据复制到剪贴板中,则为其中之一。让我们看一下这段代码:
Sheets("Enter Accident").Range("Q19").Copy
Sheets("RAW DATA").Range("AL2").PasteSpecial Paste:=xlPasteValues
这与以下内容完全相同:
Sheets("RAW DATA").Range("AL2").Value = Sheets("Enter Accident").Range("Q19").Value
除了:
SJR的答案就是对这种解释的阐述。