簡體   English   中英

VBA-excel中的動態選擇

[英]dynamic select in VBA-excel

我正在嘗試基本實現復制和粘貼作業的自動化。 我想將數據從一個文檔轉移到另一個文檔。 我想根據不總是位於同一位置的單元格中的內容查找數據,我想選擇該單元格下方的值,直到下一個空白行。

例如:在顯示“ CURRENT MONTH”的單元格下面的區域中選擇所有單元格,直到下一行為空。

這是我到目前為止的內容:

Sub getCurrentMonth()

'get the current month data
Windows("File1.xlsm").Activate
Sheets("Sheet1").Select
celltxt = ActiveSheet.Range("B1:B1000").Text
If InStr(1, celltxt, "CURRENT MONTH") Then
N = Cells(7, 2).End(xlDown).Select
Range("B7:AD" & N).Select
Selection.Copy
Windows("Automation.xlsm").Activate
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Cells(rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial
Else
MsgBox ("No data for Current Month Found")
End If    

End Sub

試一下。 根據您的描述,它假定“當前月份”下的所有數據都是連續的。 如果不是,請告訴我,我將進行編輯。

Option Explicit

Sub getCurrentMonth()

    'get the current month data
    With Workbooks("File1.xlsm").Worksheets("Sheet1")

        Dim foundIt As Range
        Set foundIt = .Range("B1:B1000").Find("CURRENT MONTH", lookat:=xlWhole)

        If Not foundIt Is Nothing Then

            Set foundIt = .Range(foundIt.Offset(1,-1), foundIt.End(xlDown)) 'from column A and down
            Set foundIt = foundIt.Resize(foundIt.Rows.Count,29) 'from column A to AD
            Workbooks("Automation.xlsm").Worksheets("Sheet1").Range("A2").Resize(foundIt.Rows.Count, foundIt.Columns.Count).Value = foundIt.Value

        Else

            MsgBox ("No data for Current Month Found")

        End If

    End With

End Sub

暫無
暫無

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

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