1
Sub RowRangeMove()
    Sheets.Add().Name = "CopySheet"
    With Sheets("BigDataSet - Copy")
        .Range("A65000", .Range("A13000").End(xlUp)).Copy Destination:=Range("A1")
    With Sheets("BigDataSet - Copy")
        .Range("B65000", .Range("B13000").End(xlUp)).Copy Destination:=Range("B1")
    With Sheets("BigDataSet - Copy")
        .Range("C65000", .Range("C13000").End(xlUp)).Copy Destination:=Range("C1")
    With Sheets("BigDataSet - Copy")
        .Range("D65000", .Range("D13000").End(xlUp)).Copy Destination:=Range("D1")
    With Sheets("BigDataSet - Copy")
        .Range("E65000", .Range("E13000").End(xlUp)).Copy Destination:=Range("E1")
    With Sheets("BigDataSet - Copy")
        .Range("F65000", .Range("F13000").End(xlUp)).Copy Destination:=Range("F1")
    With Sheets("BigDataSet - Copy")
        .Range("G65000", .Range("G13000").End(xlUp)).Copy Destination:=Range("G1")
    With Sheets("BigDataSet - Copy")
        .Range("H65000", .Range("H13000").End(xlUp)).Copy Destination:=Range("H1")
    With Sheets("BigDataSet - Copy")
        .Range("I65000", .Range("I13000").End(xlUp)).Copy Destination:=Range("I1")
    With Sheets("BigDataSet - Copy")
        .Range("J65000", .Range("J13000").End(xlUp)).Copy Destination:=Range("J1")
    End With
    .
    .
    [iterate several times]
    .
    .
    Sheets.Add().Name = "CopySheet7"
    With Sheets("BigDataSet - Copy")
        .Range("A455006", .Range("A502750").End(xlUp)).Copy Destination:=Range("A1")
    With Sheets("BigDataSet - Copy")
        .Range("B455006", .Range("B502750").End(xlUp)).Copy Destination:=Range("B1")
    With Sheets("BigDataSet - Copy")
        .Range("C455006", .Range("C502750").End(xlUp)).Copy Destination:=Range("C1")
    With Sheets("BigDataSet - Copy")
        .Range("D455006", .Range("D502750").End(xlUp)).Copy Destination:=Range("D1")
    With Sheets("BigDataSet - Copy")
        .Range("E455006", .Range("E502750").End(xlUp)).Copy Destination:=Range("E1")
    With Sheets("BigDataSet - Copy")
        .Range("F455006", .Range("F502750").End(xlUp)).Copy Destination:=Range("F1")
    With Sheets("BigDataSet - Copy")
        .Range("G455006", .Range("G502750").End(xlUp)).Copy Destination:=Range("G1")
    With Sheets("BigDataSet - Copy")
        .Range("H455006", .Range("H502750").End(xlUp)).Copy Destination:=Range("H1")
    With Sheets("BigDataSet - Copy")
        .Range("I455006", .Range("I502750").End(xlUp)).Copy Destination:=Range("I1")
    With Sheets("BigDataSet - Copy")
        .Range("J455006", .Range("J502750").End(xlUp)).Copy Destination:=Range("J1")
    End With


End Sub

当我尝试运行它时,我收到一条错误消息,提示“期望结束于”。该脚本的目标是复制行范围并将它们放入新的单独工作表中(然后我可以将其放入单独的文件中,这些文件将在 Excel 2003 中读取而不会超过最大行数)。是否应该在其中的某处添加一个或多个 End With 语句?

4

4 回答 4

2

You don't need to repeat the With part so many times.

With Sheets("BigDataSet - Copy")
  .Range("A65000", .Range("A13000").End(xlUp)).Copy Destination:=Range("A1")
  .Range("B65000", .Range("B13000").End(xlUp)).Copy Destination:=Range("B1")
  .Range("C65000", .Range("C13000").End(xlUp)).Copy Destination:=Range("C1")
  ' Etc
End With

See the MSDN documentation for the "With" keyword:

http://msdn.microsoft.com/en-us/library/wc500chb(v=vs.100).aspx

于 2012-07-03T16:53:51.267 回答
1
With Sheets("BigDataSet - Copy") 
    .Range("A455006", .Range("A502750").End(xlUp)).Copy Destination:=Range("A1") 
    .Range("B455006", .Range("B502750").End(xlUp)).Copy Destination:=Range("B1") 

    .Range("C455006", .Range("C502750").End(xlUp)).Copy Destination:=Range("C1") 
    .Range("D455006", .Range("D502750").End(xlUp)).Copy Destination:=Range("D1") 
    .Range("E455006", .Range("E502750").End(xlUp)).Copy Destination:=Range("E1") 

    .Range("F455006", .Range("F502750").End(xlUp)).Copy Destination:=Range("F1") 
    .Range("G455006", .Range("G502750").End(xlUp)).Copy Destination:=Range("G1") 
    .Range("H455006", .Range("H502750").End(xlUp)).Copy Destination:=Range("H1") 
    .Range("I455006", .Range("I502750").End(xlUp)).Copy Destination:=Range("I1") 
    .Range("J455006", .Range("J502750").End(xlUp)).Copy Destination:=Range("J1") 
