Consulting

Results 1 to 3 of 3

Thread: Solved: Question About copy Selection

  1. #1

    Question Solved: Question About copy Selection

    I tried this code to copy each selction cells to the sheets2. the probelm that i faced it's word with sheets2 too . I want to work for all sheets except sheets2
    [vba]
    Selection.Copy Sheets(2).Range("A1")
    [/vba]

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    when you activate a worksheet, previous selection will be copied to this sheet cell A1, if sheet name is not "Sheet2".

    first code works if the selection contains more than one cell.
    [vba]
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    Dim wks As Worksheet
    For Each wks In Worksheets
    If wks.Name <> "Sheet2" Then
    If Selection.Rows.Count > 1 Or Selection.Columns.Count > 1 Then
    Selection.Copy wks.Range("A1")
    End If
    End If
    Next

    End Sub
    [/vba]

    [vba]
    Private Sub Workbook_SheetActivate(ByVal Sh As Object)

    Dim wks As Worksheet
    For Each wks In Worksheets
    If wks.Name <> "Sheet2" Then
    Selection.Copy wks.Range("A1")
    End If
    Next

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    I tried this code too
    [VBA]
    If Not Selection.Parent.Name = Sheets(2).Name Then Selection.Copy Sheets(2).Range("A1")
    [/VBA]
    but the problem that I faced with my code and your first code is that when I select the cells in sheet1 and then select sheet2 to see the data it didn't print them only if I select sheets1 and then sheet2 ( I think because of the "Workbook_SheetActivate" Event

Posting Permissions

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