0

假设我在 Excel 中有 Sheet(1)。现在我也有 2500 行,其中包含从 A 到 BO 的列的数据。现在我希望将数据从这些工作表复制到同一 Excel 文件的另一工作表中 2500 行但不是整个列,而我只需要从 A 到 AA 数据的列复制到新工作表。

那么如何使用VBscript对其进行构图呢?

请帮我。

如何使用vbscript将行从一张纸复制到另一张纸

4

3 回答 3

4

要将数据从一张纸复制到另一张,您可以使用 Copy en PasteSpecial 命令。要使用.vbs脚本执行此操作,请执行以下操作:

' Create Excel object
Set objExcel = CreateObject("Excel.Application")
' Open the workbook
Set objWorkbook = objExcel.Workbooks.Open _
    ("C:\myworkbook.xlsx")
' Set to True or False, whatever you like
objExcel.Visible = True

' Select the range on Sheet1 you want to copy 
objWorkbook.Worksheets("Sheet1").Range("A1:AA25").Copy
' Paste it on Sheet2, starting at A1
objWorkbook.Worksheets("Sheet2").Range("A1").PasteSpecial
' Activate Sheet2 so you can see it actually pasted the data
objWorkbook.Worksheets("Sheet2").Activate 

如果您想在 Excel 中使用 VBS 宏执行此操作,您还可以调用复制和粘贴方法。只有您的工作簿对象将类似于ActiveWorkbook

于 2012-12-06T16:37:06.830 回答
1

此代码工作正常。只需复制并粘贴它。

Dim CopyFrom As  Object
Dim CopyTo As Object
Dim CopyThis As Object
Dim xl As Object

xl = CreateObject("Excel.Application")
xl.Visible = False
CopyFrom = xl.Workbooks.Open("E:\EXCEL\From.xls")
CopyTo = xl.Workbooks.Open("E:\EXCEL\To.xls")
For i = 0 To 1
    ''To use a password: Workbooks.Open Filename:="Filename", Password:="Password"
    If i = 0 Then
        CopyThis = CopyFrom.Sheets(1)
        CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
        CopyTo.Sheets(3).Name = "Sheet3"
    Else
        CopyThis = CopyFrom.Sheets(2)
        CopyThis.Copy(After:=CopyTo.Sheets(CopyTo.Sheets.Count))
        CopyTo.Sheets(4).Name = "Sheet4"
    End If
Next
CopyTo.Sheets(1).Activate()
CopyTo.Save()
'CopyTo.SaveAs("E:\EXCEL\Check.xls")
xl.Quit()
于 2015-06-15T10:38:55.050 回答
0
Sub buildMissingSheet(strMissingSheet)  'Just passing the missing sheet name in

' Master Sheet code
' Working on creating the "Master Sheet" at this time...May need to seperate the the code a little.
Dim GetRows1    As Worksheet
Dim GetRows2    As Worksheet
Dim PutRows     As Worksheet
Dim sglRowNum   As Single, i%


If strMissingSheet = strMASTERSHEET Then ' Create the strMASTERSHEET

  Set GetRows1 = Sheets(strRAWDATA)      ' These two sheets could be missing but will code around that later.
  Set GetRows2 = Sheets(strDATAWITH)     ' The two sheets I am getting rows from

' 只是在这里创建一个新工作表,假设它缺少 Worksheets.Add(After:=Worksheets(5)).Name = strMissingSheet Set PutRows = Sheets(strMissingSheet) ' 必须在声明之前创建缺少的工作表。

  PutRows.Select  'Select the sheet being built.

  With Cells(1, 1)
     .Value = strRAWDATA  'Not copying rows here but left it in this example anyway
    .AddComment
    .Comment.Visible = False
    .Select
    .Comment.Text Text:= _
       Chr(10) & "Name of sheet including header and the last 32 entries at the time this sheet was updated."
  End With

'这是我们将整行从一张纸复制到另一张纸的地方。

  GetRows1.Rows(1).Copy PutRows.Rows(2)  'Copy header row from existing sheet to "Master Sheet" for instance.
  GetRows1.Select
  sglRowNum = ReturnLastRow(ActiveSheet.Cells)  'return last row with data on active sheet

' 我想要最后几行数据“32 行”,所以在表格的末尾找到了这个代码,可以在包括这个网站在内的几个地方的互联网上找到。

'现在您可能一直在寻找的代码将 32 行数据从一张表移动到另一张表。

  For i = 1 To 32  'Start at row 3 on the Put sheet after sheet name and header.
     GetRows1.Rows(sglRowNum - (32 - i)).Copy PutRows.Rows(i + 2)
  Next i

结束子

于 2013-09-15T13:40:44.183 回答