當前位置:成語大全網 - 新華字典 - VBA,怎麽加和

VBA,怎麽加和

妳這個是非常實際的問題,所以就直接給妳解決好了

Option Explicit

Sub damnvba()

Dim sh As Worksheet

Set sh = ThisWorkbook.Worksheets("sheet1") '這裏放妳放數據的sheet的名稱

Dim i As Integer

Dim j As Integer

sh.Columns("A:B").Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("B1") _

, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _

False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _

xlSortNormal, DataOption2:=xlSortNormal

'sh.Range("D1").FormulaR1C1 = "=sheet1!A1 & sheet1!B1"

sh.Range("D1").FormulaR1C1 = "=RC[-3] & RC[-2]"

'根據數據多少自己改D30000

sh.Range("D1").AutoFill Destination:=Range("D1:D30000"), Type:=xlFillDefault

Dim strLast As String

i = 1

Dim iStart As Integer

Dim iEnd As Integer

Dim iCount As Integer

iCount = 1

iStart = 1

iEnd = 1

Dim iTRow As Integer

Dim iTCol As Integer

strLast = sh.Range("D1").Text

Do While sh.Range("A" & i).FormulaR1C1 <> ""

DoEvents

If sh.Range("D" & i).Text <> strLast Then

iEnd = i - 1

sh.Range("E" & iCount).FormulaR1C1 = "=sum(R[" & iStart - iCount & "]C[-2] : R[" & iEnd - iCount & "]C[-2])"

sh.Range("F" & iCount).FormulaR1C1 = strLast

iCount = iCount + 1

iStart = i

strLast = sh.Range("D" & i).Text

End If

i = i + 1

Loop

'還有最後壹個需要判斷

iEnd = i - 1

sh.Range("E" & iCount).FormulaR1C1 = "=sum(R[" & iStart - iCount & "]C[-2] : R[" & iEnd - iCount & "]C[-2])"

sh.Range("F" & iCount).FormulaR1C1 = strLast

iCount = iCount + 1

iStart = i

strLast = sh.Range("D" & i).Text

End Sub