+ Reply to Thread
Results 1 to 4 of 4

With Statement Questions

Hybrid View

OzTrekker With Statement Questions 07-01-2007, 11:27 AM
royUK can you be a little more... 07-01-2007, 11:51 AM
OzTrekker royUK, Sorry it was 1:30am... 07-01-2007, 09:18 PM
royUK I don't think you need to add... 07-02-2007, 02:44 AM
  1. #1
    Forum Contributor
    Join Date
    06-18-2007
    Location
    Kiama, Australia
    MS-Off Ver
    365
    Posts
    119

    With Statement Questions

    Hi everybody,

    I have two (2) questions.

    1.
    Can you copy a range within one "With" statement and paste it in another "With" statement immediately following the first? I've tried and "CutCopyMode" seems to turn off when the first "With" statement is ended.

    2.
    The following code was originally written to run outside a "With" statement. I have placed it in a "With" statement but I know the syntax is wrong on several lines. Can anybody give me the correct syntax?

    With Workbooks("ONLINE.xls").Worksheets("System Export")
        ' Select cell A4 and PasteSpecial values.
        ActiveSheet.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False  ' Cancel Cut or Copy mode.
        ' Resize the current selection to remove the header row from the data sort.
        Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, Selection.Columns.Count).Select
        ' Sort the slected range by txtSurname then txtFirstName.
        Selection.Sort Key1:=ActiveCell.Offset(0, 4), Order1:=xlAscending, Key2:=ActiveCell.Offset(0, 5), Order2:=xlAscending
    End With

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    can you be a little more precise/ Yoour code doesn't copy anything, state what you want to copy & where you want to paste to.
    Hope that helps.

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

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    06-18-2007
    Location
    Kiama, Australia
    MS-Off Ver
    365
    Posts
    119
    royUK,

    Sorry it was 1:30am and brain fade/frustration had set in.
    I'm new to VBA which doesn't help either.
    Hopefully this will give you a clearer picture.

    Sub Copy_Centre_System_Data()
    '
    ' Macro created 30/06/2007 by Craig.
    ' Copy the Centre System data from the User nominated file.
    '
        Dim rBottomCellA As Range  ' Declare the local variable rBottomCellA as a range.
        
        ' Execute a series of statements on the "varOpenFile" workbook in the "Details - Alphabetic" worksheet.
        With Workbooks(varOpenFile).Worksheets("Details - Alphabetic")
            ' This User selected worksheet is produced by the Centre System 'Details' report (LAR127).
            ' The data in this worksheet is not contiguous.  To overcome this scenario manually identify the top left cell
            ' (A1) of the dataset and test to identify the bottom left cell of the dataset.
            Set rBottomCellA = .Cells(65536, 5)  ' Set the range rBottomCellA equal to the last Row of Column E.
            If IsEmpty(rBottomCellA) Then  ' If the range rBottomCellA is empty.
                ' Set the range rBottomCellA equal to the value of the next populated cell above and 4 cells to the left.
                Set rBottomCellA = rBottomCellA.End(xlUp).Offset(0, -4)
            End If
            ' Select the range A1:rBottomCellA and 33 Columns to the right creating a range and copy it.
            .Range("A1", rBottomCellA.Offset(0, 33)).Copy
        End With
        
        ' ************************************************************************************************************
        ' I want to place the following section of code in a "With" statement so I don't have to activate the workbook
        ' but have struggled with the syntax and the "Paste".
        ' ************************************************************************************************************
        
        ' Activate the "Centre System Export" worksheet in the "Centre System To clubs ONLINE.xls" workbook.
        Workbooks("Centre System To clubs ONLINE.xls").Worksheets("Centre System Export").Activate
        ActiveSheet.Unprotect (conPasswd)  ' Unprotect the active sheet.
        ' Select cell A4 and PasteSpecial values to the active cell.
        ActiveSheet.Range("A4").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False  ' Cancel Cut or Copy mode and remove the moving border.
        ' Resize the current selection to remove the header row from the data sort.
        Selection.Offset(1, 0).Resize(Selection.Rows.Count - 1, Selection.Columns.Count).Select
        ' Sort the slected range by txtSurname then txtFirstName.
        Selection.Sort Key1:=ActiveCell.Offset(0, 4), Order1:=xlAscending, Key2:=ActiveCell.Offset(0, 5), _
            Order2:=xlAscending
        Workbooks(varOpenFile).Close SaveChanges:=False  ' Close the User selected file without saving changes.
        ActiveSheet.Protect (conPasswd)  ' Protect the active worksheet.
    
    End Sub

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I don't think you need to add the actual pasting/sorting to the first With Statement.

+ 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