源文件上的VBA按钮,用于从特定行导出特定列,并粘贴到具有相同格式/样式的目标excel文件

时间:2018-01-22 11:38:21

标签: excel-vba export-to-excel vba excel

我是一个VBA菜鸟试图在excel文件上创建一个导入按钮,该按钮将检查来自另一个工作簿的行数,并在新行添加到另一个工作簿时更新当前文件。请问有什么想法吗?

更新: 我认为最好从我的源excel文件导出内容,而不是从目标文件导入。

如果你有更好的选择来获得相同的最终结果,除了我所做的以外,我会非常感激。

Sub Export2Report()

Dim wb2 As Workbook
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim rng As Range
Dim row_input As Long
Dim lr As Long

'Set open target file
Set wb1 = ThisWorkbook

'Source sheet for data
Set sh1 = wb1.Sheets("sheet2")

'input row to be copied
row_input = Application.InputBox(Prompt:="Please Select Row to Export", Title:="Range Select", Type:=9)

'select columns to copy
Union(Cells(row_input, 5), Cells(row_input, 6), Cells(row_input, 9), Cells(row_input, 10), Cells(row_input, 14), Cells(row_input, 15), Cells(row_input, 16), Cells(row_input, 17), Cells(row_input, 21), Cells(row_input, 22), Cells(row_input, 23), Cells(row_input, 24), Cells(row_input, 25), Cells(row_input, 28), Cells(row_input, 30)).Select
Selection.Copy

'open target report file
Set wb2 = Workbooks.Open("C:\excel2.xlsm")
Set sh2 = wb2.Sheets("sheet2")

'activate target sheet and paste data to workbook 2
sh2.Activate
sh2.Unprotect Password:="xx"

Range("A1").End(xlDown).Offset(1, 0).Row
ActiveSheet.Paste
Application.CutCopyMode = False

'place borders on all cells

    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With

wb2.Save

End Sub

0 个答案:

没有答案