如何重复整个范围的单元格和列的宏?

时间:2013-02-16 21:36:45

标签: excel excel-vba vba

基本上我将workbook1的单元格与workbook2链接起来,整个电子表格的代码是什么? 我的错误没有清楚解释,你可以在下面看到我从workbook2中分配workbook1中单元格的值

Sub Macro2() 
    Windows("workbook2.xlsx").Activate 
    Windows("workbook1.xlsx").Activate 
    ActiveCell.FormulaR1C1 = _ 
       "='[workbook2.xlsx]workbook1'!R18C1" 
    Range("A19").Select 
    ActiveCell.FormulaR1C1 = _
        "='[workbook2.xlsx]workbook1'!R19C1" 
    Range("A20").Select 
    ......... 
End Sub 

我想知道A9到A120,E9到E120,F9到F120的最快方式是什么......

2 个答案:

答案 0 :(得分:1)

此代码将链接所有工作表中的所有单元格:

Sub LinkSheets()
    Dim ws As Worksheet

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False

    For Each ws In Worksheets
        ws.UsedRange.FormulaR1C1 = "='[workbook2.xlsx]" & ws.Name & "'!RC"
    Next

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

如果您只想链接一个工作表中的某些单元格,请尝试以下代码:

Sub LinkRange()
    Workbooks("workbook1.xlsx").Worksheets("Sheet1"). _
        Range("A9:A120,E9:E120,F9:F120").FormulaR1C1= _
        "='[workbook2.xlsx]Sheet1'!RC"
End Sub

答案 1 :(得分:0)

Sub CopyPasteEntries()
 Dim i, g, h As Integer
 Sheets("Sheet1").Select
 Range("A1").EntireRow.Copy
 Sheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteAll
 i = 2
 Sheets("Sheet1").Select
  Do Until IsEmpty(Cells(i, 1))  
  i = i + 1        
  Loop
  g = i - 1
  h = 2
  For i = 2 To g 
 Sheets("Sheet1").Select
  If Cells(i, 1).Value <> "Created" Then
   Cells(i, 1).EntireRow.Copy
 Sheets("Sheet2").Select
   Cells(h, 1).Select
 ActiveCell.PasteSpecial xlPasteAll
 h = h + 1
 End If     
  Next
 Application.CutCopyMode = False
 End Sub