Copying a column into another sheet from a reference point and up to a certain cell value

kidneythief

New Member
Joined
Mar 17, 2021
Messages
34
Office Version
  1. 365
Platform
  1. Windows
Hello! First off, apologies as I can't get my xl2bb add-in to work so I've attached images instead.

In the Output Sheet, there are two cells for inputs - one for the date (E2) and one for a truck number (E3).
Once these are filled in, they are combined into G3.

I need help with code that will take the contents of G3 as a reference and find its match in the Source Sheet.
From there, I need to copy the numbers in the B2 column Source Sheet to the right of the G3 match, below the SI#
label up to the next SI# label.

So in the sample images, if G3 in the Output Sheet is "1/3/23--- Truck 2", I need to pull B8:B12 from Source Sheet and
copy them into the blue area in Output Sheet, below the SI# header.

My vba learning progress has been extremely slow 😓 Any help on this would be greatly appreciated!
 

Attachments

  • Output Sheet.jpg
    Output Sheet.jpg
    50.5 KB · Views: 8
  • Source Sheet.jpg
    Source Sheet.jpg
    32.4 KB · Views: 7

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
In your images, you have the date reversed.
1677002319415.png



But I'm going to assume that on both sheets, it should be like this:
TRUCK 2--- 1/3/23
Or whatever, but they are the same.

Try this:

VBA Code:
Sub copytruck()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim f As Range
  Dim truck As String
  Dim i As Long, j As Long
 
  Set sh1 = Sheets("Output")
  Set sh2 = Sheets("Source")
  truck = sh1.Range("G3").Value
 
  j = 7
  sh1.Range("E7:E" & Rows.Count).ClearContents
  If truck <> "" Then
    Set f = sh2.Range("A:A").Find(truck, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      For i = f.Row + 1 To sh2.Range("B" & Rows.Count).End(3).Row
        If sh2.Range("A" & i).Value <> "" Then
          Exit Sub
        Else
          sh1.Range("E" & j).Value = sh2.Range("B" & i).Value
          j = j + 1
        End If
      Next
    End If
  End If
End Sub

Or this:

VBA Code:
Sub copytruck_2()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim f As Range
  
  Set sh1 = Sheets("Output")
  Set sh2 = Sheets("Source")
  
  sh1.Range("E7:E" & Rows.Count).ClearContents
  If sh1.Range("G3").Value = "" Then Exit Sub
  Set f = sh2.Range("A:A").Find(sh1.Range("G3").Value, , xlValues, xlWhole, , , False)
  If Not f Is Nothing Then _
    f.Resize(sh2.Range("B" & Rows.Count).End(3).Row).SpecialCells(xlCellTypeBlanks).Areas(1).Offset(, 1).Copy sh1.Range("E7")
End Sub
 
Last edited:
Upvote 0
Solution
Thank you Dante, the first one worked perfectly! I'm breaking down your code to try to learn from it and understand better, too. Also added a calculate sub to have the code run each time G3 changes.
Again, thanks for your time!
 
Upvote 1

Forum statistics

Threads
1,215,640
Messages
6,125,976
Members
449,276
Latest member
surendra75

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