简体   繁体   English

复制F列中具有特定值的范围内的行

[英]copy rows in range with specific value in column F

I have an worksheet with lots of columns and also lots rows. 我有一个工作表,其中包含很多列和很多行。 from this worksheet I want to copy the rows which match 2 conditions: 1. the value in column B must match the selected value from a dropdown list in a different worksheet 2. the value in column F must match with the selected value from a different dropdown list. 从此工作表中,我要复制匹配2个条件的行:1. B列中的值必须与另一个工作表中的下拉列表中选择的值匹配2. F列中的值必须与从其他工作表中选择的值匹配下拉列表。

I have a script that works for condition one. 我有一个适用于条件一的脚本。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim fRow As Integer, lRow As Integer
Dim value As String
Dim mychart As chart
Dim mycharts As ChartObject

If ActiveCell.Address = Sheets("blad1").Cells(1, 1).Address Then

Sheets("chartdata").Cells.ClearContents

For Each ChartObject In Sheets("blad3").ChartObjects
ChartObject.Delete
Next

value = Sheets("blad1").Cells(1, 1).value

With Sheets("schaduwblad")
fRow = .Range("B:B").find(what:=value, after:=Range("B1")).Row
lRow = .Range("B:B").find(what:=value, after:=Range("B1"), lookat:=xlWhole, searchdirection:=xlPrevious).Row
.Range("B1:DT1").Copy _
Sheets("chartdata").Range("A1")
.Range("B" & fRow, "DT" & lRow).Copy _
Sheets("chartdata").Range("A2")


  With Sheets("blad3")
  Set mychart = .Shapes.AddChart.chart

    With mychart
      .SetSourceData Source:=Sheets("chartdata").Range("B1").CurrentRegion
      .ChartType = xlLine
      .HasTitle = True
      .HasLegend = True

      With .ChartTitle
      .Text = "=Blad1!R1C1"
      .AutoScaleFont = False
      .Font.FontStyle = "verdana"

      End With
      With mychart.Legend

        .FontSize = 8
        .Position = xlLegendPositionBottom
        .AutoScaleFont = False
        .Font.FontStyle = "verdana"
        .FontSize = 8
      End With

    End With
  End With
End With
End If
End Sub

But I cannot create the script that is needed to also match condition 2. 但是我无法创建同时满足条件2所需的脚本。

