0

私は Excel VBA の初心者で、何かに行き詰まっています。いくつか試してみましたが、それを正しく理解するのに十分な知識がありません。

これが問題です。workbook1 にカレンダーから開始日と終了日を選択するフォームがあり、選択したらボタンを押して、閉じたファイルからコピーする必要があります。日付から終了日まで。

したがって、2013 年 8 月 19 日から 2013 年 8 月 25 日までを選択した場合、要素 2 から要素 11 を workbook1 にコピーします。

Workbook2 (数千の要素の日付など):

 ╔═══╦════════════╦═════════════╦═════════════╦═════════════╦═════════════╗
 ║   ║    A       ║      B      ║      c      ║      D      ║      E      ║
 ╠═══╬════════════╬═════════════╬═════════════╬═════════════╬═════════════╣
 ║ 1 ║ Type       ║ Element 1   ║             ║ 16-08-2013  ║ 18-08-2013  ║
 ║ 1 ║ Type       ║ Element 2   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 2 ║ Header     ║ Element 3   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 3 ║ Auto Align ║ Element 4   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 4 ║ Auto Align ║ Element 5   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 5 ║ Auto Align ║ Element 6   ║             ║ 19-08-2013  ║ 22-08-2013  ║
 ║ 6 ║ Auto Align ║ Element 7   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 7 ║ Auto Align ║ Element 8   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 8 ║ Auto Align ║ Element 9   ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║ 9 ║ Auto Align ║ Element 10  ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║10 ║ Auto Align ║ Element 11  ║             ║ 23-08-2013  ║ 25-08-2013  ║
 ║11 ║ Auto Align ║ Element 12  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║12 ║ Auto Align ║ Element 13  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║13 ║ Auto Align ║ Element 14  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║14 ║ Auto Align ║ Element 15  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║15 ║ Auto Align ║ Element 16  ║             ║ 26-08-2013  ║ 01-09-2013  ║
 ║.. ║    ...     ║     ...     ║     ...     ║     ...     ║     ...     ║
 ║ n ║     n      ║ Element n   ║             ║  start date ║ end date    ║
 ╚═══╩════════════╩═════════════╩═════════════╩═════════════╩═════════════╝

ワークブック1:

╔═══╦════════════╗
║   ║    A       ║
╠═══╬════════════╣
║ 1 ║ Element 2  ║
║ 2 ║ Element 3  ║
║ 3 ║ Element 4  ║
║ 4 ║ Element 5  ║
║ 5 ║ Element 6  ║
║ 6 ║ Element 7  ║
║ 7 ║ Element 8  ║
║ 8 ║ Element 9  ║
║ 9 ║ Element 10 ║ 
║10 ║ Element 11 ║
╚═══╩════════════╝

これは、これまでの更新(actualizar)ボタンにあるものです。

プライベート サブ actualizar_Click()

If calendario.SelStart + 6 = calendario.SelEnd Then //calendario is the calendar
    Sheets("variables").Range("B1").Value = calendario.SelStart //i just copy the
    Sheets("variables").Range("B2").Value = calendario.SelEnd   //selected date to wb1

    '///// code to get data

    Dim wb As Workbook
    Application.ScreenUpdating = False ' turn off the screen updating
    Set wb = Workbooks.Open("C:\Users\G\Desktop\AnalyticsBuilder\Panel a completarCOPIA.xlsx", True, True)
    ' open the source workbook, read only


    Dim c As Range
    Dim x As Range
    Set x = Range("C5")

    For Each c In wb.Worksheets("2012").Range("K:K")
        If c.Value >= calendario.SelStart And c.Value <= calendario.SelEnd Then

          ThisWorkbook.Worksheets("variables").x.Value = wb.Worksheets("2012").c.Value

        End If
    Next c


    wb.Close False ' close the source workbook without saving any changes
    Set wb = Nothing ' free memory

    Application.ScreenUpdating = True ' turn on the screen updating
    Unload Me

ElseIf calendario.SelStart + 6 <> calendario.SelEnd Then
    MsgBox ("Seleccionar semana completa"), , "Error"
End If

サブ終了

閉じた wb2 からセルをコピーしようとして成功しましたが、要素を取得するためのこのコードは機能していません。

また、閉じた wb からコピーすると、データを取得する前に数秒間 Excel がフリーズしますが、それを修正する方法はありますか?

よろしくお願いいたします。

4

2 に答える 2

0

ここを見てください。しかし、以下は私が時々使うものです。

Function xFind2B(xS As Worksheet)
    On Error Resume Next

    Dim c As Range
    Dim xRng As Range
    Set xRng = xS.Range("D1:D20")

    For Each c In xRng
        If c.Value => textbox.value And <= textbox2.value Then
            'Do Your copy
            Exit For
        End If
    Next c

    xFind2BlanksA = c.Offset(-1).Row

    On Error GoTo 0
End Function
于 2013-09-01T08:22:01.767 に答える