End With 

You only need one (Begin) With (msdn reference) . That's the whole point of that block, to let you not include the parent object each time.

于 2012-07-03T16:54:11.770 回答
1

Not only should there be at least one End With, there shouldn't be so many With statements.

Sub RowRangeMove()
    Sheets.Add().Name = "CopySheet"
    With Sheets("BigDataSet - Copy")
        .Range("A65000", .Range("A13000").End(xlUp)).Copy Destination:=Range("A1")
        .Range("B65000", .Range("B13000").End(xlUp)).Copy Destination:=Range("B1")
        .Range("C65000", .Range("C13000").End(xlUp)).Copy Destination:=Range("C1")
        .Range("D65000", .Range("D13000").End(xlUp)).Copy Destination:=Range("D1")
        .Range("E65000", .Range("E13000").End(xlUp)).Copy Destination:=Range("E1")
        .Range("F65000", .Range("F13000").End(xlUp)).Copy Destination:=Range("F1")
        .Range("G65000", .Range("G13000").End(xlUp)).Copy Destination:=Range("G1")
        .Range("H65000", .Range("H13000").End(xlUp)).Copy Destination:=Range("H1")
        .Range("I65000", .Range("I13000").End(xlUp)).Copy Destination:=Range("I1")
        .Range("J65000", .Range("J13000").End(xlUp)).Copy Destination:=Range("J1")
    End With

End Sub

would be the correct syntax.

The With statement is simply a way to shorten up your lines of code. The With statement is a way of saying "I'm going to perform a bunch of actions on a specific object" and shorten up the individual lines of code.

Example:

Without the With statement, the code above would look like this:

Sub RowRangeMove()
    Sheets.Add().Name = "CopySheet"
        Sheets("BigDataSet - Copy").Range("A65000", .Range("A13000").End(xlUp)).Copy Destination:=Range("A1")
        Sheets("BigDataSet - Copy").Range("B65000", .Range("B13000").End(xlUp)).Copy Destination:=Range("B1")
        Sheets("BigDataSet - Copy").Range("C65000", .Range("C13000").End(xlUp)).Copy Destination:=Range("C1")
        Sheets("BigDataSet - Copy").Range("D65000", .Range("D13000").End(xlUp)).Copy Destination:=Range("D1")
        Sheets("BigDataSet - Copy").Range("E65000", .Range("E13000").End(xlUp)).Copy Destination:=Range("E1")
        Sheets("BigDataSet - Copy").Range("F65000", .Range("F13000").End(xlUp)).Copy Destination:=Range("F1")
        Sheets("BigDataSet - Copy").Range("G65000", .Range("G13000").End(xlUp)).Copy Destination:=Range("G1")
        Sheets("BigDataSet - Copy").Range("H65000", .Range("H13000").End(xlUp)).Copy Destination:=Range("H1")
        Sheets("BigDataSet - Copy").Range("I65000", .Range("I13000").End(xlUp)).Copy Destination:=Range("I1")
        Sheets("BigDataSet - Copy").Range("J65000", .Range("J13000").End(xlUp)).Copy Destination:=Range("J1")
End Sub

In shorter terms, the With statement allows you to start out individual lines of code with a dot, and inside that with statement, the compiler will assume you mean the thing declared in your with statement.

So

With Answerer
  ' Inside this with block, any line beginning with "." , 
  ' the compiler will assume you mean "Answerer.".   
  ' Therefore ".FirstName" is the same as "Answerer.FirstName"
  .FirstName = "David"
  .LastName = "Stratton"
End With

is equivalient to

Answerer.FirstName = "David"
Amswerer.LastName = "Stratton"
于 2012-07-03T16:54:27.157 回答
0

你根本不需要With-End With积木。看起来您的第一个With块正在获取 A13000:J65000 的一部分并将其复制到新工作表,然后获取 A455006:J502750 的一部分并将其粘贴到第二个新工作表上。您的代码可以重构如下:

Sub RowRangeMove()

  Dim wkshts As Excel.Sheets
  Dim targetSheet As Excel.Worksheet
  Dim sourceSheet As Excel.Worksheet

  Set wkshts = Excel.Worksheets

  Application.ScreenUpdating = False

  ' create new sheet
  Set targetSheet = Excel.Worksheets.Add
  targetSheet.name = "CopySheet"

  ' copy from source to first target
  Set sourceSheet = Excel.Worksheets("BigDataSet - Copy")
  sourceSheet.Range(sourceSheet.Cells(13000, 1).End(xlUp), _
                    sourceSheet.Cells(65000, 10)).Copy targetSheet.Range("A1")

  ' create second new sheet
  Set targetSheet = Excel.Worksheets.Add
  targetSheet.name = "CopySheet7"

  ' copy from source to second target
  sourceSheet.Range(sourceSheet.Cells(455006, 1), _
                    sourceSheet.Cells(502750, 10).End(xlUp)).Copy targetSheet.Range("A1")

  Application.ScreenUpdating = True
End Sub
于 2012-07-03T18:13:02.300 回答