Small Coding Problem for Excel Macro

In summary, the conversation discusses coding a macro that will insert two rows above a given row number using a for loop and VB syntax. The individual expresses difficulty in specifying a range with a variable. They then mention finding another macro to use.
  • #1
darkchild
155
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
  • #2
Try this...

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

Related to Small Coding Problem for Excel Macro

1. What is a coding problem for Excel Macro?

A coding problem for Excel Macro is a specific task or issue that requires writing and implementing a VBA (Visual Basic for Applications) code within Microsoft Excel. These problems can range from automating tasks to creating custom functions.

2. Why is it important to know Excel Macro coding?

Knowing Excel Macro coding can greatly increase efficiency and productivity when working with large amounts of data in Excel. It allows for the automation of repetitive tasks and the creation of custom functions that can save time and effort.

3. What skills are needed to solve a small coding problem for Excel Macro?

To solve a small coding problem for Excel Macro, a basic understanding of VBA and Excel functions is necessary. Familiarity with the Excel Macro recorder and the ability to troubleshoot and debug code are also important skills to have.

4. Can anyone learn how to solve coding problems for Excel Macro?

Yes, anyone can learn how to solve coding problems for Excel Macro. There are many online resources and tutorials available to help individuals learn VBA and Excel functions. Practice and persistence are key to mastering coding for Excel Macro.

5. Are there any common mistakes to avoid when coding for Excel Macro?

Some common mistakes to avoid when coding for Excel Macro include not using proper syntax, not declaring variables, and not properly debugging code. It is important to follow best practices and double check code for errors to ensure it runs smoothly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Programming and Computer Science
Replies
18
Views
5K
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
55K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
5K
  • Programming and Computer Science
Replies
1
Views
12K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top