How do I output the result on a different sheet?

richard cheung

New Member
Joined
Aug 23, 2021
Messages
3
Office Version
  1. 365
1629699420369.png

Here is the code for the Excel VBA
---

Sub CopyData()
'Updateby Extendoffice
Dim xRow As Long
Dim VInSertNum As Variant
xRow = 1
Application.ScreenUpdating = False
Do While (Cells(xRow, "A") <> "")
VInSertNum = Cells(xRow, "B")
If ((VInSertNum > 1) And IsNumeric(VInSertNum)) Then
Range(Cells(xRow, "A"), Cells(xRow, "B")).Copy
Range(Cells(xRow + 1, "A"), Cells(xRow + VInSertNum - 1, "B")).Select
Selection.Insert Shift:=xlDown
xRow = xRow + VInSertNum - 1
End If
xRow = xRow + 1
Loop
Application.ScreenUpdating = False
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Copy the original data to a new sheet then run your code.
 
Upvote 0
I meant, how do i automatically copy from "Sheet1" and paste in "sheet2" with 1 click of a button? Currently it only able to paste in the same spreadsheet
 
Upvote 0
VBA Code:
Sub CopyData()
    Dim s1 As Worksheet, s2 As Worksheet, _
        endRow&, rep%
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    endRow = 1
    For i = 1 To s1.Cells(Rows.Count, 1).End(3).Row
        rep = IIf(IsNumeric(s1.Cells(i, 2).Value), s1.Cells(i, 2).Value, 1)
        s1.Cells(i, 1).Resize(, 2).Copy s2.Cells(endRow, 1).Resize(rep, 2)
        endRow = endRow + rep
    Next i
End Sub
 
Upvote 1
I meant, how do i automatically copy from "Sheet1" and paste in "sheet2" with 1 click of a button? Currently it only able to paste in the same spreadsheet
Put code at the start of your macro to paste the data from Sheet1 to Sheet2 (get the code from the macro recorder).
 
Upvote 0
VBA Code:
Sub CopyData()
    Dim s1 As Worksheet, s2 As Worksheet, _
        endRow&, rep%
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    endRow = 1
    For i = 1 To s1.Cells(Rows.Count, 1).End(3).Row
        rep = IIf(IsNumeric(s1.Cells(i, 2).Value), s1.Cells(i, 2).Value, 1)
        s1.Cells(i, 1).Resize(, 2).Copy s2.Cells(endRow, 1).Resize(rep, 2)
        endRow = endRow + rep
    Next i
End Sub
Works for me, thank you!
VBA Code:
Sub CopyData()
    Dim s1 As Worksheet, s2 As Worksheet, _
        endRow&, rep%
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    endRow = 1
    For i = 1 To s1.Cells(Rows.Count, 1).End(3).Row
        rep = IIf(IsNumeric(s1.Cells(i, 2).Value), s1.Cells(i, 2).Value, 1)
        s1.Cells(i, 1).Resize(, 2).Copy s2.Cells(endRow, 1).Resize(rep, 2)
        endRow = endRow + rep
    Next i
End Sub
[/QUOTE]
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
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