Consulting

Results 1 to 10 of 10

Thread: Dynamic selection of columns in Excel

  1. #1

    Question Dynamic selection of columns in Excel

    Hello, I am new to VBA and need your help.
    I want to create a macro to do this:
    When the user selects some columns, the content of those columns is copied to a new sheet. Will be copied only the rows marked with "yes" in a column which will not be copied.

    How do I use the user's selection in VBA? May be a range of columns which is not continue and it's a dynamic number of columns (user-defined).

    Thank you in advance, VBA -Community .

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [VBA]Sub CopyData()
    Const ID_COLUMN As String = "B"
    Dim ws As Worksheet
    Dim source As Range
    Dim lastrow As Long
    Dim i As Long

    Set ws = ActiveSheet
    Set source = Selection
    Worksheets.Add
    With ActiveSheet

    lastrow = ws.Cells(ws.Rows.Count, ID_COLUMN).End(xlUp).Row
    source.Copy .Range("A1")
    For i = 1 To lastrow

    If ws.Cells(i, ID_COLUMN).Value <> "yes" Then

    .Cells(i, "A").Resize(, source.Columns.Count).ClearContents
    End If
    Next i
    End With
    End Sub
    [/VBA]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Thanks, xld. I am very happy you answered so quickly.

    I am using M Office 2010 and perharps that's why I've got some errors.
    1) Type mismatch in
    [vba] lastrow = ws.Cells(ws.Rows.Count, ID_COLUMN).End(xlUp).Rows.Count [/vba]
    I replaced it with
    [vba] lastrow = ws.UsedRange.Rows.Count [/vba]
    2) The object is missing in the line
    [vba]source.Copy.Range ("A1")[/vba]
    If I just use it with . (dot) in front, I get also error.

    Please help.

    thanks a lot!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have 2010 as well, and it all worked in my tests, so maybe post a workbook.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    I have a huge workbook!

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have just looked again at the code you posted. That is not the code I gave you so I suggest you re-check what you did.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Hi,

    in this line
    [VBA]lastrow = ws.Cells(ws.Rows.Count, ID_COLUMN).End(xlUp).Row [/VBA]
    I've got "type mismatch", that"s why I change it a bit.

    So, the problem is still opened...
    Last edited by sisterProg; 11-27-2012 at 02:00 PM.

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    First time you said you got a type mismatch with

    [VBA]lastrow = ws.Cells(ws.Rows.Count, ID_COLUMN).End(xlUp).Rows.Count [/VBA]

    which is not what I gave, then you say it is with

    [VBA]lastrow = ws.Cells(ws.Rows.Count, ID_COLUMN).End(xlUp).Row[/VBA]

    Sorry, but I cannot trust what you have done, so there is no point trying anything unless you post the workbook with what code you have added so we can see what has been done.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    If you don't trust me, I trust you and this community.

    Let's see. I have a more than 10000 lines in my original worksheet. I created a model, and I attach it here. it seems the deletion of cells doesn't work.

    I must confess I would prefer to copy in the new sheet only the rows with a certain value in them, not to copy everything and delete after. That's because of the huge amount of input data which may not be all present in the resulting data.

    Thank you in advance. You're great!
    Attached Files Attached Files

  10. #10
    Any help, please?

Posting Permissions

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