Variable rows in macro

Mhtacker

New Member
Joined
Nov 18, 2023
Messages
13
Platform
  1. Windows
I created a macro to insert new row after change in column A. It works great. However I didn’t realize that the number of rows in the original data could vary. It may be more rows or less rows than what I initially had when creating the macro. I’m not good at coding so unable to understand how to make the macro recognize change in number of rows. Can you help?
 
The macro takes the first sheet in the work book and copies to second wksh where it sorts by column A, subtotals the sheet, then adds a row to each change in Column A then the sheet is copied to the last wksh for final output.
The first wksht is the input to daily and rows can vary from month to month - this is where I have an issue. This particular sheet had 60 rows, but my next sheet has 84 - some months may have 50.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
So what number of rows did you get when you ran the code that I posted and how many should it have been?
 
Upvote 0
So why didn't you state that you were getting the same error with the new code from post 19?

What is in column A below row 16 in the initial sheet?

and what do you get in the message box when you run the code below?

VBA Code:
Sub insertrowstestrows()

    Dim wb As Workbook, sht As Worksheet, rng As Range, lRow As Long
  
    Set wb = ThisWorkbook: Set sht = wb.ActiveSheet
    lRow = sht.Cells(Rows.Count, "A").End(xlUp).Row
  
    Set rng = sht.Range("A16:L" & lRow)
  
    MsgBox rng.Address
End Sub
 
Upvote 0
So why didn't you state that you were getting the same error with the new code from post 19?

What is in column A below row 16 in the initial sheet?

and what do you get in the message box when you run the code below?

VBA Code:
Sub insertrowstestrows()

    Dim wb As Workbook, sht As Worksheet, rng As Range, lRow As Long
 
    Set wb = ThisWorkbook: Set sht = wb.ActiveSheet
    lRow = sht.Cells(Rows.Count, "A").End(xlUp).Row
 
    Set rng = sht.Range("A16:L" & lRow)
 
    MsgBox rng.Address
End Sub
Sorry, this is my first attempt at ever modifying a macro and I simply don’t know what I’m doing. I will retrace and see what I get
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,953
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top