使用宏将行复制并粘贴到列中

时间:2014-02-03 06:04:05

标签: excel-vba vba excel

我对宏来说很新..
基本上我想将一个工作簿中名为“Present month”的工作表中的 (E23 to H23)复制到名为“ANA”{{1}的另一个工作表的在另一个工作簿中。

请帮帮我!!

编辑1:评论代码

(K4 to K7)

2 个答案:

答案 0 :(得分:0)

这是一个提示。转到开发人员标签 - >记录宏 - >做你想要的行动 - >停止录音和woila按alt + F11并检查模块。

F8是你的朋友,你可以看到你录制的宏做了什么!!!

答案 1 :(得分:0)

试试这个:

Sub Copy_and_update_last_col_Census()

Dim wb As Workbook
Dim rng2copy As Range, rng2paste As Range
Dim query As Integer
Dim myfile
Dim filename As String

'~~> I assumed that Charts.xlsm is the workbook you are running the macro
Set rng2paste = ThisWorkbook.Sheets("ANA").Range("K4")
filename = "NOL_Support_Ticket_Status_Report.xlsx"
'~~> Handles the error if workbook is not open
On Error Resume Next
Set wb = Workbooks(filename)
'~~> check for error and execute action necessary
If Err.Number <> 0 Then
    query = MsgBox("Source workbook not open." & vbNewLine & _
            "Do you want to open it?", vbYesNo)
Else
    GoTo proceedcopy
End If

If query = 6 Then
    myfile = Application.GetOpenFilename(Filefilter:="Excel Files (*.xlsx), *.xlsx")
Else
    MsgBox "Exiting now."
    Exit Sub
End If
'~~> check if user selected the correct file
If myfile <> False Then
    If Dir(myfile) = filename Then
        Set wb = Workbooks.Open(myfile)
    Else
        MsgBox "Wrong file loaded." & vbNewLine & _
        "Exiting now."
        Exit Sub
    End If
Else
    MsgBox "No file loaded." & vbNewLine & _
        "Exiting now."
    Exit Sub
End If
'~~> this do the actual copying
proceedcopy:
Set rng2copy = wb.Sheets("Present Month").Range("E23", "H23")
rng2copy.Copy
rng2paste.PasteSpecial xlPasteValues, , , True
Application.CutCopyMode = False
wb.Close False

End Sub

经过试用和测试 但我不能说我的结构很好 所以我对你进行进一步的测试。