这是我的方案 - 我在一张纸上的COLUMN A中有一个客户列表 - 以及相关数据......我有另一张表,我想要创建一个矩阵......但是我们要沿着ROW 1列出客户列表。 / p>
我希望将第1页上添加到COLUMN的新客户添加到表2中的ROW
这是怎么做到的?
答案 0 :(得分:0)
我不确定您的VBA级别,但在第一张工作表的代码隐藏中,您可以捕获Workhseet_Change事件。
您可以检查更新的单元格是否在A列中。如果是,您可以将该值添加到第一行的第2页。
以下是一些帮助您入门的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
'check to make sure the updated cell (Target) is in column 1 (column A)
If Target.Column = 1 Then addToSheet2 (Target.Value2)
End Sub
Private Sub addToSheet2(ByVal newValue As Variant)
Dim ws As Worksheet
Dim columnCount As Integer
Dim nextColumn As Integer
On Error GoTo errTrap
Set ws = ThisWorkbook.Sheets(2) 'probably want to use the sheet name, instead of the index
'probably a good idea to check if it already exists in row 1 of sheet 2
'get the number of columns used in sheet 2
columnCount = ws.UsedRange.Columns.Count
'this may be overkill, but if you are starting from scratch, columnCount will be 1 even if
'there is no data in sheet 2 row 1
If columnCount = 1 And ws.Range("A1").Value2 = vbNullString Then
nextColumn = columnCount
Else
nextColumn = columnCount + 1
End If
ws.Cells(1, nextColumn).Value2 = newValue
errTrap:
Set ws = Nothing
End Sub