當前位置:編程學習大全網 - 編程軟體 - excel裏VBA編寫代碼,急急急!

excel裏VBA編寫代碼,急急急!

Private?Sub?CommandButton1_Click()

Dim?sh?As?Worksheet,?rg?As?Range,?rg0?As?Range,?rg1?As?Range

Dim?a,?i,?d,?dC,?dD,?tmp

Set?d?=?CreateObject("Scripting.Dictionary")

Set?dC?=?CreateObject("Scripting.Dictionary")

Set?dD?=?CreateObject("Scripting.Dictionary")

Set?sh?=?ActiveSheet

With?sh

Set?rg0?=?.Range("A:D")?'原數據列位置

Set?rg1?=?.Range("E1")'結果數據,第壹個單元格位置

Set?rg?=?Application.Intersect(rg0,?.UsedRange)

If?rg?Is?Nothing?Then?MsgBox?"無數據!",?vbCritical:?Exit?Sub

If?rg.Rows.Count?<?2?Or?rg.Columns.Count?<>?4?Then

MsgBox?"數據設置錯誤!",?vbCritical:?Exit?Sub

End?If

End?With

a?=?rg

For?i?=?1?To?UBound(a)

tmp?=?Trim(a(i,?2))

If?tmp?<>?""?Then

If?d.Exists(tmp)?Then

d(tmp)?=?d(tmp)?&?"?"?&?a(i,?1)

Else

d(tmp)?=?a(i,?1)

dC(tmp)?=?a(i,?3)

dD(tmp)?=?a(i,?4)

End?If

End?If

Next

If?d.Count?<?2?Then?MsgBox?"無數據!",?vbCritical:?Exit?Sub

With?rg1.Resize(d.Count,?4)

.EntireColumn.ClearContents

.Columns(1)?=?Application.Transpose(d.items)

.Columns(2)?=?Application.Transpose(d.keys)

.Columns(3)?=?Application.Transpose(dC.items)

.Columns(4)?=?Application.Transpose(dD.items)

End?With

End?Sub

  • 上一篇:在VC++6.0上使用c語言編程,單精度浮點型和雙精度浮點型為何都只能精確到小數點後六位?
  • 下一篇:新疆天山職業技術大學有哪些專業
  • copyright 2024編程學習大全網