How do I copy multiple cells from one workbook to a master workbook onto the next row every time submit button used

jacjulie

New Member
Joined
May 17, 2019
Messages
2
Hello,
I am very new at writing vba. I copied a code that takes information entered from one workbook by hitting the submit button it then takes my data, opens the master log, transfers the data onto the worksheet horizontally, saves and closes both workbooks. example below of code being used.

Sub Button3_Click()


Dim mydata As Workbook


Worksheets("Secondary").Select
current = Range("I5")

Set mydata = Workbooks.Open("C:\Users\jjc\Documents\Master Secondary Sheet\Master Secondary Log.xlsx")
Worksheets("Sheet1").Select
Worksheets("Sheet1").Range("A1").Select
RowCount = Worksheets("Sheet1").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Sheet1").Range("A1")
.Offset(RowCount, 0) = current

How do I continue the code to take other cells/ranges on the original worksheet and copy them to the master log workbook to the next row, there will be 4 sets of rows with the same header information entered each time the submit button is pushed? The spreadsheet is set up to allow our technician to review our product from different shifts/different run times/ different defect codes, different date of inspection but same job number. I hope that I am not as confusing to you as I am to myself in trying to take the basic information entered and just sort it to different rows on the master log. Thank you in advance for your insight/help.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Welcome to the forum

Try this to copy a range:

Code:
Sub Button3_Click()
    Dim mydata As Workbook, RowCount As Long
    
    Worksheets("Secondary").Range("I5:I9").Copy
    Set mydata = Workbooks.Open("C:\trabajo\01.xlsx")
    RowCount = Worksheets("Sheet1").UsedRange.Rows.Count + 1
    mydata.Worksheets("Sheet1").Range("A" & RowCount).PasteSpecial xlValues
End Sub

Or copy a rows:

Code:
Sub Button3_Click()
    Dim mydata As Workbook, RowCount As Long
    
    Worksheets("Secondary").Rows("5:9").Copy
    Set mydata = Workbooks.Open("C:\trabajo\01.xlsx")
    RowCount = Worksheets("Sheet1").UsedRange.Rows.Count + 1
    mydata.Worksheets("Sheet1").Range("A" & RowCount).PasteSpecial xlValues
End Sub
 
Upvote 0
Thank you for taking your time to reply. I tried this code and it didn't work for what I was looking for or I don' t think I did it correctly. It copied the last cell in the code only to my master log. I'm trying to work with it more to see. I'm trying to copy ("I5") on my secondary worksheet log along with ("C5"), ("I7"), ("B11") and about 20 more cells that aren't in a row but move that to a row on the master log. I may have not explained myself clearly.
 
Upvote 0
Try this

Add in the red line the cells you want to copy, the cells will be copied in the order in which you put them, for example, cell "I5" is going to be pasted in column A, "C5" in B, " I7 "in C and" B11 "in column D.

Code:
Sub Button3_Click()
    Dim mydata As Workbook, mysheet As Worksheet, RowCount As Long
    Dim wCells As Variant, sh As Worksheet, i As Long
    
    wCells = Array([COLOR=#ff0000]"I5", "C5", "I7", "B11"[/COLOR])
    Set sh = Sheets("Secondary")
    
    Set mydata = Workbooks.Open([COLOR=#008000]"C:\Users\jjc\Documents\Master Secondary Sheet\Master Secondary Log.xlsx"[/COLOR])
    Set mysheet = mydata.Worksheets("Sheet1")
    RowCount = mysheet.UsedRange.Rows.Count + 1


    For j = 0 To UBound(wCells)
        mysheet.Cells(RowCount, j + 1).Value = sh.Range(wCells(j)).Value
    Next


    MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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