用其他表格中的数据填充新表格

我有一个主表,有一排房屋地址数据,其中一列是“国家”列。 我在同一份文件中还有另外50张纸,分别对应于美国的50个州。 我想要发生的事情:当我在主表单中input一个地址,并input一个状态(例如“CA”或“California”)时,我希望它自动填充具有该地址的“CA”表单。 谢谢!

这里有几个假设:

1)州字段是最后填写的,所以一旦你进入状态,你可以立即将地址复制到正确的工作表。

2)你用缩写input你的状态,你的工作表的名字和状态缩写完全一致。

3)状态工作表的数据是连续的,从A1开始。

Private Sub Worksheet_Change(ByVal Target As Range) Const lngSTATECOLUMN As Long = 6 Dim wks As Worksheet Dim lngNextAvailableRow As Long ' check that only a single cell is being changed ' If Target.Areas.Count = 1 And Target.Cells.Count = 1 Then ' check that the cell being edited is in the state column ' If Not Intersect(Target, Columns(lngSTATECOLUMN)) Is Nothing Then ' check that a two-character entry has been made in the state column ' If Len(Target.Value) = 2 Then ' turn off error checking in case it cannot find a matching worksheet ' On Error Resume Next Set wks = ThisWorkbook.Worksheets(Target.Value) On Error GoTo 0 ' continue if it found a worksheet with the same name as the state you input ' If Not wks Is Nothing Then lngNextAvailableRow = wks.Range("a1").CurrentRegion.Rows.Count + 1 ActiveSheet.Range(Cells(Target.Row, 1), Cells(Target.Row, 6)).Copy _ wks.Range("A" & lngNextAvailableRow) End If End If End If End If End Sub 

但是,我会问,为什么你需要在50个不同的工作表中复制你的数据? 这似乎非常低效,容易出错。 除非你需要这样做,否则我会强烈build议不要这样做。

如果你需要在某个时候显示或使用单独的状态地址,那么我会看看过滤地址的主要列表。