楼主 罗刚君 |
提示:此题目是“VBA入门免费教学群”(群号:30729794)的课后作业 请未参与听课者绕道,谢谢配合。 _____________________________________________________ 作业一: 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 作业二: A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和 作业三: 请用代码删除A1:G100区域的奇数行。 _____________________________________________________ 补充:“VBA入门免费教学群”(群号:30729794)每周三上课一次,有兴趣者皆可报名,永远免费。 请提交作业时注明在免费听课群的昵称。三次不交作业者,将踢出群,让出空间让更多的人进来听课,请大家配合。 |
2楼 kcxs |
客城小生的作业 1、
|
3楼 liaozhifa33 |
群昵称:宁静致远 作业一: 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 Sub 隐藏除了汇总表外的其他表() Dim i As Integer For i = 1 To Sheets.Count If Sheets(i).Name <> "汇总" Then Sheets(i).Visible = xlSheetHidden Next End Sub ====================================================== 作业二: A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和 Sub 对A1到A100偶数行大于100求和() Dim i As Byte, sum As Integer For i = 2 To 100 Step 2 If Cells(i, 1) >= 100 Then sum = sum + Cells(i, 1).Value Next MsgBox "A1到A100偶数行大于100合计数为:" & Chr(10) & sum End Sub ====================================================== 作业三: 请用代码删除A1:G100区域的奇数行。 Sub 删除A1到G100区域的奇数行() Application.ScreenUpdating = False Dim i As Byte, j As Integer For i = 1 To 100 Step 2 If i Mod 2 <> 0 Then rowsum = Rows(i).EntireRow.ClearContents Next i For j = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1 If Len(Cells(j, "A")) = 0 Then Rows(j).Delete Next j Application.ScreenUpdating = True End Sub |
4楼 wendel |
|
5楼 静思雨 |
|
6楼 冰淇林的冬天 |
Sub 影藏汇总表以为工作表() Dim i As Integer For i = Sheets.Count To 1 Step -1 If Sheets(i).Name <> "汇总" Then Sheets(i).Visible = False End If Next End Sub Sub 偶数行大于100求和() Dim i As Byte Dim S As Long S = 0 For i = 2 To 100 Step 2 If Cells(i, 1) > 100 Then S = S + Cells(i, 1).Value End If Next MsgBox "S为:" & S, vbOKOnly End Sub Sub 奇数行区域删除() Dim i As Integer For i = 99 To 1 Step -2 Range(Cells(i, 1), Cells(i, 7)).Delete shift:=xlUp Next End Sub 冰激凌的冬天 |
7楼 398829134 |
QQ昵称:Dumbledore
|
8楼 paoge |
骑着钓箱看世界 1. (1)Sub 隐藏指定工作表1() For i = 1 To Worksheets.Count Step 1 With Worksheets(i) If .Name = "汇总表" Then .Visible = True Else .Visible = False End If End With Next i End Sub (2)Sub 隐藏汇总表以外的所有表2() Dim sht As Worksheet For Each sht In Worksheets If sht.Name = "汇总表" Then sht.Visible = xlSheetVisible Else sht.Visible = xlSheetHidden End If Next sht End Sub 2、 Sub 偶数行求和() Dim i As Integer Dim sumrng As Long For i = 100 To 1 Step -2 If Range("a" & i).Value > 100 Then sumrng = sumrng + Range("a" & i).Value Next i MsgBox sumrng End Sub 3、 Sub 删除选定区域的奇数行() Dim i As Integer, rowname As String Range("a1:g100").Select For i = 1 To Selection.Rows.Count Step 2 Rows(i).Delete shift:=xlUp rowsname = rowsname & Chr(13) & i Next i MsgBox rowsname End Sub |
9楼 ★白and黑☆ |
第二题 《白and 黑》 Sub 求和偶数行的() Dim i As Byte, s As Byte For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row Step 2 If i > 100 Then s = s + i End If Next MsgBox s End Sub |
10楼 凄美の残缺 |
Sub 隐藏() Dim i As Integer For i = 1 To Sheets.Count If Sheets(i).Name <> "汇总" Then Sheets(i).Visible = False End If Next End Sub Sub 求和() Dim i As Integer Dim x As Integer For i = 1 To 100 If Cells(i, 1).Value > 100 Then If x = 0 Then x = Cells(i, 1).Value Else x = x + Cells(i, 1).Value End If End If Next End Sub Sub 删除() Dim i As Integer For i = 100 To 1 Step -1 If Cells(i, 1).Row Mod 2 = 1 Then Range("a" & i & ":g" & i).Delete Shift:=xlUp End If Next End Sub |
11楼 lb425319789 |
作业一: 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 Sub 隐藏工作表() Dim i As Integer For i = 1 To Worksheets.Count If Worksheets(i).Name <> "汇总" Then Worksheets(i).Visible = xlSheetVeryHidden End If Next i End Sub 作业二: A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和 Sub 偶数求和() Dim i As Integer Dim d As Integer Dim k As Integer k = 0 For i = 1 To 100 If i Mod 2 = 0 Then d = Range("A" & i).Value If Range("A" & i).Value > 100 Then k = k + d Else: i = i + 1 End If End If Next i MsgBox "A1:A100的偶数行大于100的数值之和为" & k End Sub 作业三: 请用代码删除A1:G100区域的奇数行。 Sub 删除行() Dim i As Integer For i = 1 To 100 If i Mod 2 = 1 Then Range("a1:g100").Rows(i).Select Selection.Delete Shift:=xlToLeft End If Next i End sub |
12楼 一点点 |
糊啦啦 作业一: 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 Sub yinc() Dim isht As Byte For isht = 1 To Sheets.Count If Sheets(isht).Name <> "汇总" Then Sheets(isht).Visible = xlSheetHidden End If Nexit isht End Sub 作业二: A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和 Sub oushu() Dim irow As Byte For irow = 1 To 100 If irow Mod 2 = 0 Then If Range("a" & irow).Value > 100 Then 求和 = 求和 + Range("a" & irow) End If End If Next irow Range("B1") = 求和 End Sub 作业三: 请用代码删除A1:G100区域的奇数行。 Sub jishu() Dim irow For irow = 100 To 1 Step -1 If irow Mod 2 = 1 Then Rows(irow).Delete End If Next irow End Sub |
13楼 ynzsvt |
'作业一: ' 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 Sub YingChangBiao() Dim i% For i = 1 To Sheets.Count If Sheets(i).Name = "汇总" Then Sheets(i).Visible = True Else Sheets(i).Visible = False Next i End Sub '作业二: ' A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和 Sub qiuhe() Dim i%, s# s = 0 For i = 2 To 100 Step 2 If Cells(i, 1) > 100 Then s = s + Cells(i, 1) Next i MsgBox s, , "偶数行大于100的数值和" End Sub '作业三: '请用代码删除A1: G100区域的奇数行? Sub shanchuHang() Dim i% Application.ScreenUpdating = False For i = 99 To 1 Step -2 Range(Cells(i, "A"), Cells(i, "G")).Delete shift:=xlUp Next i Application.ScreenUpdating = True End Sub |
14楼 芐雨 |
1.
|
15楼 一片叶子 |
@小鱼
|
16楼 gaoshuichang1 |
作业一: 请用循环语句加条件语句隐藏“汇总”表以外的所有工作表
A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和
请用代码删除A1:G100区域的奇数行。
|
17楼 mmice |
在此先谢谢罗老师 Sub zy1() ''作业一:请用循环语句加条件语句隐藏“汇总”表以外的所有工作表 Dim sht As Worksheet Application.DisplayAlerts = False For Each sht In Worksheets If sht.Name <> "汇总" Then sht.Delete End If Next Application.DisplayAlerts = True End Sub zy2() ' 作业二:A1:A100存放了若干数据,请用VBA的循环语句对A1:A100的偶数行大于100的数值求和宏1 宏 Dim xrow As Integer, sum As Integer, Cell As Range Application.DisplayAlerts = False sum = 0 For xrow = 2 To 100 Step 2 If Cells(xrow, "A").Value > 100 Then sum = sum + Cells(xrow, "A").Value End If Next MsgBox "总和" & sum Application.DisplayAlerts = True End Sub zy3() '请用代码删除A1: G100区域的奇数行? Dim i As Integer, n As Range, myrange As Range Application.DisplayAlerts = False Set myrange = Range("A1") For Each n In Range("A1:G100") If n.Row Mod 2 <> 0 Then Set myrange = Union(myrange, n) End If Next myrange.Delete Application.DisplayAlerts = True End Sub 群名片:小白 |
18楼 manuel442 |
QQ昵称:智山仁水 1、sub 隐藏() dim i for i=1 to sheets.count if sheets(i).name<>"汇总" then sheets(i).visible=false endif end sub 2、 sub 求和() dmi i,sum for i=2 to 100 step 2 if cells(i,1)>100 then sum=sum+cells(i,1) endif end sub 3、 sub 删除() Dim i As Integer, MaxRow As Integer '声明变量 MaxRow = Cells(Rows.Count, 1).End(xlUp).Row '记录A列最后一个非空单元格的行号 For i = MaxRow-1 To 1 Step -2 '从最后一个非空单元格所在行到第一行 Rows(i).Delete '如果A列第i个单元格是空白的,那么整行删除 Next i End Sub |
19楼 lfwxszw |
群里我叫*简单*27044519,上次交的作业都忘了写名字了 1、Sub 隐藏汇总表() Dim i As Byte '定义变量 For i = 1 To Sheets.Count '变量i值为1到总数 If Sheets(i).Name <> "汇总" Then '如果表名不是汇总 Sheets(i).Visible = False '则隐藏表 End If Next End Sub 2、Sub 偶数行大于100求和() Dim c As Integer '定义变量c Dim b As Byte '定义变量b For b = 2 To Range("a1:a100").Rows.Count Step 2 '变量b值为2到使用总行数,步长为2 If Cells(b, 1).Value > 100 Then '如果 cells(b,1)值大于100 则 c = c + Cells(b, 1).Value '变量c=c+cells(b,1)的值 End If Next MsgBox c End Sub 3、Sub 删除奇数行() Dim a As Byte For a = 1 To Range("A1:G100").Rows.Count Step 1 '变量a值为1到100,步长为1 Cells(a, 1).EntireRow.Delete '单元格(a,1),整行删除 Next End Sub |
20楼 健康快乐123 |
3D:水上漂123 第一题:
|
21楼 LurYangHer |
@VBA入门免费教学群 BatisHe 经过昨晚的作业评讲,觉得第三题可以更高效一点,就是不必要执行太多次的删除操作 Sub 作业三() Application.ScreenUpdating = False Dim str$ str = "A1" For i = 3 To 100 Step 2 str = str & ",A" & i Next Sheet1.Range(str).EntireRow.Delete Application.ScreenUpdating = False End Sub |
22楼 罗刚君 |
这种方式通用性不太好,因为Range的参数长度有限制 如果待删除的单元格有几百个,代码就行不通了 |
23楼 LurYangHer |
这样子啊?谢谢罗老师指点。 |