问题描述
我正在尝试减少对select的使用.我一直在参考下面的文章,但我不完全确定如何在这种情况下实现它.为了练习,我有下面的工作代码集,用于复制B1中的值并将其粘贴到A列中填充的尽可能多的行中.有效的代码,我将不胜感激!
I am trying to reduce my usage of select. I have been referencing the article below but I am not entirely sure how to implement it in this scenario. To practice I have the working set of code below that I use to copy the value in B1 and paste it down as many rows that are filled in column A. If someone could coach me through how to avoid this bad habit in order to write more effective and efficient code, I would greatly appreciate it!
How to avoid using Select in Excel VBA
Range("B1").Select
Selection.Copy
ActiveCell.Offset(0, -1).Select
Selection.End(xlDown).Select
ActiveCell.Offset(0, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Copy
Range(Selection, "B1").Select
ActiveSheet.Paste
All of that code can be translated to this:
Range("B1:B" & Cells(Rows.Count, "A").End(xlUp).Row).Value = Range("B1").Value
We're making the Range
of cells B1
through B
and the last row of cells in column A
(which we get by using Cells(Rows.Count, "A").End(xlUp).Row
) equal to the value in cell B1
.
这篇关于避免通过复制和粘贴VBA选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!