using an input box to set up how many times a loop will run

时间:2015-07-28 16:05:18

标签: excel vba excel-vba

I have a code that puts spaces in a column of numbers, so that each group of 6 numbers have 2 spaces between them, starting from the top of the sheet. After the groups of 6 there can be groups of 5, these need to have 3 spaces between them. The groups of 6 always appear above the groups of 5. I would like to have a code that asks how many groups of 6, then asks for how many groups of 5, then puts in the relevant spacing.

Sub MacroMan()
Dim x As Integer

x = 8


For i = 1 To CInt(InputBox("Run this many times:"))
'Range("H2").Paste
Application.CutCopyMode = False
Range("H&x:H&x+1").Insert Shift:=xlDown

'Range("H2:H110").Copy

x = x + 8
Next
End Sub
I've got the code to run now, but I get the following error.
Run-time error '1004'
Method 'Range of object'_'Global'failed

2 个答案:

答案 0 :(得分:1)

您可以询问用户输入并将答案存储在字符串中。然后将字符串转换为整数并使用它来确定循环次数。

SELECT Id FROM Table1 t1
WHERE NOT EXISTS (
    SELECT 1 FROM Table1 t2
    WHERE t1.WID = t2.WID AND t1.AID = t2.AID AND t1.DateValue < t2.DateValue
)

答案 1 :(得分:0)

对于您指定的次数,这应该做同样的事情。

重要提示:始终在数据的 副本 上运行未经测试的代码,因为执行的代码无法撤消!

Sub SO()

Dim x As Integer, i As Integer

x = 8

Application.CutCopyMode = False

For i = 1 To CInt(InputBox("Run this many times:"))
    Range("H" & x & ":H" & x + 1).Insert Shift:=xlDown
    x = x + 8
Next

End Sub