楼主 罗刚君 |
提示:此题目是“VBA入门免费教学群”(群号:30729794)的课后作业 请未参与听课者绕道,谢谢配合。 _____________________________________________________ 请用For Next和For Each Next两种循环语句套用实用以下功能: 参照动画,罗列Sheet1的每个科目的最后一名的姓名和成绩,保存在Sheet2中 提示:计算一个区域的最小值可用Worksheetfunction.Min函数 22课题目.rar 补充:请尽量不要用数组的知识,还没有上课到那里。_____________________________________________________ 补充:“VBA入门免费教学群”(群号:30729794)每周三上课一次,有兴趣者皆可报名,永远免费。 请提交作业时注明在免费听课群的昵称。三次不交作业者,将踢出群,让出空间让更多的人进来听课,请大家配合。 |
2楼 ynzsvt |
Sub test() Dim Arr, Brr(), i%, j%, Rng As Range Set Rng = Sheets(2).[A1] Arr = Sheets(1).UsedRange ReDim Brr(1 To UBound(Arr, 2), 1 To 3) For j = 2 To UBound(Arr, 2) Brr(j, 3) = 100: Brr(j, 1) = Arr(1, j) For i = 2 To UBound(Arr) If Arr(i, j) = Brr(j, 3) Then Brr(j, 2) = Brr(j, 2) & Chr(10) & Arr(i, 1) If Arr(i, j) < Brr(j, 3) Then Brr(j, 2) = Arr(i, 1): Brr(j, 3) = Arr(i, j) Next i Next j Rng.Resize(UBound(Arr, 2), 3) = Brr Rng.Resize(1, 3) = Array("课目", "姓名", "成绩") End Sub |
3楼 manuel442 |
昵称:智山仁水 Sub 罗列最小值() Dim rng As Range Dim cell As Range Dim cell2 As Range Dim i As Integer Dim mini As Integer Dim j As Integer Set rng = Sheets(1).UsedRange Sheets(2).Cells(1, 1) = "科目" Sheets(2).Cells(1, 2) = "姓名" Sheets(2).Cells(1, 3) = "成绩" j = 1 For Each cell In rng.Offset(, 1).Resize(1, rng.Columns.Count - 1) i = i + 1 mini = WorksheetFunction.min(rng.Offset(, i).Resize(, 1)) For Each cell2 In rng.Offset(, i).Resize(, 1) If cell2 = mini Then Sheets(2).Cells(1, 2).Offset(j) = cell2.Offset(0, -i) Sheets(2).Cells(1, 1).Offset(j) = cell Sheets(2).Cells(1, 3).Offset(j) = mini j = j + 1 End If Next Next End Sub |
4楼 爱情和面包 |
Sub Text() Dim Arr(), i As Byte, x As Byte, y As Byte For y = 2 To Cells(1, Columns.Count).End(xlToLeft).Column Mi = WorksheetFunction.Min(Cells(2, y).Resize(Cells(Rows.Count, 1).End(3).Row - 1)) For x = 2 To Cells(Rows.Count, 1).End(3).Row If Cells(x, y) = Mi Then K = K + 1 ReDim Preserve Arr(1 To 3, 1 To K) Arr(1, K) = Cells(1, y) Arr(2, K) = Cells(x, 1) Arr(3, K) = Cells(x, y) End If Next Next With Sheets(2) .UsedRange.ClearContents .Cells(1, 1) = "科目": .Cells(1, 2) = "姓名": .Cells(1, 3) = "成绩" Sheets(2).Cells(2, 1).Resize(K, 3) = WorksheetFunction.Transpose(Arr) End With End Sub |
5楼 kcxs |
客城小生的作业 以下代码适用于无重复值科目
|
6楼 爱情和面包 |
Sub Text() Dim Arr(), i As Byte, x As Byte, y As Byte Sheets(2).UsedRange.ClearContents Sheets(2).Cells(1, 1) = "科目": Sheets(2).Cells(1, 2) = "姓名": Sheets(2).Cells(1, 3) = "成绩" For y = 2 To Cells(1, Columns.Count).End(xlToLeft).Column Mi = WorksheetFunction.Min(Cells(2, y).Resize(Cells(Rows.Count, 1).End(3).Row - 1)) For x = 2 To Cells(Rows.Count, 1).End(3).Row If Cells(x, y) = Mi Then k = k + 1 Sheets(2).Cells(1, 1).Offset(k) = Cells(1, y) Sheets(2).Cells(1, 2).Offset(k) = Cells(x, 1) Sheets(2).Cells(1, 3).Offset(k) = Cells(x, y) End If Next Next End Sub |
7楼 ynzsvt |
Sub text() Dim j%, Rng1 As Range, RngFirst$, Rngo As Range, MinKe Set Rngo = Sheets(2).[A2] Sheets(1).Select For j = 2 To Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column MinKe = Application.WorksheetFunction.Min(Sheets(1).Columns(j)) Set Rng1 = Columns(j).Find(What:=MinKe) Rngo = Cells(1, j): Rngo.Offset(0, 1) = Rng1.Offset(0, 1 - j): Rngo.Offset(0, 2) = MinKe Set Rngo = Rngo.Offset(1, 0) RngFirst = Rng1.Address Set Rng1 = Columns(j).FindNext(Rng1) While Rng1.Address <> RngFirst Rngo = Cells(1, j): Rngo.Offset(0, 1) = Rng1.Offset(0, 1 - j): Rngo.Offset(0, 2) = MinKe Set Rngo = Rngo.Offset(1, 0) Set Rng1 = Columns(j).FindNext(Rng1) Wend Next j Sheets(2).[A1].Resize(1, 3) = Array("课目", "姓名", "成绩") End Sub |
8楼 LurYangHer |
From VBA入门免费教学群 @BatisHe Sub 作业22() Dim CNum As Integer Dim RNum As Integer Dim MinCJ As Byte Dim i As Integer Dim j As Integer j = 2 CNum = Sheet1.Range("A1").End(xlToRight).Column RNum = Sheet1.Range("A1").End(xlDown).Row Sheet2.Range("A1").Value = "科目" Sheet2.Range("B1").Value = "姓名" Sheet2.Range("C1").Value = "科目最低成绩" For i = 2 To CNum MinCJ = Application.WorksheetFunction.Min(Range(Sheet1.Cells(2, i), Sheet1.Cells(RNum, i))) For Each one In Range(Sheet1.Cells(2, i), Sheet1.Cells(RNum, i)) If one.Value = MinCJ Then Sheet2.Cells(j, 1) = Sheet1.Cells(1, i).Value Sheet2.Cells(j, 2) = Sheet1.Cells(one.Row, 1).Value Sheet2.Cells(j, 3) = Sheet1.Cells(one.Row, i).Value j = j + 1 End If Next one Next i End Sub |
9楼 lfwxszw |
群名片:简单27044519 第一种:FOR NEXXT Sub 最小分数() Dim rng As Range '分数所在单元格 Dim lh As Byte '列号 Dim km As Range ' 科目 Dim xm As Range '姓名 Application.DisplayAlerts = False '查找列数从第2至最后已用列 For lh = 2 To Worksheets("sheet1").UsedRange.End(2).Column '在 列号 列中查找最小值 Set rng = Worksheets("sheet1").Columns(lh).Find(WorksheetFunction.Min(Columns(lh)), , , xlWhole) '科目 值是 最小分数向上查找 行号+1 Set km = rng.Offset(-rng.Row + 1, 0) '姓名 值是 最小分数向左查找 列号+1 Set xm = rng.Offset(0, -rng.Column + 1) '表2中 第 列号 所在行的第1,2,3列值 转换成相应格式 Sheets("sheet2").Cells(1, 1) = "姓名" Sheets("sheet2").Cells(1, 2) = "科目" Sheets("sheet2").Cells(1, 3) = "分数" Sheets("sheet2").Cells(lh, 1) = CStr(xm) Sheets("sheet2").Cells(lh, 2) = CStr(km) Sheets("sheet2").Cells(lh, 3) = CStr(rng) Next lh Application.DisplayAlerts = True End Sub 第二种:不会 |
10楼 wendel |
无心手语
|
11楼 paoge |
骑着钓箱看世界 Sub 罗列各科最后一名() Dim rng As Range, rngclu As Range, i, j, k, rng2 As Range k = 1 Set rng = ActiveSheet.UsedRange Set rngclu = rng.Columns For i = 2 To rngclu.Count sc = WorksheetFunction.Min(rngclu(i)) For Each rng2 In ActiveSheet.UsedRange.Rows If rng.Cells(rng2.Row, i).Value = sc Then k = k + 1 Sheet2.Range("c" & k) = rng.Cells(rng2.Row, i) Sheet2.Range("b" & k) = rng.Cells(rng2.Row, "a") Sheet2.Range("a" & k) = rng.Cells(1, i) End If Next Next Sheet2.Range("a1:c1") = Array("科目", "姓名", "成绩") With Sheet2.UsedRange .Borders.LineStyle = xlContinuous .HorizontalAlignment = xlCenter End With End Sub |
12楼 芐雨 |
|
13楼 天空的雨 |
22课题目-免费听课群-天空答.rar |
14楼 一点点 |
不会
|
15楼 gofux |
gofux报到 22课题目.rar |
16楼 ch_liu2000 |
22试题答案.rar 浪迹天涯作业 |
17楼 冰淇林的冬天 |
冰激凌的冬天 群号:30729794 22课作业.rar |
18楼 gaoshuichang1 |
22课题目.zip 在免费听课群的昵称:顺眼了 |
19楼 lb425319789 |
只用到了FOR循环,For Each Next没有用上,不知该在什么地方用,目前代码测试顺利通过。 Sub 罗列各科目最后一名() Dim rng As Range, firstaddress As String Set rng = Worksheets(1).UsedRange Worksheets(2).Range("a1:c1") = [{"科目","姓名","成绩"}] Worksheets(1).Activate m = 1 '该变量用于定义工作表2的行数 n = 0 '该变量用于工作表1查到每科目最少分单元格时定义对应的姓名单元格 For j = 2 To rng.Columns.Count '循环从表1的第二列到最后一列 n = n + 1 Set mymin = Worksheets(1).Range(Cells(2, j), Cells(rng.Rows.Count, j)) '定义表1的2行,J列到最后一行,J列的区域范围 k = Application.WorksheetFunction.Min(mymin) '该区域数值中查找最小值,赋给K With Range(Cells(2, j), Cells(rng.Rows.Count, j)) '对表1的2行,j列到最后一行,j列范围进行查找 Set Min = .Find(k, LookAt:=xlWhole, LookIn:=xlValues) 'find方法 If Not Min Is Nothing Then '如果找到目标 firstaddress = Min.Address '记录第一个找到的单元格地址 Do '继续查找,直到找到的单元格地址等于刚才记录的单元格地址时停止 m = m + 1 '表2行数加1 Worksheets(2).Cells(m, 3) = Min.Value '将最少分数值放到表2的M行,3列 Worksheets(2).Cells(m, 2) = Min.Offset(0, j - (j + n)) '将最少分数值的单元格,左移到对应的姓名单元格,值放到表2的M行,2列 Worksheets(2).Cells(m, 1) = Cells(1, j) '表1的1行,J列即科目放到表2的M行1列 Set Min = .FindNext(Min) '查找下一个 Loop While Min.Address <> firstaddress End If End With Next Worksheets(2).UsedRange.Borders.LineStyle = xlContinuous '给工作表2使用区域添加边框 End Sub |
20楼 静思雨 |
|
21楼 杭州小菜02 |
Sub aaa() Dim c As Integer, r As Integer Dim col As Integer, rol As Integer Dim minv As Integer, a As Integer Dim rng As Range Application.ScreenUpdating = False With Sheets("sheet2") .UsedRange.Clear .Range("a1:c1") = Array("科目", "姓名", "成绩") a = 2 rol = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row col = Sheet1.Cells(1, Columns.Count).End(xlToLeft).Column For c = 2 To col minv = Sheet1.Cells(2, c).Value For r = 2 To rol If minv > Sheet1.Cells(r, c) Then minv = Sheet1.Cells(r, c).Value End If Next For Each rng In Sheet1.Cells(1, c).Resize(rol, 1) If rng.Value = minv Then .Cells(a, 1) = Sheet1.Cells(1, c) .Cells(a, 2) = Sheet1.Cells(rng.Row, 1).Value .Cells(a, 3) = rng a = a + 1 End If Next Next .Select End With Application.ScreenUpdating = True End Sub |
22楼 qing33670000 |
群名:初学VBA Sub aa() Dim icol%, irow%, c%, d% Sheets(2).Range("a1:c10").Clear Sheets(2).Range("a1:c1") = Array("科目", "姓名", "成绩") icol = Range("aa1").End(xlToLeft).Column irow = Range("a65536").End(xlUp).Row For c = 2 To icol For d = 2 To irow If Cells(d, c) = Application.Min(Range(Cells(2, c), Cells(irow, c))) Then Sheets(2).Range("a65536").End(xlUp).Offset(1, 0) = Cells(1, c) Sheets(2).Range("b65536").End(xlUp).Offset(1, 0) = Cells(d, 1) Sheets(2).Range("c65536").End(xlUp).Offset(1, 0) = Cells(d, c) End If Next Next End Sub Sub ab() Dim icol%, irow%, c%, rg As Range Sheets(2).Range("a1:c10").Clear Sheets(2).Range("a1:c1") = Array("科目", "姓名", "成绩") icol = Range("aa1").End(xlToLeft).Column irow = Range("a65536").End(xlUp).Row For c = 2 To icol For Each rg In Range("a2:a" & irow).Offset(0, c - 1) If rg = Application.Min(Range("a2:a" & irow).Offset(0, c - 1)) Then Sheets(2).Range("a65536").End(xlUp).Offset(1, 0) = Cells(1, rg.Column) Sheets(2).Range("b65536").End(xlUp).Offset(1, 0) = Cells(rg.Row, 1) Sheets(2).Range("c65536").End(xlUp).Offset(1, 0) = Cells(rg.Row, rg.Column) End If Next Next End Sub |
23楼 wangxf9209 |
交作业 22课题目、江苏-雪峰(1036638891).zip |
24楼 hzoptimax |
22课题目-●”莼黑色.rar |
25楼 zql.8008 |
群名along
|
26楼 398829134 |
QQ昵称:Dumbledore
|
27楼 twozisan |
ID==板桥 大刘 Sub 列各科目最后一名01() Dim IROW As Integer '下 Dim ICOL As Integer '右 Dim myRange1 As Range EEE = Range("IV2").End(xlToLeft).Column '右 DDD = Range("b65536").End(xlUp).Row '下 mmm = 1 For ICOL = 2 To EEE M = 100 '數據 n = 0 'irow For IROW = 2 To DDD If Cells(IROW, ICOL).Value < M Then M = Cells(IROW, ICOL).Value n = IROW Cells(IROW, ICOL).Select End If Next IROW mmm = mmm + 1 Worksheets(2).Cells(mmm, 1) = Cells(1, ICOL) Worksheets(2).Cells(mmm, 3) = Cells(n, ICOL) Worksheets(2).Cells(mmm, 2) = Cells(n, 1) Next ICOL End Sub |
28楼 wangxf9209 |
江苏-雪峰 22课第二波作业也在这里交吗? 题目二:
|