【问题标题】:Select cells that fit in the range of the counter and concatenate what is selected from the range选择适合计数器范围的单元格并连接从范围中选择的单元格
【发布时间】:2016-06-16 18:08:39
【问题描述】:

我一直在制作一个需要复制的宏,将通过计数器选择的内容连接起来。例如下面是

excel snapshot example

所以我想要做的是将列 c 中的计数设置为从 1 到“无限”,因为每个工作表会变化到 10 或数百个,并且当计数器再次达到 1 的值以停止连接列 D 什么是在从 1 到“它在再次点击 1 之前停止的最后一个数字”的范围内,并将其粘贴到不同的工作表上。我对 VBA 知之甚少,但我了解复制并粘贴到不同的工作表部分。我只是被困在柜台和连接部分。这是我到目前为止的代码(我将其编辑为类似于示例以便更好地参考)

'select counter/concatenate
Sheets(1).Select
Columns("C").EntireColumn
 Do
 Columns("C").Count
 For i = 1 To 9999
 Loop While (i <= 1)
 If i = 1 Then
 select.columns("D")

计数之后是我卡住的地方。这个计数是我在查看不同的计数器变体时得出的结果。

【问题讨论】:

  • 我只是运行它来检查计数器,但计数器不起作用。说“没有做的循环”,但我确实有一个“做”
  • 让我确保根据您的快照了解您想要做什么。脚本应该从 C1:C5 开始,当它到达新值 1 的 C6 时,连接 D1:D5 并将其放在另一张纸上 (abcde)。
  • 是的。正是这样。

标签: vba concatenation counter


【解决方案1】:

我建议你忘记列,只使用一个单元格以便于理解。单元格是一种引用,允许您使用偏移量来引用工作表上的任何其他单元格。您可以使用两个循环,外部的循环爬取列,内部的循环向下工作,直到找到 1

   Dim i As Long   ' note that in VBA integer Overflows at 65535 rows
   Dim s As String

   Set aCell = Worksheet("Sheet1").Range("D1")
   While aCell.Column < 255
      i = 0
      s = ""
      While Not aCell.Offset(i, 0).Value = 1
         s = s & aCell.Offset(1, 0).Value
      Wend
 ' paste s somewhere by using range.value = s
      Set aCell = aCell.Offset(0, 1)
   Wend

通过在范围之前指定工作簿和工作表,您可以引用正确的单元格而不依赖于活动的工作表或范围。 希望这对你有用。

【讨论】:

    【解决方案2】:

    你可以试试这个(未测试):

       Dim s As String, firstAddr as String
       Dim f as range, iniCell As Range
    
       With Worksheet("MySheet") '<--| change "MySheet" to your actual sheet name
           With .Range("C1", .Cells(.Rows.Count, 3).End(xlUp))
               Set f = .Find(What:=1, LookAt:=xlWhole, LookIn:=xlValues, After:=.Cells(.Rows.Count, 1))
               If Not f Is Nothing Then
                   firstAddr = f.Address
                   Set iniCell = f
                   Set f = FindNext(f)
                   Do While f.Address <> firstAddr
                       s = s & Join(Range(iniCell, f.Offset(-1)).Offset(, 1), "")
    
                       ' here code to paste s somewhere
    
                       Set iniCell = f
                       Set f = FindNext(f)
                   Loop 
               End If
           End With
       End With
    

    【讨论】:

      【解决方案3】:

      这是我实际测试的一个,使用 C 和 D 列中的一些随机数据。

      您必须稍作修改才能准确获取数据的去向,但应该让您朝着正确的方向前进。

      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
          Dim i As Long
          Dim s As String
          Dim lastRow As Long
          Dim c As Integer
          Dim ws1 As Worksheet
          Dim ws2 As Worksheet
          Set ws1 = ThisWorkbook.Sheets("Sheet1")
          Set ws2 = ThisWorkbook.Sheets("Sheet2")
      
          lastRow = ws1.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
          'This will get an accurate last row
      
          c = 1
      
          For i = 1 To lastRow
              s = s & ws1.Cells(i, 4).Value 'Build the string
              If ws1.Cells(i + 1, 3).Value = 1 Or ws1.Cells(i + 1, 3).Value = "" Then
                  ws2.Cells(c, 1).Value = s
                  s = ""
                  c = c + 1
              'If the next cell to check is 1 or blank, then copy the values to the next cell in order on sheet2
              End If
          Next
      End Sub
      

      遍历它,lastRow 是使用工作表中的最后一行设置的,其中包含一个值。然后,c 设置为 1,尽管您可以使用相同的过程将其设置为 ws2 中的最后一个可用行。之后,它只是从1 To LastRow 开始逐步构建字符串,并在即将达到 1 时将值转移到ws2。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-17
        • 2022-11-11
        • 2013-06-26
        • 2019-03-27
        • 1970-01-01
        相关资源
        最近更新 更多