Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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/14.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 根据if条件从每列复制数据_Excel_Vba - Fatal编程技术网

Excel 根据if条件从每列复制数据

Excel 根据if条件从每列复制数据,excel,vba,Excel,Vba,我在一张表中有数据,看起来像 A B C D 1001 1002 1003 Phone 1 1 1 TV 1 1 Remote 1 AC 1 1 1 我想要一个宏,它在另一张表中给出数据,比如 Phone 1001;1002;1003 TV 1001;1003 Remote 1003 AC 1001;10

我在一张表中有数据,看起来像

A      B      C       D
       1001   1002    1003
Phone  1      1       1
TV     1              1
Remote                1
AC     1      1       1
我想要一个宏,它在另一张表中给出数据,比如

Phone  1001;1002;1003
TV     1001;1003
Remote 1003
AC     1001;1002;1003
在两列中

这是一个示例数据,列和行每一次都会有很大的变化,比如说多达1000个。 所以我需要一个宏来从第一行获取数据,前提是对应的单元格中有“1”。

这可能会有所帮助

Sub Init()
Dim x, y As Integer
Dim s As String
Dim xt As Worksheet

Set xt = Sheets("Sheet2")'----------> you may change this

For y = 2 To 6 '--------------------tv,remote,etc
  s = ""
  For x = 2 To 4 '------------------1001,1002,1003
    If Cells(y, x) = 1 Then
      If Len(s) > 0 Then s = s & "; "
      s = s & Cells(1, x)
    End If
  Next
  xt.Cells(y, 1) = Cells(y, 1)
  xt.Cells(y, 2) = s
Next
End Sub
在你的问题中有“我想要一个…”是一个相当好的指标,它可能会。