excel宏循环执行变量不变的问题
主要问题是,选择当前行后自动生成一个表格,然后自动选择下一行,循环执行N次,但是每次都是选中行生成才是我想要的,自己点击宏一次一次生成,没问题,但是用循环执行最后生成的表...
主要问题是,选择当前行后自动生成一个表格,然后自动选择下一行,循环执行N次,但是每次都是选中行生成才是我想要的,自己点击宏一次一次生成,没问题,但是用循环执行最后生成的表格都是一样的,也就是x变量数不变,如何才能x每次重新定义
Sub 自动任务()
Dim a As Single
Dim b As Single
Dim c As Single
Dim d As Single
Dim e
Dim f As Integer
Dim i As Integer
Dim x%
f = Range("i2")
For i = 1 To f
ActiveCell.Select '选择生成行
x = ActiveCell.Row
a = Range("h" & x)
Range("单!e5") = Range("a" & x) '将a1复制到任务单!e5
Range("单!m5") = Range("b" & x)
Range("单!u5") = Range("c" & x)
Range("单!ac5") = Range("d" & x)
Range("单!e6") = Range("e" & x)
Range("单!e7") = Range("g" & x)
Range("单!e10") = Range("i" & x)
Range("单!x7") = Now() '输入日期
If a > 0.9 Then b = 1 Else: b = 0
If a > 0.9 Then c = 0 Else: c = 1
If a > 0.9 Then d = a Else: d = 4 * a
If a > 0.9 Then e = "个月" Else: e = "个周"
Range("单!h25") = b
Range("单!i25") = c
Range("单!g27") = d
Range("单!h27") = e
If Range("d" & x) = "A" Then Range("单!b11") = 1 Else Range("单!b11") = 0 '自动选择维护等级
If Range("d" & x) = "B" Then Range("单!c11") = 1 Else Range("单!c11") = 0
If Range("d" & x) = "C" Then Range("单!d11") = 1 Else Range("单!d11") = 0
If Range("c" & x) = "电气" Then Range("单!q25") = 1 Else Range("单!q25") = 0 '自动选择维护分类
If Range("c" & x) = "机械" Then Range("单!q26") = 1 Else Range("单!q26") = 0
If Range("c" & x) = "仪表自控" Then Range("单!q27") = 1 Else Range("单!q27") = 0
If Range("c" & x) = "阀门" Then Range("单!q28") = 1 Else Range("单!q28") = 0
If Range("c" & x) = "其它" Then Range("单!q29") = 1 Else Range("单!q29") = 0
Range("E2:G2").Select '打开任务单
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Windows("维护汇总.xls").Activate
Sheets("单").Select '生成副本
Sheets("单").Copy After:=Workbooks("预防性维护任务单.xls").Sheets(1)
Windows("娄山河维护汇总.xls").Activate
Sheets("年计划表").Select
Rows(x).Select
SendKeys ("{down}")
Next i
End Sub 展开
Sub 自动任务()
Dim a As Single
Dim b As Single
Dim c As Single
Dim d As Single
Dim e
Dim f As Integer
Dim i As Integer
Dim x%
f = Range("i2")
For i = 1 To f
ActiveCell.Select '选择生成行
x = ActiveCell.Row
a = Range("h" & x)
Range("单!e5") = Range("a" & x) '将a1复制到任务单!e5
Range("单!m5") = Range("b" & x)
Range("单!u5") = Range("c" & x)
Range("单!ac5") = Range("d" & x)
Range("单!e6") = Range("e" & x)
Range("单!e7") = Range("g" & x)
Range("单!e10") = Range("i" & x)
Range("单!x7") = Now() '输入日期
If a > 0.9 Then b = 1 Else: b = 0
If a > 0.9 Then c = 0 Else: c = 1
If a > 0.9 Then d = a Else: d = 4 * a
If a > 0.9 Then e = "个月" Else: e = "个周"
Range("单!h25") = b
Range("单!i25") = c
Range("单!g27") = d
Range("单!h27") = e
If Range("d" & x) = "A" Then Range("单!b11") = 1 Else Range("单!b11") = 0 '自动选择维护等级
If Range("d" & x) = "B" Then Range("单!c11") = 1 Else Range("单!c11") = 0
If Range("d" & x) = "C" Then Range("单!d11") = 1 Else Range("单!d11") = 0
If Range("c" & x) = "电气" Then Range("单!q25") = 1 Else Range("单!q25") = 0 '自动选择维护分类
If Range("c" & x) = "机械" Then Range("单!q26") = 1 Else Range("单!q26") = 0
If Range("c" & x) = "仪表自控" Then Range("单!q27") = 1 Else Range("单!q27") = 0
If Range("c" & x) = "阀门" Then Range("单!q28") = 1 Else Range("单!q28") = 0
If Range("c" & x) = "其它" Then Range("单!q29") = 1 Else Range("单!q29") = 0
Range("E2:G2").Select '打开任务单
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Windows("维护汇总.xls").Activate
Sheets("单").Select '生成副本
Sheets("单").Copy After:=Workbooks("预防性维护任务单.xls").Sheets(1)
Windows("娄山河维护汇总.xls").Activate
Sheets("年计划表").Select
Rows(x).Select
SendKeys ("{down}")
Next i
End Sub 展开
1个回答
展开全部
f = Range("i2")
For i = 1 To f
ActiveCell.Select '选择生成行
x = ActiveCell.Row
这个地方有问题
建议这样改
f = Range("i2")
ActiveCell.Select '选择生成行
x = ActiveCell.Row
For i = 1 To f
.......
x=x+1
next i
For i = 1 To f
ActiveCell.Select '选择生成行
x = ActiveCell.Row
这个地方有问题
建议这样改
f = Range("i2")
ActiveCell.Select '选择生成行
x = ActiveCell.Row
For i = 1 To f
.......
x=x+1
next i
更多追问追答
追问
已经改了,不过还是没有解决问题,因为我的表格我会筛选,所以,可能行不是按顺序来的,如1.5.10.11.16等,用x=x+1不行,所以我用了 SendKeys ("{down}"),如何解决这个问题呢
追答
读入内存数组,在内存数组中判断找到相应的行号,然后逐一处理
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询