Here is a screenshot from the structure the document has: 这是文档结构的屏幕截图: https://i.imgsafe.org/5e7034c.png
(source: imgsafe.org ) (来源: imgsafe.org

The first condition is that the match with the value in column B. That is a closed range which can easily be copied. 第一个条件是与B列中的值匹配。这是一个容易复制的封闭范围。 But the second condition uses the value in column F, which is changing every row. 但是第二个条件使用F列中的值,该值在每一行中都在变化。

For example, based on the screenshot, I want to select all the rows which have the value NL Food in column B and the Omzet (x 1000) in column F. (so rows which have Verkopen (x1000) in verpakkingen) have to be excluded from the selection. 例如,基于屏幕截图,我想选择在B列中具有NL Food值和在F列中具有Omzet(x 1000)的所有行。(因此在verpakkingen中具有Verkopen(x1000)的行)必须是从选择中排除。

(the choice for omzet (x 1.000) or Verpakking (x 1.000) is also made using a drop down list). (也可以通过下拉列表选择omzet(x 1.000)或Verpakking(x 1.000))。

How can I make VBA to only select the rows which meet both conditions? 如何使VBA仅选择同时满足两个条件的行?

Edit: 编辑:

I was able to change the data layout so that now FCT is in column B directly after MKT. 我能够更改数据布局,以便现在FCT在MKT之后的B列中。 This way, all data is first sorted on MKT and after that on FCT so my data layout should make it easier to select the area which matches both conditions, as it is one closed range. 这样,所有数据首先在MKT上排序,然后在FCT上排序,因此我的数据布局应使其更容易选择匹配两个条件的区域,因为它是一个封闭范围。 http://i.imgsafe.org/00db13c.png

Therefore, I thought I was able to change the code and have both conditions met. 因此,我认为我能够更改代码并同时满足两个条件。

I added a frow2 and lrow2 which now have to find the value2 parameter in column B. However, with the code which is posted below, I get an Error 13 message saying "types do not match". 我添加了一个frow2lrow2 ,它们现在必须在B列中找到value2参数。但是,使用下面发布的代码,我收到一条错误13消息,提示“类型不匹配”。 I do not understand why that is. 我不明白为什么会这样。 I guess it has something to do with the way I defined the search range for frow2 and lrow2. 我想这与我为frow2和lrow2定义搜索范围的方式有关。

Part of the adjusted code is below, I added the italic lines 调整后的代码部分如下,我添加了斜体行

Private Sub Worksheet_Change(ByVal Target As Range)
Dim fRow As Integer, lRow As Integer, frow2 As Integer, lrow2 As Integer

Dim value As String
Dim value2 As String
Dim mychart As chart
Dim mycharts As ChartObject

If ActiveCell.Address = Sheets("blad1").Cells(1, 1).Address Then

Sheets("chartdata").Cells.ClearContents

For Each ChartObject In Sheets("blad3").ChartObjects
ChartObject.Delete
Next

value = Sheets("blad1").Cells(1, 1).value
value2 = Sheets("blad1").Cells(1, 3).value

With Sheets("schaduwblad")
fRow = .Range("A:A").find(what:=value, after:=Range("A1")).Row
lRow = .Range("A:A").find(what:=value, after:=Range("A1"), lookat:=xlWhole, searchdirection:=xlPrevious).Row
frow2 = .Range(.Cells(fRow, 2), .Cells(lRow, 2)).find(what:=value2, after:=Range("B1"), lookat:=xlWhole).Row
lrow2 = .Range(.Cells(fRow, 2), .Cells(lRow, 2)).find(what:=value2, after:=Range("B1"), lookat:=xlWhole, searchdirection:=xlPrevious).Row
.Range("E1:DS1").Copy
Sheets("chartdata").Range("A1")
.Range("E" & fRow, "DS" & lrow2).Copy_
Sheets("chartdata").Range("A2")_

EDIT 2: 编辑2:

I tried this line (see below) to find out why I get the error 13. 我尝试了这一行(请参阅下文),以找出导致错误13的原因。

frow2 = .Range("B:B").find(what:=value2, after:=Range("B1"), lookat:=xlWhole).Row

Where I use the entire column B as search range. 我将整个B列用作搜索范围。 This works fine for the find methode. 这对于find方法很有效。 As soon as I change the range to anything else I get the error 13 message: types do not match. 一旦将范围更改为其他任何内容,我都会收到错误13消息:类型不匹配。

It seems the range.find method cannot work with ranges which are defined more then just an entire column? 似乎range.find方法不能与仅定义整列的范围一起使用? (eg B2:B41). (例如B2:B41)。

Edit 3: The reason I got the error 13 message was that I searched in a range for example B2:B41 and in the find. 编辑3:我收到错误13消息的原因是我在例如B2:B41的范围内以及查找中进行了搜索。 parameters I entered B1 as find.after range. 我输入参数B1作为范围之后的find。 I changed it now like this and it works: 我现在这样更改它,并且可以正常工作:

frow2 = .Range(.Cells(fRow, 2), .Cells(lRow, 2)).find(what:=value2, after:=Range("B" & fRow), lookat:=xlWhole).Row
lrow2 = .Range(.Cells(fRow, 2), .Cells(lRow, 2)).find(what:=value2, after:=Range("B" & fRow), lookat:=xlWhole, searchdirection:=xlPrevious).Row

Ok, I ll go with another way. 好吧,我会另辟go径。 You can use ADO SQL connection to get what you want. 您可以使用ADO SQL连接来获取所需的内容。 I have assumed your source sheet is schaduwlab and I copied query results to a sheet named Sheet1 . 我假设您的源工作表是schaduwlab并且我将查询结果复制到了名为Sheet1的工作表Sheet1 You can change them according to your own work. 您可以根据自己的工作进行更改。

Sub tadaaa()

Dim con As Object, rs As Object
Dim query As String
Dim connector As String
Dim adres As String


    Set con = CreateObject("adodb.connection")
    Set rs = CreateObject("adodb.recordset")

    adres = ThisWorkbook.FullName

    connector = "provider=microsoft.ace.oledb.12.0;data source=" & _
             adres & ";extended properties=""Excel 12.0 Macro;hdr=yes"""

    con.Open connector


    query = "select * from [schaduwblad$] where FCT = ""Omzet (x 1000)"" AND MKT = ""NL Food"""
                            'Source sheet


    Set rs = con.Execute(query) 'Execute the query

    'Recording query results to any sheet you want.
    Sheets("Sheet1").Range("A65536").End(3).Offset(1, 0).CopyFromRecordset rs

    For j = 0 To rs.Fields.Count - 1 'For the headers
        Sheets("Sheet1").Cells(1, j + 1).Value = rs.Fields(j).Name
    Next j


Set rs = Nothing

Set con = Nothing


End Sub

To get results, you should include ADO and SQL libraries from Tools/References in vba page. 为了获得结果,您应该在vba页面的“ Tools/References中包含ADO和SQL库。 I couldn't check because of some works to do. 由于某些工作,我无法检查。 But I arranged it from another vba that I have used before. 但是我是从以前使用过的另一个vba安排的。

Edit: I had tried and it worked. 编辑:我尝试过,并且有效。 Also changed quotes in query. 还更改了查询中的引号。

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

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