Small Coding Problem for Excel Macro

  • Thread starter Thread starter darkchild
  • Start date Start date
  • Tags Tags
    Coding Excel
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 3K views
darkchild
Messages
153
Reaction score
0
I want to code a macro that will take in an integer as a row number and insert two rows above that row. This will repeat in a for loop. The problem is that I'm not familiar with VB syntax. Here's what I have so far:

Sub Insert_Rows_Loop()
Dim CurrentSheet As Object
Dim x As Integer

' Loop through all selected sheets.
For Each CurrentSheet In ActiveWindow.SelectedSheets
For x = 14 To 418
' Insert 2 rows depending on values of a1 and a2.
CurrentSheet.Range("a" & x &:"a" & (x+1)&").EntireRow.Insert
Next x
Next CurrentSheet
End Sub

There is some sort of syntax error in the CurrentSheet.Range argument. How does one specify a range with a variable?

Thanks.
 
Physics news on Phys.org
Try this...

CurrentSheet.Range("a" & x & ":a" & (x+1)).EntireRow.Insert
 
Thanks, but that just deleted everything :) I found another macro to use.