[英]Excel/VBA How to move data from one worksheet to another after last used cell?
[英]Excel VBA: Add data to cell after last used row
我有一個用戶表單,並將用戶表單的標簽添加到所選工作表中。 這就是我嘗試過的。 現在的問題是為什么為什么一個單元格與其他單元格不在同一行?
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "Label" Then
With ActiveSheet
i = i + 1
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
If c <> "Chapter" Then
.Range(Cells(1, 1), Cells(1, i)).Name = "Chapter1"
.Range("Chapter1").Merge
.Range("Chapter1").Value = "Chapter 1"
.Range("Chapter1").HorizontalAlignment = xlCenter
.Cells(lastRow, i).Value = c.Caption
End If
End With
End If
Next
問題在於,第一次執行.Cells(.Rows.Count, 1).End(xlUp).Row
A2中還沒有任何內容,因此lastRow
將為1。但是一旦您將值設置為“ No. ” 在下一次您執行該代碼( i
為2)時,將在該單元格中填充A2,因此現在.Cells(.Rows.Count, 1).End(xlUp).Row
將返回2,為您帶來效果得到:所有其他值最終降低一排。
有幾種方法可以解決此問題,但這是一種方法。 添加+ IIf(i = 1, 1, 0)
lastRow
+ IIf(i = 1, 1, 0)
lastRow
的賦值:
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row + IIf(i = 1, 1, 0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.