求大神用vba写一段代码
答案:4 悬赏:30 手机版
解决时间 2021-03-24 10:02
- 提问者网友:椧運幽默
- 2021-03-23 19:49
求大神用vba写一段代码
最佳答案
- 五星知识达人网友:街头电车
- 2021-03-23 20:31
工作簿的名字没给不好弄,现就将结果放在C、D列。
Sub demo()
Dim d As Object, i As Long
Set d = CreateObject("scripting.dictionary")
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
If Not d.exists(Cells(i, 1).Value) Then
d(Cells(i, 1).Value) = 1
Else
d(Cells(i, 1).Value) = d(Cells(i, 1).Value) + 1
End If
Next
Range("C2").Resize(d.Count, 1) = Application.Transpose(Filter(d.keys, ""))
Range("D2").Resize(d.Count, 1) = Application.Transpose(Filter(d.items, ""))
End Sub
追问数据A和数据B就是工作蒲名字追答Sub demo()
Dim d As Object, i As Long
Set d = CreateObject("scripting.dictionary")
With Workbooks("数据a.xlsm").Sheets("Sheet1")
For i = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
If Not d.exists(.Cells(i, 1).Value) Then
d(.Cells(i, 1).Value) = 1
Else
d(.Cells(i, 1).Value) = d(.Cells(i, 1).Value) + 1
End If
Next
End With
With Workbooks("数据B.xlsx").Sheets("Sheet1")
.Range("A2").Resize(d.Count, 1) = Application.Transpose(Filter(d.keys, ""))
.Range("B2").Resize(d.Count, 1) = Application.Transpose(Filter(d.items, ""))
End With
End Sub
全部回答
- 1楼网友:动情书生
- 2021-03-23 22:31
Sub 宏1()
Dim arr, o, i, x
Set o = CreateObject("Scripting.Dictionary")
arr = Sheets("Sheet1").UsedRange
For i=1 To UBound(arr)
x = arr(i,1)
o(x) = o(x) + 1
Next i
ReDim arr(i, 2)
i = 1
For Each x In o.Keys
arr(i,1) = x
arr(i,2) = o(x)
i = i + 1
Next x
i = i - 1
Sheets("Sheet2").Range("A1").ReSize(i, 2) = arr
End Sub追问可以加个联系方式么,我还有一个程序需要写
Dim arr, o, i, x
Set o = CreateObject("Scripting.Dictionary")
arr = Sheets("Sheet1").UsedRange
For i=1 To UBound(arr)
x = arr(i,1)
o(x) = o(x) + 1
Next i
ReDim arr(i, 2)
i = 1
For Each x In o.Keys
arr(i,1) = x
arr(i,2) = o(x)
i = i + 1
Next x
i = i - 1
Sheets("Sheet2").Range("A1").ReSize(i, 2) = arr
End Sub追问可以加个联系方式么,我还有一个程序需要写
- 2楼网友:底特律间谍
- 2021-03-23 22:26
你这里数据a,数据b 是两个工作簿文件?追问是的追答Dim MyRows1 As Integer
Dim ThisValue As String
Dim MyDataText() As String
Dim MyDataNumb() As Integer
Dim n As Integer
Dim i As Integer
MyRows1 = 1
n = 0
Do While Workbooks("数据a.xlsx").Sheets("Sheet1").Cells(MyRows1, 1).Value <> ""
ThisValue = Workbooks("数据a.xlsx").Sheets("Sheet1").Cells(MyRows1, 1).Value
For i = 1 To n
If MyDataText(i) = ThisValue Then
MyDataNumb(i) = MyDataNumb(i) + 1
Exit For
End If
Next i
If i > n Then
n = n + 1
ReDim Preserve MyDataText(n) As String
MyDataText(n) = ThisValue
ReDim Preserve MyDataNumb(n) As Integer
MyDataNumb(n) = 1
End If
MyRows1 = MyRows1 + 1
Loop
For i = 1 To n
Workbooks("数据b.xlsx").Sheets("Sheet1").Cells(i, 1) = MyDataText(i)
Workbooks("数据b.xlsx").Sheets("Sheet1").Cells(i, 2) = MyDataNumb(i)
Next i
Dim ThisValue As String
Dim MyDataText() As String
Dim MyDataNumb() As Integer
Dim n As Integer
Dim i As Integer
MyRows1 = 1
n = 0
Do While Workbooks("数据a.xlsx").Sheets("Sheet1").Cells(MyRows1, 1).Value <> ""
ThisValue = Workbooks("数据a.xlsx").Sheets("Sheet1").Cells(MyRows1, 1).Value
For i = 1 To n
If MyDataText(i) = ThisValue Then
MyDataNumb(i) = MyDataNumb(i) + 1
Exit For
End If
Next i
If i > n Then
n = n + 1
ReDim Preserve MyDataText(n) As String
MyDataText(n) = ThisValue
ReDim Preserve MyDataNumb(n) As Integer
MyDataNumb(n) = 1
End If
MyRows1 = MyRows1 + 1
Loop
For i = 1 To n
Workbooks("数据b.xlsx").Sheets("Sheet1").Cells(i, 1) = MyDataText(i)
Workbooks("数据b.xlsx").Sheets("Sheet1").Cells(i, 2) = MyDataNumb(i)
Next i
- 3楼网友:怀裏藏嬌
- 2021-03-23 21:16
可以以人民币结算么追问什么追答快自己好好做作业吧,不明白翻翻书!
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