Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Copy multiple individual cells

  1. #1

    Copy multiple individual cells

    How do you copy multiple cells at a time in VBA? I need to copy cells L3, O3, O1, O2, in that order, and paste them in a new sheet. Help is much appreciated.


  2. #2
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    where in this new sheet are you pasting them?

  3. #3
    The sheet's in a seperate workbook which is already open and set as ActiveWorkbook

  4. #4
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    from what i can tell you will have to cut and past them one at a time.

    some thing like

    [VBA]workbooks("original").worksheets("Sheet1").range("L3").copy workbook("new").worksheet("Sheet1").range("x1")[/VBA]

    obviously you need to change the workbook names and sheet names to match what you want, but four calls of that with the right address should get you your desired results.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by figment
    from what i can tell you will have to cut and past them one at a time.
    Agreed,
    You can list the addresses in arrays and loop the copy to make it neater.
    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'

  6. #6

    Post

    I'm trying to open each and copy L3. It doesn't work. What am I missing?

        For Each File In FileList
            If InStr(File.Name, SearchChar) = 0 Then
                Range("A" & i).Value = File.Name
                Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbook("Money").Worksheet("Sheet 1").Range("E" & i)
                i = i + 1
            End If
        Next File

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you initialised i?
    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'

  8. #8
    Well, since I don't know what you mean, probably not.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    i = 1
    For Each File In FileList
    If InStr(File.Name, SearchChar) = 0 Then
    Range("A" & i).Value = File.Name
    Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbook("Money").Worksheet("Sheet 1").Range("E" & i)
    i = i + 1
    End If
    Next File
    [/VBA]
    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'

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    ...or maybe the space here, or the missing "s"
    Worksheet("Sheet 1")
    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'

  11. #11
    Yes, I had initiallized it. Thanks.

    The other things seem to be correct. Any other ideas?

  12. #12
    VBAX Regular
    Joined
    Jun 2008
    Posts
    64
    Location
    what error are you getting?

  13. #13
    Here's what I have so far.
        For Each File In FileList
            If InStr(File.Name, SearchChar) = 0 Then
                 wb1.Range("A" & i).Value = File.Name
                Workbooks.Open (Folder & "\" & File.Name), Password:=PwStr, UpdateLinks:=xlUpdateLinksNever
                i = i + 1
                Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbooks("Money").Worksheets("Kiln 1").Range("E" & i)
            End If
        Next File
    It says subscript out of range. This is what I just added:
                Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbooks("Money").Worksheets("Kiln 1").Range("E" & i)

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post the whole of your code?
    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'

  15. #15
    Sub Money()
    '
    ' Money Macro
    ' Keyboard Shortcut: Ctrl+Shift+Q
    
        Dim fso, Folder, FileList, File
        Dim SearchChar
        Dim PwStr
        Dim wb1 As Worksheet
        
        Set wb1 = ActiveWorkbook.Sheets("Money Chart")
    
        PwStr = Application.InputBox(Prompt:="Please type in the password; press cancel if none.", Type:=2)
            SearchChar = "Package"
     
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        Set Folder = fso.GetFolder(Trim(Range("A2").Value))
        Set FileList = Folder.Files
     
        i = 3
      
        For Each File In FileList
     
    
            If InStr(File.Name, SearchChar) = 0 Then
                '"A" will ensure that the data will start pasting from Column A
                wb1.Range("A" & i).Value = File.Name
                Workbooks.Open (Folder & "\" & File.Name), Password:=PwStr, UpdateLinks:=xlUpdateLinksNever
                i = i + 1
                Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbooks("Money").Worksheets("Sheet 1").Range("E" & i)
            End If
        Next File
        
    Set wb1 = Nothing
    
    End Sub

  16. #16
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    i have made a few changes and added a few comments and questions. my gut reaction is that your opening a workbook that doesn't have a histogram worksheet.

    [VBA]Sub Money()
    '
    ' Money Macro
    ' Keyboard Shortcut: Ctrl+Shift+Q

    Dim fso As Object, Folder, FileList, File
    Dim SearchChar
    Dim PwStr As String
    Dim wb1 As Worksheet

    Set wb1 = Worksheets("Money Chart")

    PwStr = Application.InputBox(Prompt:="Please type in the password; press cancel if none.", Type:=2)
    SearchChar = "Package"

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set Folder = fso.GetFolder(Trim(Range("A2").Value)) 'you should define what sheet this range is coming from
    Set FileList = Folder.Files

    I = 3

    For Each File In FileList
    If InStr(File.Name, SearchChar) = 0 Then
    '"A" will ensure that the data will start pasting from Column A
    wb1.Range("A" & I).Value = File.Name
    Workbooks.Open (Folder & "\" & File.Name), Password:=PwStr, UpdateLinks:=xlUpdateLinksNever
    I = I + 1 'should this be befor or after the copy?
    Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbooks("Money").Worksheets("Sheet 1").Range("E" & I)
    Workbooks(File.Name).Close
    End If
    Next File

    Set wb1 = Nothing

    End Sub[/VBA]

  17. #17
    Thanks for your reply. I get a "subscript out of range" here:

    [VBA]Workbooks(File.Name).Worksheets("Histogram").Range("L3").Copy Workbooks("Money").Worksheets("Sheet 1").Range("E" & I)[/VBA]

    I'm afraid that there is a worksheet called "Histogram".

  18. #18
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    try workbooks(left(file.name,len(file.name)-4) instead of workbooks(File.name)
    if your using excel 2008 files then use -5 it might be the .xls in the file name that is messing up the workbooks object.

    although it would probably be best to just declare a workbook object and set it equal to when the workbook is opened.

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    "Sheet 1"
    See post #10
    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'

  20. #20
    mdmackillop, I see what you said, but I don't understand what's different in mine. Thanks

Posting Permissions

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