当前位置:首页经验技巧Excel经验excel入门

vba怎么写两个excel表格

2026-01-10 10:07:36

1.Excel中VBA如何操作两个表的数据

Public Sub 复制数据()

Dim sh1, sh2 As Range

Set sh1 = Sheets("biao1").Range("A1:A10000")

Set sh2 = Sheets("biao2").Range("D1:D10000")

Sheets("biao3").Range("a1") = Sheets("biao1").Range("A1")

han = 0

For x = 1 To 100

sj = Sheets("biao1").Cells(x, 1)

If han > 0 Then

For y = 1 To han

sj1 = Sheets("biao3").Cells(y, 1)

If sj1 = sj Then

cf = True

Exit For

Else

cf = False

End If

Next y

End If

If cf = False Then

han = han + 1

Sheets("biao3").Cells(han, 1) = Sheets("biao1").Cells(x, 1)

End If

Next x

han = han - 1

For x = 1 To 100

sj = Sheets("biao2").Cells(x, 1)

If han > 1 Then

For y = 1 To han

sj1 = Sheets("biao3").Cells(y, 1)

If sj1 = sj Then

cf = True

Exit For

Else

cf = False

End If

Next y

End If

If cf = False Then

han = han + 1

Sheets("biao3").Cells(han, 1) = Sheets("biao2").Cells(x, 1)

End If

Next x

End Sub

哈哈,学习了。UsedRange.Rows.Count:表中有数据的最后一行的行号。

2.vba如何将两个EXCEL工作簿 1工作簿单元格的内容写到2工作簿的指

最后一句应该是引用对应名称工作簿吧。具体如下:

Sub 引用()

n = 1 '项目总数,即汇总表A列行数

For i = 1 To n

shname = Sheets("汇总表").Range("A" & i).Value 'Ai的值,即工作簿名

Set ssh = Sheets(shname) '引用工作簿名对应工作簿

MsgBox ssh.Range("A1").Value, 0, "OK" '显示该对应工作簿A1的值,测试用,可删除

'ssh里的其他操作

Next

End Sub


电脑版

免责声明:本站信息来自网络收集及网友投稿,仅供参考,如果有错误请反馈给我们更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任,谢谢您的合作。
版权所有:五学知识网 Copyright © 2015-2026 All Rights Reserved .