简体   繁体   English

VBA宏无法通过ActiveCell找到最后使用的单元格

[英]VBA Macro can't find the last used cell with ActiveCell

I would like to copy paste some values from one (src)Workbook to my (dst)workbook by finding the last not empty cell. 我想通过查找最后一个非空单元格将粘贴的一些值从一个(src)工作簿复制到我的(dst)工作簿。

In my case, the copy paste is working fine until the the src Workbook has no entry. 就我而言,复制粘贴工作正常,直到src Workbook没有条目。

Outcome of my macro looks like: 我宏的结果如下:

Header - |email   |Name  |origin
1.row  - |email3  |Name2 |CH3
2.row  - |email2  |empty |ch2

What I expected is: 我所期望的是:

Header - email  |Name    |origin
1.row  - email3 |x       |CH3
2.row  - email2 |Name2   |ch2

My code block: 我的代码块:

Workbooks.Open (Filepath & MyFile)
Range("A2").Copy
ActiveWorkbook.Close savechanges:=False

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("AP217").Range(Cells(erow, 1), Cells(erow, 1))

If ActiveCell.Value = "" Then
ActiveCell.Value = "x"
End If

It would be great, if someone could advise. 如果有人可以提供建议,那将是很好。

Cheers 干杯

I would simply handle the error, something like this: 我只会处理错误,如下所示:

erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row

On Error Resume Next ' ADD THIS LINE

ActiveSheet.Paste Destination:=Worksheets("AP217").Range(Cells(erow, 1), Cells(erow, 1))

On Error GoTo 0 ' ADD THIS LINE -- this cancels the "Resume next", just in case...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM