嗨,我是VBA的新手想要将从C:14开始的一行代码复制到K:14并将其粘贴到另一个工作簿中,进入C5到K5行。我已经编写了这段代码,但仍然会出错,我是否在正确的轨道上?
Sub Shankill()
Dim sBook_t As String
Dim sBook_s As String
Dim sSheet_t As String
Dim sSheet_s As String
Dim lMaxRows_t As Long
Dim lMaxRows_s As Long
Dim sMaxCol_s As String
Dim sRange_t As String
Dim sRange_s As String
sBook_t = "Shankill DR.xls"
sBook_s = "DR complete.xls"
sSheet_t = "Shankill DR"
sSheet_s = "DR complete"
lMaxRows_t = Workbooks(sBook_t).Sheets(sSheet_t).Cells(Rows.Count, "C:14").End(xlUp).Row
lMaxRows_s = Workbooks(sBook_s).Sheets(sSheet_s).Cells(Rows.Count, "C5").End(xlUp).Row
sMaxCol_s = Workbooks(sBook_s).Sheets(sSheet_s).Cells(1, Columns.Count).End(xlToLeft).Address
sMaxCol_s = Mid(sMaxCol_s, 2, InStr(2, sMaxCol_s, "$") - 2)
If (lMaxRows_t = 1) Then
sRange_t = "C14:" & sMaxCol_s & lMaxRows_s
sRange_s = "C:5" & sMaxCol_s & lMaxRows_s
Workbooks(sBook_t).Sheets(sSheet_t).Range(sRange_t) = Workbooks(sBook_s).Sheets(sSheet_s).Range(sRange_s).Value
Else
sRange_t = "C:14" & (lMaxRows_t + 1) & ":" & sMaxCol_s & (lMaxRows_t + lMaxRows_s - 1)
sRange_s = "C:5" & sMaxCol_s & lMaxRows_s
Workbooks(sBook_t).Sheets(sSheet_t).Range(sRange_t) = Workbooks(sBook_s).Sheets(sSheet_s).Range(sRange_s).Value
End If
End Sub
答案 0 :(得分:2)
想要将从C:14开始的一行代码复制到K:14并将其粘贴到另一个工作簿中,然后粘贴到第C5行到第K5行。
你需要的只是
Sub Shankill()
Dim sBook_t As String
Dim sBook_s As String
Dim sSheet_t As String
Dim sSheet_s As String
sBook_t = "Shankill DR.xls"
sBook_s = "DR complete.xls"
sSheet_t = "Shankill DR"
sSheet_s = "DR complete"
Workbooks(sBook_t).Sheets(sSheet_t).Range("C14:K14").Copy _
Workbooks(sBook_s).Sheets(sSheet_s).Range("C5")
End Sub
来自评论的跟进
是的我想复制计算结果的值吗?
将您的代码更改为此(未经测试)
Sub Shankill()
Dim sBook_t As String
Dim sBook_s As String
Dim sSheet_t As String
Dim sSheet_s As String
sBook_t = "Shankill DR.xls"
sBook_s = "DR complete.xls"
sSheet_t = "Shankill DR"
sSheet_s = "DR complete"
Workbooks(sBook_t).Sheets(sSheet_t).Range("C14:K14").Copy
Workbooks(sBook_s).Sheets(sSheet_s).Range("C5").PasteSpecial Paste:=xlPasteValues
End Sub
答案 1 :(得分:0)
试试这个
Sub macro1()
Range("A3:M6").Copy Destination:=Cells(ActiveCell.Row + 1, 1)
End Sub