使用VBA的方式在EXCEL中的sheet1和sheet2表格内容一样,将sheet2单元格的数值移动到sheet1相应的单元格中
sheet1和sheet2A列是名称;B1、C1、D1、E1、F1、G1.......是日期依此类推,B2、C2、D2、E2、F2、G2.........是数值依此类推。...
sheet1和sheet2 A列是名称;B1、C1、D1、E1、F1、G1.......是日期依此类推,B2、C2、D2、E2、F2、G2.........是数值依此类推。如何将sheet2单元格中有数值的单元格移动到sheet1相应的单元格内?谢谢!如
展开
展开全部
Sub Test()
Dim sht1 As Worksheet, sht2 As Worksheet
Dim myCell As Range, rng As Range
Set sht1 = Worksheets(1)
Set sht2 = Worksheets(2)
With sht2.Cells(1, 1).CurrentRegion
Set rng = sht2.Cells(2, 2).Resize(.Rows.Count - 1, .Columns.Count - 1)
End With
For Each myCell In rng.SpecialCells(xlCellTypeConstants)
With myCell
sht1.Cells(.Row, .Column) = .Value
End With
Next myCell
End Sub
详见附件。
更多追问追答
追问
您好!刚刚运行了一下下是能实现了,但是当SHEET2表中的A列名称顺序没有按照SHEET1的顺序排列的时候,SHEET2单元格的数值不能安照A列名称对号入座哦!谢谢!
追答
等下哦,是可以解决的。
Sub Test()
Dim sht1 As Worksheet, sht2 As Worksheet
Dim myCell As Range, rng As Range
Dim i As Integer, m As Integer
Dim j As Integer, n As Integer
Dim k As Integer
Set sht1 = Worksheets(1)
Set sht2 = Worksheets(2)
With sht1
m = .Cells(1, 1).CurrentRegion.Rows.Count
Set rng = .Range("A1:A" & m)
End With
With sht2.Cells(1, 1).CurrentRegion
m = .Rows.Count
n = .Columns.Count
End With
For i = 2 To m
On Error Resume Next
k = WorksheetFunction.Match(sht2.Cells(i, 1), rng, 0)
If Err.Number = 0 Then
On Error GoTo 0
For j = 2 To n
Set myCell = sht2.Cells(i, j)
If myCell <> "" Then
sht1.Cells(k, j) = myCell
End If
Next j
Else
Err.Clear
On Error GoTo 0
End If
Next i
End Sub
2015-05-15 · 知道合伙人互联网行家
关注
展开全部
确实不明白你的问题
追问
我怎么样才能上传EXCEL文档呢?
追答
上传到百度网盘就可以了,要不发我邮箱258347442
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你得上传附件,看看情况 这样描述很模糊的
更多追问追答
追问
名称
4月1日
4月2日
4月3日
4月4日
4月5日
A
1000
1000
1000
10
B
100
100
1000
C
100
1000
10
D
100
1000
E
100
100
1000
1000
10
追答
Sub DDD()
Dim str As Variant
With ThisWorkbook
For i = 2 To 256
For j = 1 To 65536
.Worksheets("Sheet2").Activate
If .Worksheets("Sheet2").Cells(i, j) "" Then
str = .Worksheets("Sheet2").Cells(i, j).Value
End If
.Worksheets("Sheet1").Activate
.Worksheets("Sheet1").Cells(i, j) = str
str = ""
Next i
Next j
End With
END SUB
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询