VBA - COPY AND PASTE RANGE OF CELLS IN MULTIPLE SHEETS

evernaren

New Member
Joined
Mar 7, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Dear all,

As i am beginner to VBA coding. i found this VBA code in this website under the above mentioned title. it was working fine when i run the code, But i have some different criteria. Can some one help me in the regard please.

In the below mentioned code it was mentioned the range of cells to be copied. but in my condition it is not like that, sheet name alpha as 15 rows and sheet name beta as 100 rows. i like to copy only the exact number of rows to the summary sheet. from column A to Column H. the row starts from 24th.

and After copying the contents to the Summary sheets, i like to sum the quantity of all the repeated items to a single item in another sheet call "consolidated sheet".

i also attached the sample file for your reference. Please help.

Private Sub CommandButton1_Click()
'Modified 10/12/2021 6:36:48 PM EDT
On Error GoTo M
Application.ScreenUpdating = False
Dim i As Long
Dim ans As String
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Sheets("MASTER").Cells(Rows.Count, "A").End(xlUp).Row
Lastrowa = 6

For i = 2 To Lastrow
ans = Sheets("MASTER").Cells(i, 1).Value

With Sheets(ans)
.Range("A19:F100").Copy
Sheets("SUMMARY").Cells(Lastrowa, 1).PasteSpecial xlPasteValues
Lastrowa = Sheets("SUMMARY").Cells(Rows.Count, "A").End(xlUp).Row + 1
End With
Next
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "You tried to use a sheet name that does not exist" & vbNewLine & "Or we had another problem"
Application.ScreenUpdating = True

End Sub

