Loop a range and skip rows in Excel Worksheet

cribal48

New Member
Joined
Mar 19, 2020
Messages
1
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
I have an excel worksheet that accepts input from another excel file. This excel file has structured data in which I need to separate individually as sheets. I already have the following code to copy and format that data in a certain range but I need to loop this process for the whole worksheet until there's no more data to process.
The range currently I set is A2:P20 the next range is 4 rows below and that would be A25:P43.

The sheet that's going to be imported does not have a definite number of rows each time. I was able to loop the range but it returns an empty value causing an error.

VBA Code:
[/FONT]
Option Explicit

Public Sub CopySheetToClosedWorkbook()
    Dim fileName
    Dim closedBook As Workbook
    Dim currentSheet As Worksheet
    Dim lLastRow As Long
    Dim lRow As Long
    Dim lRangeSize As Long
    Dim lSpacerSize As Long
    Dim theRange
 
    fileName = Application.GetOpenFilename("Excel Files (*.xls*),*xls*")
 
    If fileName <> False Then
    
        Application.ScreenUpdating = False
 
        Set closedBook = Workbooks.Open(fileName)
        
        closedBook.Sheets(1).Range("A2:P20").Copy
        
        lRangeSize = 19
        lRow = 2
        lSpacerSize = 4
        lLastRow = 1000
        
        Do Until lRow > lLastRow

        Debug.Print Range("A" & lRow).Resize(lRangeSize, 16).Address

        theRange = Range("A" & lRow).Resize(lRangeSize, 16).Address
        
        lRow = lRow + lRangeSize + lSpacerSize
        
        closedBook.Sheets(1).Range(theRange).Copy
        
        ThisWorkbook.Worksheets("input").Range("A2").PasteSpecial xlPasteValues
        
        CopySheetAndRenameByCell2
    
        Loop
        
        Application.ScreenUpdating = True
        closedBook.Application.CutCopyMode = False
        closedBook.Close (True)
    
    End If
End Sub
[FONT=arial]
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi & welcome to MrExcel.
Will col A be blank between the rows that need to be copied?
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,312
Members
448,564
Latest member
ED38

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