Consulting

Results 1 to 17 of 17

Thread: export a range

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    export a range

    hello
    i have a list whitch i can selewct from through a macro values that meet a certain criteria like all names with a four character names (????).
    all those value turn red. i want to copy them to anothewr sheet and paste them
    there.
    what wrong with rhe macro i have
    [VBA] Sub copyselect()
    Dim cell As Range
    Dim newsheet As Worksheet
    For Each cell In selection
    If cell.Font.ColorIndex = 3 Then
    cell.Copy
    Set newsheet = Worksheets.Add
    newsheet.Range("A1").PasteSpecial paste:=xlValues
    End If
    Next
    End Sub
    [/VBA] thanks
    Last edited by johnske; 12-15-2005 at 02:20 AM. Reason: edited to include VBA tags
    moshe

  2. #2
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    export a range 2

    hello
    this is another more elaborate version of the macro.i can not make the macro paste the result i got (all cell turn red) into another sheet.
    please help
    thanks
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    moshe

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Moshe,
    Could you please edit your post to use the VBA tags around your code. It makes it more readable.
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    export a range 3

    hello
    how do i edite my tags
    thanks
    moshe

  5. #5
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    this is another more elaborate version of the macro.i can not make the macro paste the result i got (all cell turn red) into another sheet.
    please help
    thanks
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    moshe

  6. #6
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    moshe

  7. #7
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    [VBA] [/VBA]
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").Activate
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    moshe

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lior03
    hello
    how do i edite my tags
    thanks
    Click the VBA tags link in my signature below...
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    [VBA] like that?hello[VBA][/VBA]
    this is another more elaborate version of the macro.i can not make the macro paste the result i got (all cell turn red) into another sheet.
    please help
    thanks
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub[VBA][/VBA]
    [/VBA] [VBA][/VBA]
    moshe

  10. #10
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    [VBA] [VBA][/VBA] hello
    this is another more elaborate version of the macro.i can not make the macro paste the result i got (all cell turn red) into another sheet.
    please help
    thanks
    [VBA][/VBA]
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    [/VBA]
    moshe

  11. #11
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    [VBA] hello
    this is another more elaborate version of the macro.i can not make the macro paste the result i got (all cell turn red) into another sheet.
    please help
    thanks
    Sub likeit2()
    Range(selection, selection.End(xlDown)).Select
    selection.clearformats
    Cells.HorizontalAlignment = xlRight
    Application.ScreenUpdating = False
    Dim cell As Range
    Dim x As String
    Dim newsheet As Worksheet
    x = InputBox("select text format,do not use inverted commas:", "text finder", Default:="enter data like ???")
    If x = Empty Then Exit Sub
    For Each cell In selection
    If cell Like x Then
    cell.Font.ColorIndex = 3
    cell.Copy
    Set newsheet = Worksheets.Add
    Range("A1").PasteSpecial xlPasteValues
    selection.Font.Bold = True
    If Not cell Like x Then
    selection.Font.Bold = True
    Exit Sub
    Application.ScreenUpdating = True
    End If
    End If
    Next
    End Sub
    [/VBA]
    moshe

  12. #12
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Personally I find it quicker to have a reference formula that you can use to filter the results. For example, if your conditional formatting is highlight cells with a value <100, then you can have a formula like

    [VBA]=IF(C2<100,"Copy","")[/VBA]

    You can then record a macro that uses the autofilter to select all rows where the filter formula = "Copy", and copy those rows in one hit to a new sheet.

  13. #13
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    i am trying to export a range that meet a certain criteria - date's quarter number to a new sheet.
    [VBA]
    On Error Resume Next
    Dim cell As Range
    Dim qtr As Integer
    Dim qr As Integer
    If IsEmpty(selection) Then Exit Sub
    qtr = InputBox("select quarter number: 1 to 4", "select:")
    For Each cell In selection
    qr = DatePart("q", cell)
    If qr = qtr Then
    cell.Copy
    Sheets.Add
    ActiveSheet.paste
    End If
    Next

    [/VBA]
    how do i make the sheet open once ,with all dates that meet the criteria.
    moshe

  14. #14
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Your code is adding new sheet for every cell that meets your criteria. If you want one new sheet with all the matching values.

    Dim newSheet as Worksheet
    Rem code
    
    Set newSheet = Sheets.Add
    For Each cell In selection 
        qr = DatePart("q", cell) 
        If qr = qtr Then 
             newSheet.Range("A65536").End(xlup).Offset(1,0).Value=Cell.Value
        End If 
    Next 
    newSheet.Range("A1").Delete Shift:=xlup
    If newSheet.Range("A1").Value = vbNullString then newSheet.Delete
    Application.ScreenUpdating = True

  15. #15
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello
    the code failed to copy the dates.why?
    thanks
    moshe

  16. #16
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Does it copy anything?

  17. #17
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    no.as far as i understand the problem is with my add sheet command.how can i place it outside the loop and still make the macro select dates?
    thanks
    moshe

Posting Permissions

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