Repeat VBA code to next row

edwardvolpe2012

New Member
Joined
Jan 21, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm creating a VBA code to transpose some information for me.

Breakdown of the data
Column A is a code or SKU
C1:E1 are production dates
C2:A4 are volumes

Ultimately the code I've written below tells me when production is starting for each code and populates the date of production start in column B
So for example if you look at row 2 the code will find the first cell with data in it, jump to the top row, pull the date, and place it in column B
I've gotten the code to work but I over 200 rows to apply this too (I've already done this and encountering compile error if I go over a 250 rows).
Instead of writing code for the next row / iteration 200 + times, I want to know if there is a way to repeat the code to each row.
I've been trying loop functions but cant figure it out, any help would be greatly appreciated.

Before Macro Run
1642780059063.png


End Result
1642780110616.png


For example:

Sub Macro1()

Range("A2").Select
Selection.End(xlToRight).Select
ActiveCell.EntireColumn.Select
Selection.End(xlUp).Select
Selection.Copy
Range("B2").Select
ActiveSheet.Paste


Range("A3").Select
Selection.End(xlToRight).Select
ActiveCell.EntireColumn.Select
Selection.End(xlUp).Select
Selection.Copy
Range("B3").Select
ActiveSheet.Paste

Range("A4").Select
Selection.End(xlToRight).Select
ActiveCell.EntireColumn.Select
Selection.End(xlUp).Select
Selection.Copy
Range("B4").Select
ActiveSheet.Paste


End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:
VBA Code:
Sub PullDate()
    Application.ScreenUpdating = False
    Dim v As Variant, r As Long, c As Long
    v = Range("A1").CurrentRegion.Value
    For r = 2 To UBound(v)
        For c = 3 To UBound(v, 2)
            If v(r, c) <> "" Then
                Range("B" & r) = v(1, c)
                Exit For
            End If
        Next c
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about this...

VBA Code:
Sub Columns()

    Dim arr, arr2
    Dim i As Long, x As Long
    Dim lastC
    
    lastC = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    x = 1
    arr = Range(Cells(1, 3), Cells(1, lastC))
    ReDim arr2(1 To UBound(arr, 2), 1 To 1)
    For i = UBound(arr2) To 1 Step -1
        arr2(i, 1) = arr(1, x)
        x = x + 1
    Next
    Range("B2").Resize(UBound(arr2)) = arr2
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,388
Messages
6,119,227
Members
448,878
Latest member
Da9l87

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