REFERENCE.xlsx
ABCDEFGH
222220305110-MNT
23REV : 1026-May-2022
24Sr. #Item CodeNo.DescriptionUomQTYUnit PriceTotal Price
26658110504081501. 16" Gear Operated Butterfly Valve Grooved, American Standard, UL/FM Approved, W/Tamper Switch, W/P 175 PsiPieces2.001,155.002,310.00
2738501. 2Water Flow Switch 6", UL Listed - FM ApprovedPieces2.00505.001,010.00
2836501. 3Test & Drain Valve, Size 2", Female x Female NPT, Brass, (1/2" Orifice), UL ListedPieces2.00893.001,786.00
294406105585225311. 4Pressure Gauge 0-300 Psi, 3-1/2" (90mm) Dia, 1/4" End Connection, UL Listed/FM ApprovedPieces2.0099.00198.00
304114111401085151. 51/2" Ball Valve, NPT Double Female Threaded, Brass Nickel Plated, Steel Lever Handle, W/P 175 Psi (12 Bar), UL ListedPieces2.0023.0046.00
313251110220086511. 6Check Valve 6", Ductile Iron Body to C508, Swing Type, EDPM Seat, Flanged Ends To ANSI B16.1, Class 125, FF Flange, W/P 200 Psi, RED RAL 3000 FBE Coating, UL/FM ApprovedPieces2.001,432.002,864.00
321. 7Installation for the aboveLOT2.001,680.003,360.00
3379302. 1Automatic Air Release Valve 1" FNPT inlet & 1/2" FNPT outlet , 175 Psi water, FM ApprovedPieces2.00470.00940.00
344116111401085252. 21" Ball Valve, NPT Double Female Threaded, Brass Nickel Plated, Steel Lever Handle, W/P 175 Psi (12 Bar), UL ListedPieces2.0055.00110.00
352. 3Installation for the aboveLOT2.00160.00320.00
3641161114010852531" Ball Valve, NPT Double Female Threaded, Brass Nickel Plated, Steel Lever Handle, W/P 175 Psi (12 Bar), UL ListedPieces2.0055.00110.00
37754112301221054. 1Fire Department Connection, Exposed Straight, 4" Female NPT Outlet and 2 x 2-1/2" Female NST Swivel Inlet, Double Drop Clapper, Brass, UL/FM ApprovedPieces2.001,064.802,129.60
383258110220086074. 2Check Valve 4", Ductile Iron Body to C508, Swing Type, EDPM Seat, Flanged Ends To ANSI B16.1, Class 125, FF Flange, W/P 300 Psi, RED RAL 3000 FBE Coating, UL/FM ApprovedPieces2.00858.001,716.00
394181108864752814. 3Cap (Plug) 2-1/2" Male NST with Chain, Brass FinishPieces4.00104.50418.00
4079604. 4Wall Plate for 4" Fire Department ConnectionPieces2.00118.00236.00
41327204. 53/4" NPT Male Inlet & Male Outlet, Automatic Ball DripPieces2.0072.00144.00
424. 6Installation for the aboveLOT2.001,200.002,400.00
435. 1Single Door Fire Hose Reel Cabinet, Recessed Type, Solid Door, Stainless steel Brush Finish #304 (Facial) and Back Box standard E.G Steel*** Without Rail***Pieces8.001,300.0010,400.00
44682110804220455. 21-1/2" Pressure Restricting Valve, NPT, Brass Finish, UL/FM ApprovedPieces8.00452.103,616.80
45439106211103325. 3Fire Hose Reel 1" x 30 Mtr. Cabinet Mounted, Swinging Automatic Type (Steel), c/w 1" Brass Chrome Plated Rotary Nozzle and 1-1/2" x 1" Tail Hose Coupling, SLIM TypePieces8.00710.005,680.00
465. 4Installation for the aboveLOT8.00650.005,200.00
474847111101080406Globe Valve 1 1/2" Bronze, UL ListedPieces8.00250.002,000.00
48313007. 1OS&Y Gate Valve 2", Ductile Iron Body, Rising Stem with EPDM Coated Resilient Wedge, W/P 200 Psi, RED RAL3000 FBE Coating, UL/FM ApprovedPieces45.00657.0029,565.00
4937907. 2Tamper Switch (Supervisory), UL/FM - Single ContactPieces45.00283.0012,735.00
504. 7VALVE 2"7. 3Installation for the aboveLOT45.00480.0021,600.00
51313308. 1OS&Y Gate Valve 4", Ductile Iron Body, Rising Stem with EPDM Coated Resilient Wedge, W/P 200 Psi, RED RAL3000 FBE Coating, UL/FM ApprovedPieces2.00876.001,752.00
5237908. 2Tamper Switch (Supervisory), UL/FM - Single ContactPieces2.00283.00566.00
534. 4VALVE 4"8. 3Installation for the aboveLOT2.00560.001,120.00
659ENGINEERING, TESTING & COMMISSIONINGLOT1.003,900.003,900.00
68TOTAL PRICE FOR THE ABOVE (IN SAR):118,232.40
1
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi evernaren. It's unclear what row numbers U actually want to transfer and where exactly U want to put them on the summary sheet? This bit of code transfers Worksheets("Alpha").Range("A24:H38") to Rows 1 to 15 on the summary sheet followed by Worksheets("Beta").Range("A24:H123") to rows 16 to 115 on the summary sheet. I have no idea what "After copying the contents to the Summary sheets, i like to sum the quantity of all the repeated items to a single item in another sheet call "consolidated sheet"." means. What repeated items? Put the values where on the consolidated sheet? Anyways, this will get U started. Adjust the range(s) to the actual rows that U want transferred. Save a copy of your wb and give this code a trial. HTH. Dave
Code:
Sub test()
Dim Rng As Range, Rng2 As Range, LastRow As Integer
'Grab Some Data and Store it in a "Range" variable
Set Rng = Worksheets("Alpha").Range("A24:H38")
Set Rng2 = Worksheets("Beta").Range("A24:H123")
'Transfer Values to same spot in another worksheet (Mimics PasteSpecial Values Only)
Worksheets("summary sheet").Range("A1").Resize(Rng.Rows.Count, _
                       Rng.Columns.Count).Cells.Value = Rng.Cells.Value
LastRow = Sheets("summary sheet").Cells(Rows.Count, "D").End(xlUp).Row
Worksheets("summary sheet").Range("A" & LastRow + 1).Resize(Rng2.Rows.Count, _
                       Rng2.Columns.Count).Cells.Value = Rng2.Cells.Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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