原諒我,因爲這可能非常簡單。我正在嘗試創建一個VBA宏,它可以快速從原始數據獲取統計信息並將它們放入表中。原始數據來自格式爲:Excel VBA邏輯:使用循環獲取兩個單元格之間的範圍
(他們不會永遠是三個一組)
如何我會得到的範圍內對所有的類別,然後使用相同的範圍爲列B和C獲取我需要的統計數據?
原諒我,因爲這可能非常簡單。我正在嘗試創建一個VBA宏,它可以快速從原始數據獲取統計信息並將它們放入表中。原始數據來自格式爲:Excel VBA邏輯:使用循環獲取兩個單元格之間的範圍
(他們不會永遠是三個一組)
如何我會得到的範圍內對所有的類別,然後使用相同的範圍爲列B和C獲取我需要的統計數據?
下面的代碼爲您提供了每個類別的行號,並假設列B上的內容沒有中斷,您的問題是按類別獲取列C:D的內容,讓這些行值使您代碼來獲取C:D的內容。
Public Sub Sample()
Dim WkSht As Worksheet
Dim StrCategory As String
Dim LngRow As Long
Dim LngRowStart As Long
Set WkSht = ThisWorkbook.Worksheets("RawData")
'Take note of the category we are one
StrCategory = WkSht.Range("A" & 2).Value
'Take not of the row the category started on
LngRowStart = 2
'Look to the next row
LngRow = 3
'Loop through the data until column B has no value, signifying the end of the dataset
Do Until WkSht.Range("B" & LngRow) = ""
'Go to the next row until we are given a new category or make it to the end of the dataset
Do Until (WkSht.Range("A" & LngRow) <> "") Or (WkSht.Range("B" & LngRow) = "")
LngRow = LngRow + 1
Loop
'Talk in the immediate pane
Debug.Print StrCategory & " is on rows " & LngRowStart & " to " & LngRow - 1
'Get the next values
StrCategory = WkSht.Range("A" & LngRow)
LngRowStart = LngRow
'Move on
LngRow = LngRow + 1
Loop
Set WkSht = Nothing
End Sub
下面是我給它的輸入數據: -
下面是從代碼的輸出: -
這個工程,但它沒有更新類別名稱,所以它打印出來:A從2到4 ..... A從5到7 ... A從8到10等 –
嗯,並在你正在使用的數據不會出現'A','[空白]','[空白]','A'嗎? –
不,數據將始終有一個值 –
你可以使用一些If
語句並將其全部拉入數組中,但似乎更直接地填入空白
Sub FillColA()
Dim LastRow As Long
LastRow = Application.WorksheetFunction.CountA(Range("B:B"))
Range("A2:A" & LastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
End Sub
爲什麼你不能將類別複製到所有行? – aFast