+ Reply to Thread
Results 1 to 10 of 10

Required vba code copy data sheets to new workbook & retn to old sheet userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-16-2018
    Location
    India
    MS-Off Ver
    2021
    Posts
    177

    Required vba code copy data sheets to new workbook & retn to old sheet userform

    Dear Sir,

    I want to required vba code copy data from sheets to new workbook .
    for exp ;
    1. listbox1.value select option 1 then copy sheet1 to new workbook
    2. listbox1.value select option 2 then copy sheet2 to new workbook
    3. listbox1.value select option 3 then copy sheet3 to new workbook

    when complete the task , then goto return userform1 .

    thank you all..

    listbox.png

    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim wbI As Workbook, wbO As Workbook
        Dim wsI As Worksheet, wsO As Worksheet
        
    If ListBox1.Value = "COPY SHEET 1 TO NEW WORKBOOK" Then
        Set wbI = ThisWorkbook
        Set wsI = wbI.Sheets("Sheet1")
        Set wbO = Workbooks.Add
        With wbO
            Set wsO = wbO.Sheets("Sheet1")
            'Save the file
            '.SaveAs Filename:="C:\Book2.xls", FileFormat:=56
    
            'wsI.Range("A1:B10").Copy
             wsI.Cells.Copy
             wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=False
            
             Application.ScreenUpdating = True
         End With
    ElseIf ListBox1.Value = "COPY SHEET 2 TO NEW WORKBOOK" Then
        Set wsI = wbI.Sheets("Sheet2")
        Set wbO = Workbooks.Add
        With wbO
            Set wbI = ThisWorkbook
             Set wsO = wbO.Sheets("Sheet1")
            'Save the file
            '.SaveAs Filename:="C:\Book2.xls", FileFormat:=56
    
            'wsI.Range("A1:B10").Copy
             wsI.Cells.Copy
             wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=False
            
             Application.ScreenUpdating = True
         End With
    ElseIf ListBox1.Value = "COPY SHEET 3 TO NEW WORKBOOK" Then
        Set wbI = ThisWorkbook
        Set wsI = wbI.Sheets("Sheet3")
        Set wbO = Workbooks.Add
        With wbO
             Set wsO = wbO.Sheets("Sheet1")
            'Save the file
            '.SaveAs Filename:="C:\Book2.xls", FileFormat:=56
    
            'wsI.Range("A1:B10").Copy
             wsI.Cells.Copy
             wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
             SkipBlanks:=False, Transpose:=False
            
             Application.ScreenUpdating = True
        
        End With
    End If
    End Sub
    
    Private Sub CommandButton2_Click()
    Unload Me
    End Sub
    
    Private Sub UserForm_Initialize()
    
    With ListBox1
        .AddItem "COPY SHEET 1 TO NEW WORKBOOK"
        .AddItem "COPY SHEET 2 TO NEW WORKBOOK"
        .AddItem "COPY SHEET 3 TO NEW WORKBOOK"
    End With
    
    End Sub

    error.png
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    Hi sbvaram,

    Try below code ...
    Private Sub CommandButton1_Click()
    
    Dim wb1 As Workbook, wb2 As Workbook
    Set wb1 = ThisWorkbook
    
    If ListBox1.ListIndex <> -1 Then
       Set wb2 = Workbooks.Add
       wb1.Sheets("Sheet" & ListBox1.ListIndex + 1).[A1].CurrentRegion.Copy wb2.Sheets(1).[A1]
    End If
    
    Unload Me
    
    End Sub
    Last edited by nankw83; 11-12-2020 at 12:57 PM.
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Forum Contributor
    Join Date
    06-16-2018
    Location
    India
    MS-Off Ver
    2021
    Posts
    177

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    Dear Sir, Thank you for your fast reply
    Sir, its working correctly.

    Can you give me the code of if else condition, because I can use the For loop in the future, and after the new workbook is created, I have to come back to the userform also. can is possible?

    Thank you again..

  4. #4
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    I'm not sure what you're trying to achieve ? Can you explain what you want to do & perhaps we can assist with the required code

  5. #5
    Forum Contributor
    Join Date
    06-16-2018
    Location
    India
    MS-Off Ver
    2021
    Posts
    177

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    For expample small change in listbox option
    1) i want to rows grater than 5000 amount in new worksheet
    2 ) i want to rows lower than 4000 amount in new worksheet
    in vba if condition and after cursor retn to userform1
    RETN TO USERFORM.png

    Thank You
    Attached Files Attached Files

  6. #6
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    Is this what you're looking for ? Data will be populated in Sheet2

    Sub CommandButton1_Click()
    
    Dim Rg As Range, Amt As String
    Set Rg = Sheets("Sheet1").[D1:D2]
    
    If ListBox1.ListIndex = -1 Then Exit Sub
    
    Amt = IIf(ListBox1.ListIndex = 0, ">5000", "<4000")
    Rg = Application.Transpose(Array("AMOUNT", Amt))
    
    Sheets("Sheet1").[A1].CurrentRegion.AdvancedFilter 2, Rg, Sheets("Sheet2").[A1:B1]
    Rg.ClearContents
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    06-16-2018
    Location
    India
    MS-Off Ver
    2021
    Posts
    177

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    ohh sorry sir,my typing mistake i want to actually above 5000 rows in all sheets output report in new Workbook not worksheet. & when task completed cursor is return to userform1.

  8. #8
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    Try below code ...
    Private Sub CommandButton1_Click()
    
    Dim wb1 As Workbook, wb2 As Workbook, ws As Worksheet, Cnt As Long, Amt As String
    
    If ListBox1.ListIndex = -1 Then Exit Sub
    Amt = IIf(ListBox1.ListIndex = 0, ">5000", "<4000")
    
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Add
    
    For Each ws In wb1.Sheets
       Cnt = Cnt + 1
       With ws.[A1].CurrentRegion
          .AutoFilter 2, Amt, xlFilterValues
          .Offset(IIf(Cnt > 1, 1, 0)).SpecialCells(12).Copy wb2.Sheets(1).Range("A" & Rows.Count).End(3)
          .AutoFilter
       End With
    Next
    
    wb1.Activate
    
    End Sub

  9. #9
    Forum Contributor
    Join Date
    06-16-2018
    Location
    India
    MS-Off Ver
    2021
    Posts
    177

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    nankw83 sir, Thank you very for your fast reply , & working Perfect

    Give it to me as i wanted.
    Thank you again..

  10. #10
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: Required vba code copy data sheets to new workbook & retn to old sheet userform

    Glad to help & thanks for the added Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] amend code copy data from userform to sheet before lastrow
    By ABDELFATTA in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-14-2020, 01:05 PM
  2. Userform Code Required to Control Data between Database and Input Sheet
    By Rudidw in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-28-2020, 01:15 AM
  3. [SOLVED] vba code to paste data from different sheets to one single sheet in same workbook
    By Ishwarind in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-11-2016, 02:39 AM
  4. VBA COde For copy and paste B column of every sheets to new workbook in one sheet
    By Chetan_raghu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2016, 02:22 PM
  5. [SOLVED] VBA code required to insert extra/New sheets with existing sheet data
    By mchilapur in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2015, 01:52 AM
  6. Code Required To Use Condition To Copy Data From Different Worksheets Into Another Sheet
    By lukerogers1987 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-29-2012, 05:26 AM
  7. Replies: 0
    Last Post: 08-11-2011, 01:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1