Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA,对于每个应用程序匹配,需要对象_Excel_Vba - Fatal编程技术网

Excel VBA,对于每个应用程序匹配,需要对象

Excel VBA,对于每个应用程序匹配,需要对象,excel,vba,Excel,Vba,在应用程序匹配中循环出现问题。 我在K.Offset0,1行上得到一个所需的错误对象。复制FV.Offset2,0 守则应 1个循环通过CS范围 2当CS在FV范围内匹配时 3将CS Offset0,1中的单元格输入FV 2列,在Offset2,0上方 这是我的全部代码: Sub n() Dim FV As Variant Dim CS As Variant Dim K As Variant FV = Sheets("NEW").Range("A28:A34").Value CS = She

在应用程序匹配中循环出现问题。 我在K.Offset0,1行上得到一个所需的错误对象。复制FV.Offset2,0

守则应

1个循环通过CS范围

2当CS在FV范围内匹配时

3将CS Offset0,1中的单元格输入FV 2列,在Offset2,0上方

这是我的全部代码:

Sub n()

Dim FV As Variant
Dim CS As Variant
Dim K As Variant

FV = Sheets("NEW").Range("A28:A34").Value
CS = Sheets("CS").Range("A1:L1").Value

For Each K In CS
    If Not IsError(Application.Match(CS, FV, 0)) Then
        K.Offset(0, 1).Copy FV.Offset(2, 0)
        Else:
    End If
Next K

End Sub

您可以使用纯VBA函数,如:

Sub CopyMatchingValues()
    Dim FV As Range
    Dim CS As Range
    Dim cellFV As Range
    Dim cellCS As Range

    Set FV = Sheets("NEW").Range("A28:A34")
    Set CS = Sheets("CS").Range("A1:L1")

    For Each cellCS In CS.Cells
        For Each cellFV In FV.Cells
            If cellFV.Value = cellCS.Value Then
                cellFV.Offset(2, 0).Value = cellCS.Offset(0, 1).Value
            End If
        Next
    Next
End Sub
多么精彩的比赛啊。第一场比赛的问题
CS等不是范围,而是数组。请了解如何使用ArrayNK或FV都是范围对象。一个是单个值,另一个是二维值数组。对不起,伙计们,我弄错了,我试着让它看起来更干净一些,变量实际上现在已经拼写好了。你们想要数组的For循环吗。当您应该使用单个值时,您正在尝试使用数组与CS进行匹配。并且k不是所述的范围对象。通过使用.Value,您可以生成二维数组。阅读帮助页面,了解有关和的信息,尤其是。很难从你的问题中准确地说出你对数据有什么要求,以及你对结果有什么要求。多亏它起了作用,只需调整偏移量,我就得到了行和列的混合。是的,更容易理解,只是希望。。我知道。。关于与数组循环。非常好!谢谢工作速度比上面的快10倍。如何添加更多这些/更多来源?比如说我想按照类似的匹配逻辑从另一张纸上复制粘贴到FV中?发布另一个问题,我们拭目以待。几次之后你就可以自己做了。你可以在我的答案中找到更多的例子,其中有更好的解释。你有什么不清楚的,尽管问吧。我有个问题。如果这次我想循环通过另一个垂直数组。我怎样才能循环通过一列而不是一行,即对于iCS=1到UBoundCS,2到循环通过一行,但现在我想循环通过一个垂直数组。在“对于iCS=1到UBoundCS,2”,2表示通过列,所以答案是“对于iCS=1到UBoundCS,1”或更短“对于iCS=1到UBoundCS”。我不认为垂直和水平这两个术语在使用,我只是用它们来表示更多行或更多列的位置,以便更好地可视化,即2表示第二个维度或列,1表示可以省略的第一个或多个行。与范围的一个很好的类比是UBoundCS,1=Rows.Count和UBoundCS,2=Columns.Count。也许你可以用“i”替换iFV,用“j”替换iCS,使其更易于阅读。你也可以在“for iFV=1 to UBoundFV”行中看到这一点。更好的做法是总是像本例中的iFV那样声明行,只要我完全忽略了它。通常,当我声明变量时,我喜欢包含它们的类型,因此使用“我的方式”CS将是带有行计数器lngCS和列计数器intCS的VNTC,类似地,FV将是带有lngFV的vntFV,用于行,而intFV用于列,这可能有助于可读性。现在,当您通读代码时,会看到“vnt…”-“aHa,array”,“int…”-“aHa,columns”和“lng…”-“aHa,rows”。你最好还是去划船。。。而且。。。
Option Explicit

Sub XMatch()

  Const FirstMatch As Boolean = True
  Dim FV As Variant     ' Search Array (Vertical)
  Dim CS As Variant     ' Source Array (Horizontal)
  Dim K As Variant      ' Target Array (Vertical)
  Dim iFV As Integer    ' Search Array Rows Counter
  Dim iCS As Integer    ' Source Array Columns Counter

  ' Paste ranges into arrays.
  FV = Sheets("NEW").Range("A28:A34").Value       ' Search Array = Search Range
  CS = Sheets("CS").Range("A1:L2").Value          ' Source Array = Source Range

  ' The Target Array is the same size as the Search Array.
  ReDim K(1 To UBound(FV), 1 To 1)
  ' ReDim K(LBound(FV, 1) To UBound(FV, 1), LBound(FV, 2) To UBound(FV, 2))

  ' Loop through first and only COLUMN of first dimension of Search Array.
  For iFV = 1 To UBound(FV)
  ' For iFV = LBound(FV, 1) To UBound(FV, 1)

    ' Loop through first ROW of second dimension of Source Array.
    For iCS = 1 To UBound(CS, 2)
    ' For iCS = LBound(CS, 2) To UBound(CS, 2)

      If FV(iFV, 1) = CS(1, iCS) Then
        ' Match is found, read from second ROW of the second dimension of Source
        ' Array and write to first and only COLUMN of first dimension of Target
        ' Array.
        K(iFV, 1) = CS(2, iCS)
        ' Check True/False
        If FirstMatch Then
          ' When FirstMatch True, stop searching.
          Exit For
'         Else
          ' When FirstMatch False, try to find another match to use as result.
        End If
'       Else
        ' Match is not found.
      End If

    Next

  Next

  ' Paste Target Array into Target Range, which is two columns to the right of
  ' Search Range.
  Sheets("NEW").Range("A28:A34").Offset(0, 2) = K ' Target Range = Target Array

End Sub