Excel vba - xlDown [英] Excel vba - xlDown

查看:172
本文介绍了Excel vba - xlDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码正在尝试将所选范围(以 rng 传递)粘贴到工作表的末尾。
如果已经有2行已经存在(A1,A2),它可以工作。

  Sub copyRow(rng As Range ,ws As Worksheet)
Dim newRange As Range
设置newRange = ws.Range(A1)。End(xlDown).Offset(1,0)
rng.Copy
newRange.PasteSpecial(xlPasteAll)
End Sub

所以,如果A1和A2存在如果您调用此方法100次,则会在其后插入100行。但是,如果没有行存在或只有A1,它只是覆盖A2。我可以在同一行看到excel写(覆盖)



显示,与xlDown如果计算是否少于2行有关,不确定。 p>

任何帮助将不胜感激。

解决方案

对不起,但我不同意与Michael的答案。



End(xlDown)是VBA等效于点击 Ctrl +



尝试 Ctrl + 下来




  • 一个空列

  • 第1行,第2行,第3行,第7行,第8行,第13条,第14条和第15条中第1行和第2行的其他





这将让您了解所有不同的行, Ctrl + Down 可能会带你到。



设置newRange = ws.Range(A1 ).End(xlDown).End(xlDown).End(xlUp).Offset(1,0)不一定会带你到最后使用的行加1。



我很惊讶设置newRange = ws.Range(A1)。End(xlDown).Offset(1,0)柱。 Range(A1)。End(xlDown)将带你到表格的底部,然后 .Offset(1,0)会尝试把你带走。



考虑:

  Dim RowLast As Long 

RowLast = ws.Cells(Rows.Count,A)。End(xlUp).Row
/ pre>


  • 如果列A为空,则RowLast将设置为1。

  • 如果A1有一个值,但没有其他单元格有值,RowLast将被设置为1。

  • 如果列A中的一些单元格具有值,RowLast将被设置为具有值的底行。

  • 如果在最后一行中有值,它将被忽略。

  • 如果您在最后两行中有值,则RowLast将设置为Rows.Count - 1。



我假设你在borrom行中没有值。如果您不在乎第1行是否为空白栏,则:

  RowLast = ws.Cells(Rows。 Count(A)。End(xlUp).Row 
设置NewRange = ws.Cells(RowLast + 1,A)

应该给出所需的结果,而不管sheet ws的当前内容如何。



如果您关心第1行留空,则尝试使用 Ctrl + Down Ctrl + 向上将使您了解不同组合值的影响。


The code below is trying to paste the selected range (passed as rng) to the end of the worksheet. It works if there is already 2 rows already present(A1, A2).

Sub copyRow(rng As Range, ws As Worksheet)
    Dim newRange As Range
    Set newRange = ws.Range("A1").End(xlDown).Offset(1, 0)
    rng.Copy
    newRange.PasteSpecial (xlPasteAll)
End Sub

So, if A1 and A2 present and if you call this method 100 times, it inserts 100 rows after them. But, if no rows present or only A1, it just overwrite A2. I could see excel write on the same row (overwrite)

Appears that, something to do with how xlDown calculates if there are less than 2 rows, not sure.

Any help would be appreciated.

解决方案

Sorry but I do not agree with Michael's answer.

End(xlDown) is the VBA equivalent of clicking Ctrl+Down.

Try Ctrl+Down with

  • an empty column
  • a column with a value in row 1 but no other
  • values in rows 1 and 2
  • values in rows 1, 2, 3, 7, 8, 9, 13, 14 and 15

This will give you an idea of all the different rows, Ctrl+Down might take you to.

Set newRange = ws.Range("A1").End(xlDown).End(xlDown).End(xlUp).Offset(1, 0) does not necessarily take you to the last used row plus 1.

I am surprised Set newRange = ws.Range("A1").End(xlDown).Offset(1, 0) worked with an empty column. Range("A1").End(xlDown) would take you to the bottom row of the sheet then .Offset(1, 0) would try to take you off the sheet.

Consider:

Dim RowLast As Long

RowLast = ws.Cells(Rows.Count, "A").End(xlUp).Row

  • If column A is empty, RowLast will be set to 1.
  • If A1 has a value but no other cells have values, RowLast will be set to 1.
  • If a number of cells in column A have values, RowLast will be set to the bottom row with a value.
  • If you have a value in the final row, it will be ignored.
  • If you have a value in the final two rows, RowLast will be set to Rows.Count - 1.

I assume you do not have values in the borrom rows. If you do not care if row 1 is left blank with an empty column, then:

RowLast = ws.Cells(Rows.Count, "A").End(xlUp).Row
Set NewRange = ws.Cells(RowLast + 1, "A")

should give the desired result regardless of the current contents of sheet ws.

If you do care about row 1 being left blank, experimenting with Ctrl+Down and Ctrl+Up will give you an understanding of the effect of different combinations of values.

这篇关于Excel vba - xlDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