2015-03-03 52 views
0

我在網上搜索了高和低,以找到我的宏觀問題的答案 - 我不認爲我想這樣做是困難的,但也許它是!宏用固定的列複製可變數量的行

在我的數據集列K包含我的數據標記。我想要做的是在列K中找到「22」,並找到「23」爲K列,並複製列C中包含的標記所表示的行號之間的列C到J中的所有數據行。標記22和23之間的行對於每個電子表格都不同,這是我遇到問題的地方。我試圖設置這些變量的範圍,但我基本上是一個宏觀新手,並沒有多少運氣。任何幫助不勝感激。

回答

2

也許最簡單的方法就是隻要找到22 & 23和地方建立的行位於

然後你就可以創建範圍與已知的行和列進行復制。 爲此示例命名工作表「複製到工作表」。

Sub Button1_Click() 
    Dim Rws As Long, Rng As Range 
    Dim c1 As String, c2 As String 
    Dim FndC1 As Range, FndC2 As Range 
    Dim c1C As Integer, c2C As Integer 
    Dim ws As Worksheet 
    Set ws = Sheets("Copy to Sheet") 
    c1 = 22 
    c2 = 23 
    Rws = Cells(Rows.Count, "K").End(xlUp).Row 
    Set Rng = Range(Cells(1, "K"), Cells(Rws, "K")) 

    Set FndC1 = Rng.Find(what:=c1, lookat:=xlWhole) 
    Set FndC2 = Rng.Find(what:=c2, lookat:=xlWhole) 

    If Not FndC1 Is Nothing Then 
     c1C = FndC1.Row 
    Else: MsgBox c1 & " Not Found" 
     Exit Sub 
    End If 
    If Not FndC2 Is Nothing Then 
     c2C = FndC2.Row 
    Else: MsgBox c2 & " Not Found" 
     Exit Sub 
    End If 
    Range(Cells(c1C + 1, "C"), Cells(c2C - 1, "J")).Copy _ 
      ws.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 


End Sub 
+1

這是驚人的 - 完美的作品!非常感謝你,你用這段代碼爲我節省了無限的時間。 – 2015-03-03 14:22:49