Small Coding Problem for Excel Macro

  • Thread starter Thread starter darkchild
  • Start date Start date
  • Tags Tags
    Coding Excel
AI Thread Summary
The discussion revolves around creating a VBA macro to insert two rows above a specified row number in an Excel worksheet. The user initially provided a code snippet but encountered a syntax error in the range specification. The correct syntax for defining a range with variables was clarified, emphasizing the need to use the correct concatenation method. However, after attempting the suggested correction, the user reported that the macro deleted existing data instead of inserting rows. Ultimately, the user decided to seek an alternative macro solution.
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.
 
Computer science 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.
 
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...

Similar threads

Replies
6
Views
55K
Replies
1
Views
5K
Replies
6
Views
3K
Replies
9
Views
5K
Replies
10
Views
2K
Replies
4
Views
2K
Replies
11
Views
9K
Back
Top