excel中用VBA实现如下:表格汇总.xls(其中有3个sheet,名称为1.2 1.3 1.4),表格天津.xls(其中有3个sheet,
excel中用VBA实现如下:表格汇总.xls(其中有3个sheet,名称为1.21.31.4),表格天津.xls(其中有3个sheet,名称为1.21.31.4),把天...
excel中用VBA实现如下:表格汇总.xls(其中有3个sheet,名称为1.2 1.3 1.4),表格天津.xls(其中有3个sheet,名称为1.2 1.3 1.4),把天津.xls3个sheet中第二行到最后一行复制到汇总.xls对应的3个sheet中每个sheet的最后一行下面。
展开
3个回答
展开全部
Sub 汇总()
Dim WB As Workbook, YN As Boolean
Dim Hzsht, Tjsht As Worksheet
Dim RowHz, RowTj As Long
For Each WB In Workbooks
If WB.Name = "表格天津.xls" Then
YN = True
Exit For
End If
Next
If YN = False Then Workbooks.Open ThisWorkbook.Path & "\表格天津.xls"
For i = 1 To 3
Set Hzsht = Workbooks("表格汇总.xls").Sheets(i)
Set Tjsht = Workbooks("表格天津.xls").Sheets(i)
RowHz = Hzsht.UsedRange.Rows.Count
RowTj = Tjsht.UsedRange.Rows.Count
Tjsht.Rows("2:" & RowTj).Copy Hzsht.Range("A" & RowHz + 1)
Next
End Sub
追问
先谢谢了!我表中sheet1和3实现了,就是2实现不了,什么原因呢?另外我想只拷贝数值。
追答
上面的代码顺序拷贝你工作薄左数第1,2,3 这三个表.如果你要求的表不是前3个?那 我改代码.
另外你说的只拷贝数值是什么意思? 要拷贝的区域中有公式?你只需要拷贝数值?
好吧看下面的代码
Sub 汇总()
Dim WB As Workbook, YN As Boolean
Dim Hzsht, Tjsht As Worksheet
Dim RowHz, RowTj As Long
For Each WB In Workbooks
If WB.Name = "表格天津.xls" Then
YN = True
Exit For
End If
Next
bmarr = Split("1.2,1.3,1.4", ",")
If YN = False Then Workbooks.Open ThisWorkbook.Path & "\表格天津.xls"
For i = 0 To 2
Set Hzsht = Workbooks("表格汇总.xls").Sheets(bmarr(i))
Set Tjsht = Workbooks("表格天津.xls").Sheets(bmarr(i))
RowHz = Hzsht.UsedRange.Rows.Count
RowTj = Tjsht.UsedRange.Rows.Count
Tjsht.Rows("2:" & RowTj).Copy
Hzsht.Range("A" & RowHz + 1).PasteSpecial Paste:=xlPasteValues
Next
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询