Excel p> 代码 Sub testWhat() Dim dTime As Double dTime = Timer Const sName As String = "Sheet1" Const sFirst As String
p> 代码Excel p> 代码 Sub testWhat() Dim dTime As Double dTime = Timer Const sName As String = "Sheet1" Const sFirst As String,excel,vba,Excel,Vba,p> 代码 Sub testWhat() Dim dTime As Double dTime = Timer Const sName As String = "Sheet1" Const sFirst As String = "A1" Const dName As String = "Sheet1" Const dFirst As String = "C1" Dim srg A
Sub testWhat()
Dim dTime As Double
dTime = Timer
Const sName As String = "Sheet1"
Const sFirst As String = "A1"
Const dName As String = "Sheet1"
Const dFirst As String = "C1"
Dim srg As Range: Set srg = ThisWorkbook.Worksheets(sName).Range(sFirst)
Dim drg As Range: Set drg = ThisWorkbook.Worksheets(dName).Range(dFirst)
sadTest srg, drg
Debug.Print Timer - dTime
End Sub
Sub sadTest(sCell As Range, dcell As Range)
Dim rg As Range
With sCell.CurrentRegion
Set rg = .Columns(1).Resize(.Rows.Count - 1).Offset(1)
End With
Dim kData As Variant: kData = rg.Value
Dim vData As Variant: vData = rg.Offset(, 1).Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim arl As Object: Set arl = CreateObject("System.Collections.ArrayList")
Dim srCount As Long: srCount = UBound(kData, 1)
Dim drCount As Long: drCount = 1
Dim rData As Variant: ReDim rData(1 To srCount)
Dim Key As Variant
Dim dCount As Long
Dim i As Long
For i = 1 To srCount
Key = kData(i, 1)
If dict.Exists(Key) Then
dCount = dict(Key) + 1
rData(i) = dCount
dict(Key) = dCount
If dCount > drCount Then
drCount = drCount + 1
End If
Else
arl.Add Key
dict(Key) = 1
rData(i) = 1
End If
Next i
Dim dcCount As Long: dcCount = dict.Count
' arl.IndexOf(kData(i, 1), 0) + 1
' Application.Match(kData(i, 1), dict.keys, 0) ' to slow
Dim Result As Variant: ReDim Result(1 To drCount, 1 To dcCount)
For i = 1 To srCount
Result(rData(i), arl.IndexOf(kData(i, 1), 0) + 1) = vData(i, 1)
Next i
dcell.Resize(drCount, dcCount).Value = Result
End Sub
将namedworkbook | thisworkbook.namedsheet.Range(“A2:B”&m)读入数组。Redim
a
(1到1,1到iif(m=2,2,m-1))外部环路。通过循环数组而不是表填充。在工作表引用中要明确。将v
as()声明为string当ubound v=-1
为no时会发生什么”,“在a(1,c)
中?事实上,代码是用来演示解决方案的(但我认为这不是最好的方法)。有更好的解决办法吗?@TimWilliams是的。我说错了。我的错误。将namedworkbook | thisworkbook.namedsheet.Range(“A2:B”&m)读入数组。Redima
(1到1,1到iif(m=2,2,m-1))外部环路。通过循环数组而不是表填充。在工作表引用中要明确。将v
as()声明为string当ubound v=-1
为no时会发生什么”,“在a(1,c)
中?事实上,代码是用来演示解决方案的(但我认为这不是最好的方法)。有更好的解决办法吗?@TimWilliams是的。我说错了。我的错,非常感谢。那也太棒了。非常感谢。那也太棒了。非常感谢。太棒了,太谢谢你了。太棒了。
Option Explicit
Public Sub WriteOutValuesByKeyColumn()
Dim columnMapping As Scripting.dictionary, targetSheet As Worksheet
Set targetSheet = ThisWorkbook.Worksheets("Sheet1")
Set columnMapping = New Scripting.dictionary 'qualify with class
With targetSheet
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Dim inputArray() As Variant, tempArray() As Variant
inputArray = .Range("A2:B" & lastRow).Value
ReDim tempArray(1 To 1, 1 To IIf(lastRow = 2, 2, lastRow - 1)) 'max size array at start
Dim i As Long, counter As Long
For i = LBound(inputArray, 1) To UBound(inputArray, 1)
If Not columnMapping.Exists(inputArray(i, 1)) Then
counter = counter + 1
tempArray(1, counter) = inputArray(i, 2)
columnMapping.Add inputArray(i, 1), counter
Else
tempArray(1, columnMapping.Item(inputArray(i, 1))) = tempArray(1, columnMapping.Item(inputArray(i, 1))) & ", " & inputArray(i, 2)
End If
Next
Dim columnValues() As String
For i = 1 To UBound(tempArray, 2)
If tempArray(1, i) <> vbNullString Then
columnValues = Split(tempArray(1, i), ", ")
.Cells(1, i + 2).Resize(UBound(columnValues, 1) + 1).Value = WorksheetFunction.Transpose(columnValues)
Else
.Cells(1, i + 2) = tempArray(1, i)
End If
Next
End With
End Sub
Sub testWhat()
Dim dTime As Double
dTime = Timer
Const sName As String = "Sheet1"
Const sFirst As String = "A1"
Const dName As String = "Sheet1"
Const dFirst As String = "C1"
Dim srg As Range: Set srg = ThisWorkbook.Worksheets(sName).Range(sFirst)
Dim drg As Range: Set drg = ThisWorkbook.Worksheets(dName).Range(dFirst)
sadTest srg, drg
Debug.Print Timer - dTime
End Sub
Sub sadTest(sCell As Range, dcell As Range)
Dim rg As Range
With sCell.CurrentRegion
Set rg = .Columns(1).Resize(.Rows.Count - 1).Offset(1)
End With
Dim kData As Variant: kData = rg.Value
Dim vData As Variant: vData = rg.Offset(, 1).Value
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim arl As Object: Set arl = CreateObject("System.Collections.ArrayList")
Dim srCount As Long: srCount = UBound(kData, 1)
Dim drCount As Long: drCount = 1
Dim rData As Variant: ReDim rData(1 To srCount)
Dim Key As Variant
Dim dCount As Long
Dim i As Long
For i = 1 To srCount
Key = kData(i, 1)
If dict.Exists(Key) Then
dCount = dict(Key) + 1
rData(i) = dCount
dict(Key) = dCount
If dCount > drCount Then
drCount = drCount + 1
End If
Else
arl.Add Key
dict(Key) = 1
rData(i) = 1
End If
Next i
Dim dcCount As Long: dcCount = dict.Count
' arl.IndexOf(kData(i, 1), 0) + 1
' Application.Match(kData(i, 1), dict.keys, 0) ' to slow
Dim Result As Variant: ReDim Result(1 To drCount, 1 To dcCount)
For i = 1 To srCount
Result(rData(i), arl.IndexOf(kData(i, 1), 0) + 1) = vData(i, 1)
Next i
dcell.Resize(drCount, dcCount).Value = Result
End Sub