VBA copy and paste to new sheet based on number of rows

wolthers

New Member
Joined
Sep 2, 2015
Messages
18
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi, I am making a workbook to log all data based on coffee roast profiles sold.

Sales sheet has the following which I want to copy to log sheet.
B2 = sales ID
B4 = name (either company or person)

Then I can have data from D11 all the way to F20, and that's where I am finding difficulties, I can copy and paste D11:F11 but if I have more than one row then I will only have data on the first row on the log sheet.
D11E11F11
LightWhole1,250 g
MediumWhole2,500 g
DarkWhole2,500 g
MediumGround750 g


log sheet is as follows:
(A)Day(B)Order Nr(C)Client(D)Seller(E)Roast(F)Prep(G)Quantity
16-06-20SBC 00001BakerDanLightWhole1250
MediumWhole2500
DarkWhole2500
MediumGround750
16-06-20SBC 00002LawyerTomLightWhole1250
MediumWhole2500
DarkWhole2500
MediumGround750
16-06-20SBC 00003DentistJoeLightWhole1250
MediumWhole2500
DarkWhole2500
MediumGround750


should look as follows:
(A)Day(B)Order Nr(C)Client(D)Seller(E)Roast(F)Prep(G)Quantity
16-06-20SBC 00001BakerDanLightWhole1250
16-06-20SBC 00001BakerDanMediumWhole2500
16-06-20SBC 00001BakerDanDarkWhole2500
16-06-20SBC 00001BakerDanMediumGround750
16-06-20SBC 00001LawyerTomLightWhole1250
16-06-20SBC 00001LawyerTomMediumWhole2500
16-06-20SBC 00001LawyerTomDarkWhole2500
16-06-20SBC 00001LawyerTomMediumGround750
16-06-20SBC 00001DentistJoeLightWhole1250
16-06-20SBC 00001DentistJoeMediumWhole2500
16-06-20SBC 00001DentistJoeDarkWhole2500
16-06-20SBC 00001DentistJoeMediumGround750



my VBA macro:

VBA Code:
Sub insert()

Dim ms As Worksheet, NR
Set ms = Sheets("log")
   With Sheets("sales")

       NR = ms.Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
       

       ms.Range("A" & NR) = Date
       
           .Range("B2").Resize(1).Copy  'sale nr
       ms.Range("B" & NR).PasteSpecial xlValues
       
           .Range("B4").Resize(1).Copy  'client
       ms.Range("C" & NR).PasteSpecial xlValues
       
       myValue = InputBox("Quem vendeu") 'broker
       ms.Range("D" & NR).Value = myValue
       
'A B C and D should repeat the paste by the amount of rows between B11 and B20 with data

       .Range("d11:f11").Resize(10).Copy   'order
       ms.Range("E" & NR).PasteSpecial xlValues, Transpose:=False
    
    
   End With
  
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
VBA Code:
Sub dittoIt()
'
' dittoIt Macro
'

'
    Range("A1:G12").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Application.CutCopyMode = False
    Selection.FormulaR1C1 = "=R[-1]C"
End Sub
This will fill in the blanks
and then you may want to copy and paste as values
 
Upvote 0
You can fill in the missing values in your table using the methods described here (both manual and VBA options):
 
Upvote 0
Thanks Joe4, these are a bit complex for me, but I was able to get one column to fill up, do you know how I can make the below work for the first 4 columns?

VBA Code:
Sub FillColBlanks_Offset()
'by Rick Rothstein  2009-10-24
'fill blank cells in column with value above
'https://www.contextures.com/xlDataEntry02.html

  Dim Area As Range, LastRow As Long
  On Error Resume Next
  LastRow = Cells.Find(What:="*", SearchOrder:=xlRows, _
               SearchDirection:=xlPrevious, _
               LookIn:=xlFormulas).Row
  For Each Area  ms.Range("A1").EntireColumn(1).Resize(LastRow). _
               SpecialCells(xlCellTypeBlanks).Areas
    Area.Value = Area(1).Offset(-1).Value
  Next
End Sub

I tried messing with EntireColumn(4)
Area(1 through 4) will just choose which column based on column A.
 
Upvote 0
Here is some code that is based on braindiesel's method, but is a little more dynamic, and converts everything to values:
VBA Code:
Sub MyFillMacro()

    Dim rng As Range
    
'   Set range to apply this to
    Set rng = Range("A1").CurrentRegion

'   Fill in blanks with formulas
    rng.SpecialCells(xlCellTypeBlanks).Select
    Selection.FormulaR1C1 = "=R[-1]C"
    
'   Turn formulas into values
    rng.Value = rng.Value

End Sub
Note that I am assuming that your headers start in cell A1.
 
Upvote 0
Thanks Joe,

Also tried this code below earlier today which worked.

VBA Code:
'Activate target worksheet.
Worksheets(5).Activate
'Find the last row with data.
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

For x = 1 To LastRow
    'Don't do anything until the first time a non-blank cell in column A is reached.
    If Cells(x, 1) <> "" Then
        DataStarted = True
    'Then if there are blanks in the row, fill them in with the data from the cell above.
    ElseIf DataStarted Then
        LastCol = Rows(x - 1).Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        With Range(Cells(x, 1), Cells(x, LastCol))
            .SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
            .Value = .Value
        End With
    End If
Next x
 
Upvote 0
Just bear in mind that loops are rather slow, so if there are others ways of doing it, your code will be faster if you avoid looping.
The way I did it does not use any loops (and the code is a bit shorter too!).
 
Upvote 0
Thanks Joe, I have tested yours out and it's indeed faster, but it also copies down to 10 rows below, because of this other line:

VBA Code:
 .Range("d11:f11").Resize(10).Copy

It will only copy values if there are, the nr. of rows may vary depending on the size of the order.
Example below where I had 4 rows of items, it went all the down to 10 lines.

datesales idbuyersellerroastprepquantity
17-06-20SBC 00017BakerDanLightGround1250
17-06-20SBC 00017BakerDanMediumGround1000
17-06-20SBC 00017BakerDanlightWhole2000
17-06-20SBC 00017BakerDanMediumWhole1250
17-06-20SBC 00017BakerDanMediumWholeShould stop here
17-06-20SBC 00017BakerDanMediumWhole
17-06-20SBC 00017BakerDanMediumWhole
17-06-20SBC 00017BakerDanMediumWhole
17-06-20SBC 00017BakerDanMediumWhole
17-06-20SBC 00017BakerDanMediumWhole
 
Upvote 0
OK, assuming that quantity is in column G, we only need to make a few tweaks:
VBA Code:
Sub MyFillMacro()

    Dim lr as Long
    Dim rng as Range
   
'   Find last row with data in column G
    lr = Cells(Rows.Count, "G").End(xlUp).Row

'   Set range
    Set rng = Range("A1:G" & lr)

'   Fill in blanks with formulas
    rng.SpecialCells(xlCellTypeBlanks).Select
    Selection.FormulaR1C1 = "=R[-1]C"
   
'   Turn formulas into values
    rng.Value = rng.Value

End Sub
 
Upvote 0
Adjusted as per your last code, but it continues filling all the way down.

The button is pressed on another sheet, could that be it? I tried activating the worksheet with Worksheets(6).Activate but it didn't help
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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