求一段excel vba循环代码

作者&投稿:庄方 (若有异议请与网页底部的电邮联系)
Excel的VBA循环代码~

程序开始前,加上一句代码,不让Excel刷新即可
Application.screenupdating=FALSE

17行代码,^_^
Sub THREE_X_PLUS_1()Dim i As Integer, d As Integer, c As Integer, m As IntegerFor i = 1 To 100 d = i m = 0 c = 1 Do d = IIf(d Mod 2 = 0, d / 2, d * 3 + 1) c = c + 1 m = IIf(d > m, d, m) Loop While d 1 Cells(i, 1) = i Cells(i, 2) = c - 1 Cells(i, 3) = mNext iEnd Sub

精简为12行!哈!
Sub THREE_X_PLUS_1()Dim i As Integer, d As IntegerFor i = 1 To 100 d = i Cells(i, 1) = i Do d = IIf(d Mod 2 = 0, d / 2, d * 3 + 1) Cells(i, 2) = Cells(i, 2) + 1 Cells(i, 3) = IIf(d > Cells(i, 3), d, Cells(i, 3)) Loop While d 1Next iEnd Sub

楼主,您看看这段代码, 代码中变化的位置有两个地方
1. Start:=X
2. .Size =X+8

所以先定义一个变量来代替上述X位置, 然后再用一个循环使变量从1变化到8并变成相关设定动作即可.
如下:

Dim i As Integer

For i=8 to 16 then
With ActiveCell.Characters(Start:=i, Length:=1).Font
.Name = "Consolas"
.FontStyle = "常规"
.Size = i + 8
End With
Next i

注: 这个代码之前该还有单元格选择的宏动作, 当然改好后的宏上也要有单元格选择的动作。 因为您问题的只是简化,我的回答也只是对上面重复部分的简化

Sub FontFormat()
Dim rngSelect As Range
Set rngSelect = Worksheets("Sheet1").Range("A1:G10")
For Each Cell In rngSelect
With Cell.Characters(Start:=8, Length:=1).Font
.Name = "宋体"
.FontStyle = "常规"
.Size = 16
End With
Next
End Sub

代码基本上是这样的,可以根据你的需要修改一下。


临沧市13943584186: 求一段excel vba循环代码 -
善倩科尔: Sub FontFormat() Dim rngSelect As Range Set rngSelect = Worksheets("Sheet1").Range("A1:G10")For Each Cell In rngSelectWith Cell.Characters(Start:=8, Length:=1).Font.Name = "宋体".FontStyle = "常规".Size = 16End WithNext End Sub代码基本上是这样的,可以根据你的需要修改一下.

临沧市13943584186: 录制的宏如下.求一个EXCEL中VBA的循环语句. -
善倩科尔: Sub abc() Dim i% For i = 1 To 9999 If --Right(i, 1) = 1 Then Range("d" & i).FormulaR1C1 = "=rc[-3]" Else Range("d" & i).FormulaR1C1 = "=r[-1]c" End If Next End Sub

临沧市13943584186: 求教关于EXCEL宏命令使用VBA实现一个特定条件的循环(详见细节) -
善倩科尔: 加个判断语句不就好了么, n = 1 searchString = Sheet1.Cells(n, 1) a = Mid$(searchString, 1, 10) '这里加个判断 if a=“” then aaa=aaa+1 else aaa=0 i = 1 If a = "Ranking a" Then然后在你的循环条件里加上 aaa=3,我不知道你用的什么循环,如果是DO---LOOP的话很方便,如 DO UNTIL aaa=3 …… LOOP 或者在你循环结束语句的前面加上 IF aaa=3 then exit do 'exit do 是退出循环语句,根据你的循环实际语句修改

临沧市13943584186: 求编写一个在excel上运行的简单的vba程序 -
善倩科尔: sub deleteRow() for i = 200 to 1 step -1 cmpValue = range("A" & i).valueif cmpValue = "27" or cmpValue = "35" or cmpValue = "69" or cmpValue = "77" then row(i).delete end if next end sub

临沧市13943584186: vba excel 循环取得一串单元格内容 -
善倩科尔: Sub kb() Dim i As Integer Dim ss As String For i = 1 To 5 If Range("c" & i) <> "" Then ss = Range("c" & i).Text MsgBox ss End If Next End Sub 编辑的时候有缩进.不懂为嘛打出来没缩进了..

临沧市13943584186: excel VBA 循环语句 -
善倩科尔: 直接用单元格查找方法(Find)比如要找“张三”:Set ra = Cells.Find("张三") If Not ra Is Nothing Then '找到“张三”后要运行的代码段 End If 代码中 ra 就是张三所在单元格,对其它单元格的引用就可用 ra.Offset 进行

临沧市13943584186: excel vba如何顺序循环动作 -
善倩科尔: 提供一个思路给你,代码自己再修改一下.12345678910 Fori = 0 To4 IfCells(1, 2 + i).Value = ""Then Range("A1").Select EndIf Range(Chr(98 + i) & "1").Copy [H65536].End(xlUp).Offset(1, 0).Activate ActiveCell.Offset("0").Resize([B2].Value).Select ActiveSheet.Paste end Next

临沧市13943584186: excel vba 循环取值 -
善倩科尔: excel vba一般有下面几种循环语句: 1、For ... Next for i=1 to 100Cells(i,1)=i Next2、Do Until ... Loop i=1 Do until i=100Cells(i,1)=1i=i+1 Loop3、Do While ... Loop i=1 Do While i<=100Cells(i,1)=1i=i+1 Loop 可以根据情况选择使用

临沧市13943584186: excel vba 时间自增的循环怎么写 -
善倩科尔: 按alt+f11,复制一下代码粘贴,alt+f11,alt+f8,选择宏--时间自增,执行.Sub 时间自增() Dim a, b, c c = 0 For a = 0 To 23 For b = 15 To 45 Step 15 c = c + 1 Cells(1, c) = a & ":" & b Next b If ac = c + 1 Cells(1, c) = a + 1 & ":" & "00" End If Next a End Sub

临沧市13943584186: 如何用excel的VBA进行循环操作,计算一个函数的值,比如y=4x,x<100,怎样使得x每间隔1个单位输出y的值 -
善倩科尔: 在工作表名称上点击鼠标右键 选查看代码,在弹出的VBA编辑窗口粘贴以下代码,关闭VBA编辑窗口,返回工作表,按ALT+F8选中宏 执行 Sub js() Dim arr() x = 0 'x初始值 over = 100 'x终止值 Do n = n + 1 ReDim Preserve arr(1 To 2, 1 To n) arr(1, n) = x arr(2, n) = 4 * x x = x + 1 Loop While x < over Cells(1, 1).Resize(n, 2) = WorksheetFunction.Transpose(arr) End Sub

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网