简体   繁体   English

如何在VBA中使用两个变量选择行范围

[英]How to select a range of rows using two variables in VBA

I'm brand new to VBA and am trying to write a macro to pull specific data from all workbooks in the same directory into a master workbook. 我是VBA的新手,正在尝试编写宏以将特定数据从同一目录中的所有工作簿提取到主工作簿中。 I'm getting stuck when trying to select a range of data using variables so that I can copy and paste the data into the master workbook. 尝试使用变量选择数据范围时遇到麻烦,因此我可以将数据复制并粘贴到主工作簿中。 I've been watching videos and reading through forums, but can't seem to get the macro to work. 我一直在观看视频并通过论坛阅读,但似乎无法使宏正常工作。

I have an Excel sheet that lists employees in column A, with all the data I want to copy about the employees in columns B, C, D, E, and F (in subsequent rows). 我有一个Excel工作表,该工作表在A列中列出了雇员,并在B,C,D,E和F列中(随后的行中)列出了我要复制的所有有关雇员的数据。 So for example, row 1 contains the first employee in cell A1, and then rows 2 through 5 contains the data in columns B through F. Row 6 contains the next employee's name in cell A6, and the data about them resides in rows 7 through 9 (columns BF). 因此,例如,第1行包含单元格A1中的第一个雇员,然后第2行至第5行包含B至F列中的数据。第6行包含单元格A6中的下一个雇员姓名,有关他们的数据位于第7行至第6行中9(BF列)。 I want to copy rows 2-5 and paste them into the master workbook, and then copy 7-9 and paste into master, 8-14, and so on and so forth. 我想复制2-5行并将其粘贴到主工作簿中,然后复制7-9行并粘贴到主工作簿中,8-14,依此类推。

My first attempt was to define two variables as integers. 我的第一次尝试是将两个变量定义为整数。 Then I tried to find the name of the first employee in column A and select the row after, and set the first variable equal to that row. 然后,我尝试在A列中找到第一个雇员的姓名,然后选择后面的行,并将第一个变量设置为等于该行。 Then find the name of the second employee, select the row before and set variable 2 equal to that row. 然后找到第二个雇员的姓名,选择前面的行,并将变量2设置为等于该行。 Then select the range using those two variables. 然后使用这两个变量选择范围。 Here's what my code looks like: 这是我的代码:

Sub SelectConsultantData()
Dim Consultant1 As Integer, Consultant2 As Integer
Dim ConsultantRange As Range

    Columns("A:A").Select
    Selection.Find(What:="Andrew", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Consultant1 = Rows(ActiveCell.Row).Select
    Consultant1 = Consultant1 + 1
    Columns("A:A").Select
    Selection.Find(What:="Bob", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Consultant2 = Rows(ActiveCell.Row).Select
    Consultant2 = Consultant2 - 1
    Set ConsultantRange = Range(Consultant1, Consultant2).Select

End Sub

Any idea what I'm doing wrong, or can anyone think of a better approach? 知道我在做什么错吗,还是有人可以想到更好的方法? Also please let me know if I need to provide further context. 另外,请让我知道是否需要提供进一步的信息。

Thanks in advance for any help. 在此先感谢您的帮助。

Your code can be re-written as below. 您的代码可以如下重写。 Avoid using Select in your code. 避免在代码中使用“选择”。 Check this link to know why. 检查此链接以了解原因。

Sub SelectConsultantData()
    Dim Consultant1 As Integer, Consultant2 As Integer
    Dim ConsultantRange As Range

    Dim rngFind As Range
    Set rngFind = Columns("A:A").Find(What:="Andrew", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not rngFind Is Nothing Then
        Consultant1 = rngFind.Row + 1
    End If

    Set rngFind = Columns("A:A").Find(What:="Bob", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)

    If Not rngFind Is Nothing Then
        Consultant2 = rngFind.Row - 1
    End If

    If Consultant1 > 0 And Consultant2 > 0 Then
        Set ConsultantRange = Range(Cells(Consultant1, 2), Cells(Consultant2, 6))
    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM