VBA copy dynamic range from multi sheet to sheet3

hassan.ajam

New Member
Joined
Oct 25, 2012
Messages
2
I am trying to copy a range of valid cells "non-blank" from "sheet_a" ,"sheet_b" ,"sheet_c" to "sheet3"
i got some help and i was successful to copy from one sheet only. how to copy from all the sheets listed from the same workbook.

following is the VBA code i am using

Sub CopySample()
Dim shSrc As Worksheet
Dim shDst As Worksheet
Dim rSrc As Range
Dim rDst As Range
Dim numCol As Long ' number of columns to copy

On Error GoTo EH


numCol = 12

' select source and dest sheets
Set shSrc = ActiveWorkbook.Worksheets("SheetA")
Set shDst = ActiveWorkbook.Worksheets("Sheet3")


' Select initial rows
Set rSrc = shSrc.Cells(1, 1)
Set rDst = shDst.Cells(1, 1)


' loop over source
Do While rSrc <> "STOP"
' Test Source row, Qty = 0 and Name is not blank
With rSrc
If .Offset(0, 11) > 0 And .Value <> "" Then
'Copy
.Resize(1, numCol).Copy rDst.Resize(1, numCol)
Set rDst = rDst.Offset(1, 0)
End If
End With

Set rSrc = rSrc.Offset(1, 0)
Loop

Exit Sub
EH:
MsgBox "Error " & Err.Description
End Sub

Thanks
Ajam
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Ok let me put it in a simple example

Sheet_A have the following

[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD]6[/TD]
[TD]joe[/TD]
[TD]jim[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Ron[/TD]
[TD]jack[/TD]
[TD]459030[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]STOP[/TD]
[/TR]
</tbody>[/TABLE]

Sheet_B
[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD]6[/TD]
[TD]AJ[/TD]
[TD]Mac[/TD]
[TD]465456[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Joe[/TD]
[TD]Jim[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Sam[/TD]
[TD]Jack[/TD]
[TD]962310[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]STOP[/TD]
[/TR]
</tbody>[/TABLE]

Sheet_c
[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD]6[/TD]
[TD]Bill[/TD]
[TD]Ron[/TD]
[TD]170456[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Will[/TD]
[TD]Mac[/TD]
[TD]464646[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD]Jack[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Stop[/TD]
[/TR]
</tbody>[/TABLE]


when i run the Macro I should copy sheet_A, sheet_b and sheet_c to sheet 3 based on cell in Column 4 is > 0 then do until cell in column 4 = "stop"

[TABLE="class: grid, width: 150"]
<tbody>[TR]
[TD]6[/TD]
[TD]Joe[/TD]
[TD]Jim[/TD]
[TD]123456[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]AJ[/TD]
[TD]Mac[/TD]
[TD]465456[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Bill[/TD]
[TD]Ron[/TD]
[TD]170456[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Will[/TD]
[TD]Mac[/TD]
[TD]464646[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Sam[/TD]
[TD]Jack[/TD]
[TD]962310[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Ron[/TD]
[TD]Jack[/TD]
[TD]459030[/TD]
[/TR]
</tbody>[/TABLE]

Thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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