Better Code to Loop Through One Sheet, Spit Out Data in Other Sheet

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
I have the following code that loops through Sheet1 Column J to find "TOP LEVEL" or "MAKE" and it Grabs the PART NUMBER. Then when LSR_01 appears, I need to paste the data in Sheet2 four times. So for every LSR_01, it needs to iterate four times on Sheet2 along with the most recent PART NUMBER. How can I write this code to be more efficient? I don't have any issues with the code I am just trying to improve my VBA skills.
VBA Code:
Sub MrExcelTest()
Application.ScreenUpdating = False
Dim cl As Object, partNumber As String, lastRow As Long
    For Each cl In Sheets("Sheet1").Range("J:J")
        Select Case cl.Value
            Case "TOP LEVEL", "MAKE"
                partNumber = cl.Offset(0, -3).Value
            Case "LSR_01"
                Sheets("Sheet2").Activate
                lastRow = Cells(Rows.Count, 1).End(xlUp).Row
                Cells(lastRow, 1).Offset(1, 0).Select
                ActiveCell.Value = partNumber
                ActiveCell.Offset(1, 0).Value = partNumber
                ActiveCell.Offset(2, 0).Value = partNumber
                ActiveCell.Offset(3, 0).Value = partNumber
                ActiveCell.Offset(0, 1).Value = cl.Value
                ActiveCell.Offset(1, 1).Value = cl.Value
                ActiveCell.Offset(2, 1).Value = cl.Value
                ActiveCell.Offset(3, 1).Value = cl.Value
            Case ""
                Application.ScreenUpdating = True
                Exit Sub
            Case Else
        End Select
    Next cl
End Sub

Snag_4e505ed.png


Snag_4e52898.png


Here is what the answer should look like.

Snag_4e55f86.png
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This might be slightly more efficient.
VBA Code:
Sub MrExcelTest()
Dim cl As Range, partNumber As String, lastRow As Long

    Application.ScreenUpdating = False

    For Each cl In Sheets("Sheet1").Range("J:J")
    
        Select Case cl.Value
            Case "TOP LEVEL", "MAKE"
                partNumber = cl.Offset(0, -3).Value
            Case "LSR_01"
                With Sheets("Sheet2")
                    lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
                    .Cells(lastRow, 1).Offset(1, 0).Resize(4).Value = partNumber
                    .Cells(lastRow, 1).Offset(1, 1).Resize(4).Value = cl.Value
                End With
            Case ""
                Application.ScreenUpdating = True
                Exit Sub
            Case Else
        End Select
        
    Next cl
    
End Sub
 
Upvote 0
This might be slightly more efficient.
VBA Code:
Sub MrExcelTest()
Dim cl As Range, partNumber As String, lastRow As Long

    Application.ScreenUpdating = False

    For Each cl In Sheets("Sheet1").Range("J:J")
   
        Select Case cl.Value
            Case "TOP LEVEL", "MAKE"
                partNumber = cl.Offset(0, -3).Value
            Case "LSR_01"
                With Sheets("Sheet2")
                    lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
                    .Cells(lastRow, 1).Offset(1, 0).Resize(4).Value = partNumber
                    .Cells(lastRow, 1).Offset(1, 1).Resize(4).Value = cl.Value
                End With
            Case ""
                Application.ScreenUpdating = True
                Exit Sub
            Case Else
        End Select
       
    Next cl
   
End Sub
This is definitely more efficient. Thank you!
 
Upvote 0
How about
VBA Code:
Sub MrExcelTest()
   Application.ScreenUpdating = False
   Dim cl As Object, partNumber As String, lastRow As Long
   
   With Sheets("Sheet2")
      For Each cl In Sheets("Sheet1").Range("J1:J" & Rows.Count).End(xlUp)
         Select Case cl.Value
            Case "TOP LEVEL", "MAKE"
               partNumber = cl.Offset(0, -3).Value
            Case "LSR_01"
               lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
               .Cells(lastRow, 1).Offset(1, 0).Resize(4, 2).Value = Array(partNumber, cl.Value)
         End Select
      Next cl
   End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub MrExcelTest()
   Application.ScreenUpdating = False
   Dim cl As Object, partNumber As String, lastRow As Long
  
   With Sheets("Sheet2")
      For Each cl In Sheets("Sheet1").Range("J1:J" & Rows.Count).End(xlUp)
         Select Case cl.Value
            Case "TOP LEVEL", "MAKE"
               partNumber = cl.Offset(0, -3).Value
            Case "LSR_01"
               lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
               .Cells(lastRow, 1).Offset(1, 0).Resize(4, 2).Value = Array(partNumber, cl.Value)
         End Select
      Next cl
   End With
End Sub
Thank you! I like how it does both at the same time!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,867
Members
449,053
Latest member
Mesh

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