我正在开发一个分析数据的程序。我是Visual Basics和Macros的新手。我有2列A和B,其值介于.001和1.我需要帮助创建箱子来存储这些数字。我已经完成了研究并查看了很多示例程序,没有任何东西能够满足我的需求。 这里有一些重要的细节。 A列需要从A1到A2625的数据,B列需要从B1到B2700的数据。我需要将这些数据放入10个箱子中。例如:Bin 1需要保持值.001到.11,Bin 2-12-.20 .....直到bin 10.有什么建议吗?谢谢!
答案 0 :(得分:0)
在VBA中,您将创建一个类似于
的宏Option Explicit
Sub myMacro()
Dim nRow As Integer
Dim wSht As Worksheet
Dim endRowColumn1 As Integer
Dim endRowColumn2 As Integer
Set wSht = ActiveWorkbook.Sheets(1)
endRowColumn1 = wSht.Cells(Rows.Count, 1).End(xlUp).Row
endRowColumn2 = wSht.Cells(Rows.Count, 2).End(xlUp).Row
' Clear results from last run
wSht.Range("C:E").Clear ' Clear rows 3,4,5
wSht.Cells(1, 3) = "Bin1"
wSht.Cells(1, 4) = "Bin2"
wSht.Cells(1, 5) = "No Criteria Met"
' STARTING WITH SECOND ROW
For nRow = 2 To endRowColumn1 ' For each row... to the end of your rows..
' If the first bin criteria is met...
If (wSht.Cells(nRow, 1) >= 0.001) And _
(wSht.Cells(nRow, 1) < 0.12) Then
' Then put the value into the first bin. (column C)
wSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 1)
ElseIf (wSht.Cells(nRow, 1) >= 0.12) And _
(wSht.Cells(nRow, 1) < 0.2) Then
' Then put the value into the second bin. (column D)
wSht.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 1)
Else ' No Criteria Met
wSht.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 1)
End If
Next nRow
For nRow = 2 To endRowColumn2 ' For each row... to the end of your rows..
' If the first bin criteria is met...
If (wSht.Cells(nRow, 2) >= 0.001) And _
(wSht.Cells(nRow, 2) < 0.12) Then
' Then put the value into the bin. (column C)
wSht.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 2)
ElseIf (wSht.Cells(nRow, 2) >= 0.12) And _
(wSht.Cells(nRow, 2) < 0.2) Then
' Then put the value into the second bin. (column D)
wSht.Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 2)
Else ' No Criteria Met
wSht.Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = wSht.Cells(nRow, 2)
End If
Next nRow
End Sub
如果您对代码有疑问,请与我们联系,我们可以进行讨论。
答案 1 :(得分:0)
您不需要VBA来执行此操作。 Excel中有一个名为Frequency的函数。如果您在一列中有数字,则可以在另一列中创建这样的分类
+--------+------+
| values | bins |
+--------+------+
| 59 | 0 |
| 16 | 10 |
| 12 | 20 |
| 5 | 30 |
| 33 | 40 |
| 90 | 50 |
| 92 | 60 |
| 74 | 70 |
| 26 | 80 |
| 63 | 90 |
| 40 | 100 |
| etc| |
+--------+------+
然后你可以使用
FREQUENCY(values,bins)
我需要像这样输入,因为它是一个数组公式:
注意我选中的所有单元格都与选中的单元格相邻,我在顶部单元格中处于编辑模式。然后,一旦编写了公式,您点击ctrl + alt + enter,您选择的所有单元格将填入该区域的出现次数。
这适用于多列。
制作直方图图表b频率。
这不会为你创造垃圾箱。