Excel VBA - 打开工作簿并粘贴数据

时间:2017-05-07 17:51:40

标签: excel vba

我找到了这个优秀的代码,但我需要根据自己的目的进行调整。

首先,我需要打开网络上的数据工作簿。我遇到的问题是,有时可能会被其他用户打开并提供“只读”选项。如何让它接受只读选项,以便我可以开始提取数据。

其次,它使用“=”进行复制。如何更改它以仅复制值?

第一个宏:

Sub test()
'to open another workbook
Application.ScreenUpdating = False
Workbooks.Open Filename:=ThisWorkbook.Path & "\Schedule.xls"
ThisWorkbook.Activate
Application.ScreenUpdating = True

End Sub

第二个宏:

Dim Sh As Worksheet
Dim Newsh As Worksheet
Dim myCell As Range
Dim ColNum As Integer
Dim RwNum As Long
Dim Basebook As Workbook

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
End With

'Delete the sheet "Summary-Sheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Worksheets("Summary-Sheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True

'Add a worksheet with the name "Summary-Sheet"
Set Basebook = ThisWorkbook
Set Newsh = Basebook.Worksheets.Add
Newsh.Name = "Summary-Sheet"

'The links to the first sheet will start in row 2
RwNum = 1

For Each Sh In Basebook.Worksheets
    If Sh.Name <> Newsh.Name And Sh.Visible Then
        ColNum = 1
        RwNum = RwNum + 1
        'Copy the sheet name in the A column
        Newsh.Cells(RwNum, 1).Value = Sh.Name

        For Each myCell In Sh.Range("A1,D5:E5,Z10")  '<--Change the range
            ColNum = ColNum + 1
            Newsh.Cells(RwNum, ColNum).Formula = _
            "='" & Sh.Name & "'!" & myCell.Address(False, False)
        Next myCell

    End If
Next Sh

Newsh.UsedRange.Columns.AutoFit

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
End With
End Sub

0 个答案:

没有答案