Excel VBA导出到其他Excel工作簿

时间:2015-01-12 10:13:23

标签: excel vba excel-vba

我没有足够的VBA excel经验,所以我可以使用一些帮助。

我创建了一个excel工作表。

现在我想创建一个宏,它将我的工作表中的数据发送到其他excel工作簿。

我想使用if语句,所以如果项目名称=“x”,那么宏应该将数据发送到工作簿“x”并按日期对导入的工作表进行排名。

我在网上发现了这个并稍微调整了一下

Private Sub CommandButton21_Click()

    Dim Data As Range

    Dim myData As Workbook

    Worksheets("blad1").Select

    Set Data = Range("c2")

    Set myData = Workbooks.Open("C:\test\locatie.xlsx")
    Worksheets("blad1").Select
    Worksheets("blad1").Range("a1").Select

    RowCount = Worksheets("blad1").Range("A1").CurrentRegion.Rows.Count
    With Worksheets("blad1").Range("A1")

.Offset(RowCount, 0) = Data

    End With
End Sub

1 个答案:

答案 0 :(得分:0)

将工作表复制到新文件的简单示例:

IF projectname = "x" THEN 'you have to define projectname

OldName = ThisWorkbook.Name 'name of your open file
Workbook.Add 'Open new file
newName = ActiveWorkbook.Name 'name of the new file
Windows(OldName).Activate 'original file select
Sheets("Sheetname").Activate 'define Sheetname of your original file
ActiveSheet.Select
ActiveSheet.Copy after:=Workbooks(newName).Sheets(1)
Windows(newName).Activate
ActiveWorkbook.SaveAs Filename:=filename1, FileFormat:=51, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False 'define filename1
Application.DisplayAlerts = True
End If