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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,216,077
Messages
6,128,674
Members
449,463
Latest member
Jojomen56

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