如何将多个动态列合并到新工作表中的单个列中

时间:2019-04-24 22:55:42

标签: excel vba

我正在尝试将多个动态列合并到新表的单个列中。我编写的代码可以转移从多个下拉菜单中做出的选择,用户可以在其中选择多个选项进入自己的单元格。我正在尝试创建一个函数,将这些选择转移到新工作表的单个列中。如果有人能告诉我如何将这种变化的行范围(约8列)转移到一个新的工作表上,那将是不错的选择,或者将选择内容直接放置在同样有效的新工作表上是否更容易。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Row = 11 Then
  Dim rng As Range, cell As Range
  Set rng = Range("A11:H11")
  For Each cell In rng
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
        GoTo Exitsub
    Else: If Target.Value = "" Then GoTo Exitsub Else
     Application.EnableEvents = False
     Newvalue = Target.Value
     Application.Undo
     Oldvalue = Target.Value
       If Oldvalue = "" Then
        Target.Value = Newvalue
        Dim val As String
        val = ActiveCell.Value
        ActiveCell.Offset(0, 9).Value = val
       Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & "; " & Newvalue
            'below begins addition from Shanell
            Dim txt As String
            Dim i As Integer
            Dim FullName As Variant

            txt = ActiveCell.Value

            FullName = Split(txt, ";")

            For i = 0 To UBound(FullName)
                ActiveCell.Offset(i, 9).Value = FullName(i)
            Next i
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
  Next cell
End If
Set rng = ActiveCell.CurrentRegion
lastCell = rng.Columns(1).Rows.Count + 1
'Attempt to copy to new sheet'
For iCol = 11 To rng.Columns.Count
    Range(Cells(11, iCol), Cells(rng.Columns(iCol).Rows.Count, iCol)).Cut
    ActiveSheet.Paste Destination:=Worksheets("Links").Range("lastCell,A3")
    lastCell = lastCell + rng.Columns(iCol).Rows.Count

Next iCol

我已经尝试过此代码,并且没有错误,但是值未显示在新表上。我在试图将值复制到新表的地方添加了注释。

我还尝试了如下代码:

Range("J11", Range("Q11").End(xlDown)).Copy Worksheets("Links").Range("A3:A")

但是,这也不起作用。

0 个答案:

没有答案