Consulting

Results 1 to 6 of 6

Thread: VBA to copy specific cells to specific columns in different workbook

  1. #1

    VBA to copy specific cells to specific columns in different workbook

    Looking for some help, as I cant quite find exactly what I'm looking for.

    I have a sales quote form template that certain data is captured on, in certain cells. I need to transfer this data into another workbook, into certain columns, but onto two sheets, one being a summary where all quotes are captured, the second being month specific, so January quotes go onto Januarys tab, etc. etc. Obviously this data will need to go onto the next empty line on the relevant sheet.

    I'm attaching samples of both workbooks. Quote Sheet will be a template and the source data while Quote Capture Register 2015 is the destination file. I have highlighted the cells to be copied and where they are to be copied to.

    If someone could assist I would be very grateful.

    thanks for you time.
    Attached Files Attached Files

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    pointer:
    Workbooks("Destination_Workbook.xlsm").Worksheets("Destination_Worksheet").Range(Destination_Cell).Value = Workbooks("Source_Workbook.xlsm").Worksheets("Source_Worksheet").Range(Source_Cell).Value
    you can use variables to easily write and read the code:
    Dim sWS As Worksheet, dWS As Worksheet
    
    Set sWS = Workbooks("Source_Workbook.xlsm").Worksheets("Source_Worksheet")
    Set dWS = Workbooks("Destination_Workbook.xlsm").Worksheets("Destination_Worksheet")
    
    'ex1:
    dWS.Range("A1").Value = sWS.Range("Z24").Value
    
    'ex2:
    dWS.Range("F46").Value = sWS.Range("A24").Value * sWS.Range("R17").Value
    
    '...
    '...
    '...
    you can run your 'final' code from both workbooks or from another workbook.
    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
    Thanks for your response mancubus, that's has helped me get started, and using your pointer and example I can get it to fill the summary sheet.

    However, I'm not experienced enough to modify the code to select the next empty row on the summary next time a form is submitted so everytime I enter new data, it just goes into row 4 everytime instead of the next blank row.

    Any chance of a pointer on how to do that bit?

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome.


    first declare a variable which will hold the last non blank cell's row number
    Dim LastRow as Long
    you can find the last blank non cell in column A like this:

    LastRow = dWS.Cells(Rows.Count, "A").End(xlUp).Row
    or
    LastRow = dWS.Cells(Rows.Count, 1).End(xlUp).Row
    or
    LastRow = dWS.Range("A" & Rows.Count).End(xlUp).Row
    (Rows.Count gives total nuımber of rows in a worksheet. Range("A" & Rows.Count) = Range("A1048576"))

    so first blank cell in colum A is LastRow + 1

    ex1 will be
    dWS.Range("A" &  LastRow + 1).Value = sWS.Range("Z24").Value

    or you can directly assign the row number of first non blank cell in column A
    LastRow = dWS.Range("A" & Rows.Count).End(xlUp).Offset(1,0).Row
    then
    dWS.Range("A" &  LastRow).Value = sWS.Range("Z24").Value
    Last edited by mancubus; 02-03-2015 at 08:46 AM. Reason: typo
    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)

  5. #5
    mancubus, you are the Man. Thanks for the help, that's doing exactly what I wanted for the first part. I'll just copy and paste for the second part cos even I can do that .

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome.

    thanks for the feedback and marking the thread as solved.
    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)

Posting Permissions

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