|
本帖最后由 peter199083 于 2023-3-29 16:46 编辑
第一次在论坛上解答问题,希望附件能对楼主有帮助。
- Private Sub Worksheet_Activate()
- Call df
- End Sub
- Private Sub Worksheet_Change(ByVal Target As Range)
- If Target.Address = "$I$1" Then
- Call ff
- End If
- End Sub
- Sub df()
- Dim arr As Variant
- arr = Worksheets("Sheet1").[A1].CurrentRegion
-
- Set d_tle = CreateObject("scripting.dictionary")
- Dim i As Integer
- For i = 1 To UBound(arr, 2)
- d_tle(arr(1, i)) = i
- Next
-
- Set d_code = CreateObject("scripting.dictionary")
- For i = 2 To UBound(arr, 1)
- d_code(arr(i, d_tle("代码"))) = ""
- Next
-
- Dim list_code As String
- For Each Key In d_code.Keys
- If Not list_code = "" Then
- list_code = list_code & "," & Key
- Else
- list_code = Key
- End If
- Next
-
- Worksheets("Sheet2").[I1].Validation.Delete
- Worksheets("Sheet2").[I1].Validation.Add Type:=xlValidateList, Formula1:=list_code
- End Sub
- Sub ff()
- Dim arr As Variant
- arr = Worksheets("Sheet1").[A1].CurrentRegion
-
- Set d_tle = CreateObject("scripting.dictionary")
- Dim i As Integer
- For i = 1 To UBound(arr, 2)
- d_tle(arr(1, i)) = i
- Next
-
- Set d_code = CreateObject("scripting.dictionary")
- For i = 2 To UBound(arr, 1)
- d_code(arr(i, d_tle("代码"))) = ""
- Next
-
- Dim brr As Variant
- Dim row_arr, col_arr As Variant
- Dim r, c As Integer
- r = 0
- ReDim row_arr(1)
- For i = 1 To UBound(arr, 1)
- If arr(i, d_tle("代码")) = Worksheets("Sheet2").[I1].Value Then
- r = r + 1
- ReDim Preserve row_arr(r)
- row_arr(r) = i
- End If
- Next
- c = 0
- ReDim col_arr(1)
- For i = 1 To Worksheets("Sheet2").[A2].CurrentRegion.Columns.Count
- If d_tle.Exists(Worksheets("Sheet2").Cells(2, i).Value) = True Then
- c = c + 1
- ReDim Preserve col_arr(c)
- col_arr(c) = d_tle(Worksheets("Sheet2").Cells(2, i).Value)
- End If
- Next
-
- Dim crr As Variant
- ReDim crr(1 To UBound(row_arr), 1 To UBound(col_arr))
- For i = 1 To UBound(row_arr)
- For j = 1 To UBound(col_arr)
- crr(i, j) = arr(row_arr(i), col_arr(j))
- Next
- Next
-
- Worksheets("Sheet2").[A1].Offset(2).Resize(UBound(crr, 1), UBound(crr, 2)) = crr
- End Sub
复制代码
|
|