+ Reply to Thread
Results 1 to 15 of 15

User Input to set last column in range to copy

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    User Input to set last column in range to copy

    Hi All,

    If possible could someone please help with changing the below code so that column "D" in the rngIn part of the code can be a variable. i.e so the user can select one, two, three or four columns (with visible data) to be copied. The "b3" part is a constant.

    All help is appreciated.

    Sub x() 'Moves Visible Range to sheet and formats
      Application.ScreenUpdating = False
    Sheets("Edit").Activate
        Dim rngIn As Range
        Dim rngOut As Range
        Dim rngX As Range
        Dim answr As VbMsgBoxResult
    
    'answr = MsgBox("Tool", vbYesNo + vbQuestion, "Copy")
    'If answr = vbNo Then End
    
    
    'End With
    
    Set rngIn = Sheets("Edit").Range("b3:d" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
        For Each rngX In rngIn.Rows
            Set rngOut = Sheet24.Range("A" & Rows.Count).End(xlUp)
    Cheers
    Last edited by Zimbo; 04-15-2009 at 04:00 AM.

  2. #2
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: User Input to change last column in range to copy

    what is this actually doing?
    I dont like to use code i dont understand
    it makes it hard to use in other situations
    so please try to be as clear and patent as possible with me

    Criticism is welcomed

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to change last column in range to copy

    Maybe something like
      Application.ScreenUpdating = False
        Dim Col    As Long
        Dim rngIn  As Range
        Dim rngOut As Range
        Dim rngX   As Range
        Dim answr  As VbMsgBoxResult
        With Sheets("Edit")
            'answr = MsgBox("Tool", vbYesNo + vbQuestion, "Copy")
            'If answr = vbNo Then End
    
    
            'End With
            Col = Application.InputBox("Enter final column of data to copy", Type:=1)
            If Col < 2 Or Col > 4 Then Exit Sub    'change max & min column here
            Set rngIn = .Range(.Cells(3, 2), Cells(.Rows.Count, Col).End(xlUp).Row).SpecialCells(xlCellTypeVisible)
        End With
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Re: User Input to change last column in range to copy

    Hi Roy and Friel,

    Thanks for the prompt responses.

    The code is to allow a user to select the last column in a table to copy to a another sheet.

    The first column will always be a constant and by changing the input value they will either select more or less columns. (minimum of one).

    Roy, if I am not mistaken your code prevents a user from copying if they do not select a column in a fixed range.(This may be useful as an addition)

    Cheers

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to change last column in range to copy

    The c ode asks for the user to input a column, in the example between B & D. You can change this as necessary

  6. #6
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Red face Re: User Input to change last column in range to copy

    Hi Roy,

    My appologies your code should do exactly what I want.

    It is however coming up with a runtime error '1004', application-defined or object defined error.

    Any suggestions on where this needs to be changed.

    Thanks

  7. #7
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Re: User Input to set last column in range to copy

    Hi All,

    This is a Bump.
    Can anyone help me with a fix for the runtime error in the above post.

    Cheers

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to set last column in range to copy

    Have you go a sheet clled Edit/

    Attach the workbook that errors

  9. #9
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Re: User Input to set last column in range to copy

    Hi All,

    Please find attached my workbook with it's code included. The runtime error occurs after the user input is entered.

    The idea is that the user can select the rows they want to copy by hiding the unchecked rows (column A) with the toggle button then pressing the copy button. They can then select the last column of information to copy to sheets (sheet10) via the input box, the user will set the last column in range to either Narrative1 ,2 or 3.

    You will note that the infomation is copied into one column in sheets (sheet10).

    Thanks again for any help with this runtime error

    Cheers
    Attached Files Attached Files

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to set last column in range to copy

    Sorry but you need to not only revise the code, bu look at the design of the spreadsheet itself


    There are several errors in the code itself for e.g. you turn off screenupdating at the end of he code, this will have the effect of preventing the sheet refreshing & you will not see he sheet; yo seem to want to loop hrough cells, but use Rows instead of a range
     For Each rngX In rngIn.Rows
    Why are you using a ToggleButton instead of a Commandbutton? The code in this button again uses Row, but you don't need it

    I've tried to amend the code to do what I think you wan.
    Attached Files Attached Files
    Last edited by royUK; 04-14-2009 at 04:22 AM.

  11. #11
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Thumbs up Re: User Input to set last column in range to copy

    Thanks Roy,

    The code almost does exactly what I want it too.

    The only issue is that it is copying the rows based on the last selected column when it should copy all visible rows in column B and include or exclude information based on the user input.

    Cheers
    Last edited by Zimbo; 04-14-2009 at 11:27 PM.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to set last column in range to copy

    Is this what you mean/
    Private Sub CommandButton1_Click()
     Dim rngIn  As Range
        Dim rngOut As Range
        Dim rngCl  As Range
        Dim Col    As Long
        Dim Rw     As Long
        Dim LastRw As Long
        Dim answr  As VbMsgBoxResult
    
        With Sheet19
            Col = Application.InputBox("Enter final column of data to copy", Type:=1)
            If Col < 3 Or Col > 5 Then Exit Sub    'change max & min column here
            LastRw = .Cells(.Rows.Count, 2).End(xlUp).Row
            Set rngIn = .Range(.Cells(3, 2), .Cells(LastRw, Col))    '.SpecialCells(xlCellTypeVisible)
        End With
    
        With Sheet24
            .Cells.Clear
            Rw = 1
            For Each rngCl In rngIn
                If Not IsEmpty(rngCl) Then
                    .Cells(Rw, 1).Value = rngCl.Value
                    Rw = Rw + 1
                End If
            Next rngCl
    
            Application.CutCopyMode = False
            With .UsedRange
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
        End With
        'Call OtherBlaBla
        Application.Goto Sheet24.Cells(1, 1)
    End Sub

  13. #13
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Re: User Input to set last column in range to copy

    Hi Roy,

    That is almost it.

    The only thing being that when this code is run it is unhiding the hidden rows and copying evrything in the range.
    It only needs to copy the visible rows in the range that the user has set.

    Cheers

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: User Input to set last column in range to copy

    I've corrected that
    Private Sub CommandButton1_Click()
     Dim rngIn  As Range
        Dim rngOut As Range
        Dim rngCl  As Range
        Dim Col    As Long
        Dim Rw     As Long
        Dim LastRw As Long
        Dim answr  As VbMsgBoxResult
    
        With Sheet19
            Col = Application.InputBox("Enter final column of data to copy", Type:=1)
            If Col < 3 Or Col > 5 Then Exit Sub    'change max & min column here
            LastRw = .Cells(.Rows.Count, 2).End(xlUp).Row
            Set rngIn = .Range(.Cells(3, 2), .Cells(LastRw, Col)).SpecialCells(xlCellTypeVisible)
        End With
    
        With Sheet24
            .Cells.Clear
            Rw = 1
            For Each rngCl In rngIn
                If Not IsEmpty(rngCl) Then
                    .Cells(Rw, 1).Value = rngCl.Value
                    Rw = Rw + 1
                End If
            Next rngCl
    
            Application.CutCopyMode = False
            With .UsedRange
                .HorizontalAlignment = xlLeft
                .VerticalAlignment = xlBottom
                .WrapText = False
                .Orientation = 0
                .AddIndent = False
                .IndentLevel = 0
                .ShrinkToFit = False
                .ReadingOrder = xlContext
                .MergeCells = False
            End With
        End With
        'Call OtherBlaBla
        Application.Goto Sheet24.Cells(1, 1)
    End Sub

  15. #15
    Forum Contributor
    Join Date
    02-01-2008
    Location
    Perth, Australia
    MS-Off Ver
    Excel 365
    Posts
    135

    Thumbs up Re: User Input to set last column in range to copy

    Hi Roy,

    That does exactly what I need it to do.
    Thank you for your patience and help, it is really appreciated.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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