簡體   English   中英

Select 引用列中最后一個填充的單元格

[英]Select the last filled cell from the referenced column

我試圖找到最后一個空列並寫下列名。 代碼通過它的名稱找到這個新列,然后選擇第二個單元格並將 B 列和 C 列中的值粘貼到 exceldown。 但是,代碼用連字符“-”填充了 D 列的所有空白單元格。 我想以某種方式定義最后一個填充的單元格並粘貼到 B 列的最后一個填充單元格。 我面臨語法錯誤。 任何線索我如何實現它? 謝謝!

表“水果蔬菜

一個 C
1 類別 水果 蔬菜
2 空白的 蘋果 菠菜
3 空白的 橙子 卷心菜

工作表“ FruitsVege ”中的所需結果

一個 C D
1 類別 水果 蔬菜 水果蔬菜
2 空白的 蘋果 菠菜 蘋果菠菜
3 空白的 橙子 卷心菜 橙白菜

編碼

Sub Merge_FV ()
  
Dim r1 As Range, r2 As Range, r3 As Range

Dim emptyColumn As Long


'find empty Column (actually cell in Row 1)'
emptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If

 Cells(1, emptyColumn).Value = "FruitsVege"

With Rows(1)
    Set r1 = .Find(What:="Fruits", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set r2 = .Find(What:="Vegetables", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set r3 = .Find(What:="FruitsVege", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
     If Not r1 Is Nothing And Not r2 Is Nothing And Not r3 Is Nothing Then
         r3.Offset(1, 0).Select
         Range(ActiveCell, ActiveCell.End(xlDown)).Formula = "=" & r1.Offset(1).Address(0, 0) & " & ""-"" & " & r2.Offset(1).Address(0, 0)
    End If
End With

     
End Sub

未經測試但應該可以工作 - 我剛剛修改了公式行。 您需要從工作表底部向上查找最后一行,並且不需要 select 任何東西。

Sub Merge_FV()
  
Dim r1 As Range, r2 As Range, r3 As Range

Dim emptyColumn As Long, LastRow As Long

'find empty Column (actually cell in Row 1)'
emptyColumn = Cells(1, Columns.Count).End(xlToLeft).Column
If emptyColumn > 1 Then
    emptyColumn = emptyColumn + 1
End If

Cells(1, emptyColumn).Value = "FruitsVege"

With Rows(1)
    Set r1 = .Find(What:="Fruits", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set r2 = .Find(What:="Vegetables", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
    Set r3 = .Find(What:="FruitsVege", Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not r1 Is Nothing And Not r2 Is Nothing And Not r3 Is Nothing Then
        LastRow = Cells(Rows.Count, r1.Column).End(xlUp).Row
        r3.Offset(1, 0).Resize(LastRow - 1).Formula = "=" & r1.Offset(1).Address(0, 0) & " & ""-"" & " & r2.Offset(1).Address(0, 0)
    End If
End With

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM