查找某个标头并在其下复制一定数量的条目

时间:2019-05-27 14:05:57

标签: excel vba

我正在尝试搜索某个标头(在此示例中为标头1),并将该标头下方的一定数量的单元格复制到一个范围内。标头本身的文本不应该被复制,而是其下面的所有内容。

以下代码有效,但是它复制了太多行。当我使用For x = 0 To 3时,它会将“标题1”下面的下10行复制,而不是仅复制3行。我找不到我的错误。

Sub FindCopyPasteV2()

    With Worksheets("Sheet1").Range("A:FF")

        Dim FindEQ3 As Range
        Dim TestR As Range
        Dim x As Long

           Set FindEQ3 = .Find(What:="Header 1", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)

              For x = 0 To 3

                 Set FindEQ3 = FindEQ3.Resize(FindEQ3.Rows.Count + x).Offset(1)
                 Set TestR = .Range("K" & 5 + x)

                 FindEQ3.Copy TestR

              Next x

    End With

End Sub

enter image description here

2 个答案:

答案 0 :(得分:1)

我认为您的问题仅仅是您做了两次。循环的目的是确保复制3行,但实际上FindEQ3.Resize已经做到了。

此代码应更接近您想要的代码。我主要只是把循环带出来。

Sub FindCopyPasteV2()

    With Worksheets("Sheet1").Range("A:FF")

        Dim FindEQ3 As Range
        Dim TestR As Range

        Const NUM_ROWS_COPY As Long = 3

        Set FindEQ3 = .Find(What:="Header 1", LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)

        Set FindEQ3 = FindEQ3.Resize(NUM_ROWS_COPY).Offset(1)
        Set TestR = .Range("K" & 5)

        FindEQ3.Copy TestR


    End With

End Sub

答案 1 :(得分:0)

我不确定为什么要使用for循环复制多个单元格。请找到下面的代码。这应该找到您想要的标题,然后跨其他位置“复制”值。

public partial class Form1 : Form
{


    public Form1()
    {
        InitializeComponent();
    }

    Thread h1;
    int cont = 0;
    string[] arrayUrls = new[] { "https://www.bbc.com/", "https://php.net/", "https://www.nytimes.com/" };

    private void button_start_Click(object sender, EventArgs e)
    {
        h1 = new Thread(new ThreadStart(startApplication));
        h1.Start();
    }

    private void startApplication() {
        if (cont < arrayUrls.Count())
        {
            ThreadController.InvokeIfRequired(webBrowser1, () =>
            {
                webBrowser1.ScriptErrorsSuppressed = true;
                webBrowser1.Navigate(arrayUrls[cont]);
            });
        }
        else {
            MessageBox.Show("end of the program");
        }
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        var br = sender as WebBrowser;
        if (br.Url == e.Url)
        {
            cont++;
            ThreadController.InvokeIfRequired(webBrowser1, () =>
            {
                int hightWeb = webBrowser1.Document.Window.Size.Height;

                for (int i = 0; i < hightWeb; i = i + 100)
                {
                    webBrowser1.Document.Window.ScrollTo(0, i);

                    //Sleep 50 miliseconds
                    Thread.Sleep(50);
                }

            });

            startApplication();
        }
    }
}

static class ThreadController
{
    public static void InvokeIfRequired(this Control control, MethodInvoker action)
    {
        if (control.InvokeRequired)
        {
            control.Invoke(action);
        }
        else
        {
            action();
        }
    }
}