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
 
I am confused. None of those seem to resemble the structure of the Log sheet example you posted in your initial post.
I thought we were starting with that, and when we are done, it should look like the last picture in the initial post.
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Sorry for the confusion, Joe.

The examples and file only have "fictional" data, I deleted all the data on the sheet 'log' so you could check it out fresh.
You can see what happens when you run the macro, what will be sent from sheet 'Venda' to sheet 'Log'

The whole idea is to press one button and it will fill up the log sheet with data from venda sheet, and also at the same time fill in the blanks.
 
Upvote 0
OK, I see what is going on. Your earlier code is making Excel think that the last row is row 11, not row 6. I think it may have something to do with transposing the data.
We will need to step through it to see what is going on. I need to step away for a while, but will be back on later tonight.
 
Upvote 0
Sorry for the delay in getting back to you. I never did make it back to the computer last night. Family affairs went longer than I expected.

I think I found the source of the problem. It is this line of your code:
VBA Code:
.Range("d11:f11").Resize(10).Copy   'order
Basically, with "Resize(10)", you are telling it to copy 10 rows, even when there is less rows of data than that. So the macro sells G11 as the end row after the paste, even though the data really only goes down to G6.

So instead of telling it to copy 10 rows every time, let's find the ending row of data and only go down that far.
So, add another variable to your variable section like this:
VBA Code:
Dim l2 as Long
and then replace the line of code I mentioned above with this instead:
VBA Code:
lr2 = .Range("D10").End(xlDown).Row
.Range("D11:F" & lr2).Copy
and I think that should fix it for you.
 
Upvote 0
Don't worry about it Joe, thanks a lot again for all your help. Hope you and your family are doing good during these times.

It worked, the data is copied and filled in - thank you so much for taking the time on helping me sort this out!
 
Upvote 0
You may get that error if it is not finding any blanks in your range.
One way around it is something like this:
VBA Code:
    Dim rng2 As Range
   
    On Error Resume Next
    Set rng2 = rng.SpecialCells(xlCellTypeBlanks)
    On Error GoTo 0
   
    If Not rng2 Is Nothing Then
        rng2.FormulaR1C1 = "=R[-1]C"
    End If
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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