使用vba公式并复制另一个工作簿

时间:2018-02-15 12:11:02

标签: excel vba excel-vba

我在每个单元格中都有一个文本列,以某种方式在另一列中提取。

我制作了这个vba脚本,但不适用于在每个单元格的另一列中应用公式,它作为一个循环,我什么也看不到。

Sub CommandButtonClick()
 Dim Addr As String
 Addr = Range("E2:E") & Cells(Rows.Count,"E").End(xlUp).Row).Address
 Range(Addr) = Evaluate("SUBSTITUTE(RIGHT(E2,LEN(E2)-
 FIND("=>",E2)),CHAR(62),"")")
END Sub

这是将结果复制到另一个工作簿中:

Sub copyresult()
Dim x As Workbook
Dim y As Workbook

'## Open both workbooks first:
Set x = Workbooks.Open(" ticketsosurgenti ")
Set y = Workbooks.Open(" ticketsos ")

'Now, copy what you want from x:
x.Sheets("Sheet1").Range("C2:C").Copy
x.Sheets("Sheet1").Range("D2:D").Copy
'Now, paste to y worksheet:
y.Sheets("Sheet1").Range("A1:A").PasteSpecial
y.Sheets("Sheet1").Range("B1:B").PasteSpecial
'Close x:
x.Close

End Sub

1 个答案:

答案 0 :(得分:0)

对于工作簿之间的复制和粘贴,以下内容应该有所帮助:

Sub copyresult()
     Dim x As Workbook
     Dim y As Workbook
     Dim LastRow As Long
     '## Open both workbooks first:
     Set x = Workbooks.Open("C:/ticketsosurgenti.xlsx")
     Set y = Workbooks.Open("C:/ticketsos.xlsx")
     'above amend the path and filename of both your workbooks
     Dim wsX As Worksheet: Set wsX = x.Sheets("Sheet1")
     Dim wsY As Worksheet: Set wsY = y.Sheets("Sheet1")

     LastRow = wsX.Cells(wsX.Rows.Count, "A").End(xlUp).Row
     'get the last row with data on Column A on Sheet x

     wsX.Range("C2:C" & LastRow).Copy
     wsY.Range("A1").PasteSpecial xlPasteAll

     wsX.Range("D2:D" & LastRow).Copy
     wsY.Range("B1").PasteSpecial xlPasteAll

     x.Close
 End Sub