Share via

How do I record a macro that involves two workbooks?

Anonymous
2015-07-10T03:05:25+00:00

I've tried using the record macro icon, but when I checked the macro, it recorded only my movements in the current workbook.

So is it possible to run a macro in the current workbook that would copy from a previous worksheet and paste on the current worksheet?

Because I intend to use that macro in the future, to copy as picture the sparkline graphs of hundreds of data, as it would be too tedious to do that, where I would have to click each sparkline graph to copy it as picture to the new workbook.

Thanks.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2015-07-10T04:13:41+00:00

ok,

1) Selection as picture in new WB

Sub Selection_AsPicture_NewWB()

'July 10, 2015

Const cel As String = "A1" '<< top left cell

If MsgBox("select a range ?", vbOKCancel) = vbCancel Then Exit Sub

Selection.CopyPicture xlScreen, xlBitmap

Set wb = Workbooks.Add(xlWorksheet)

With ActiveSheet

.Paste Destination:=.Range(cel)

.Range(cel).Select

End With

'wb.Save

End Sub

xxxxxxxxxxxxxxxxxxxxxxxxxxxx

2) Selection as picture in new Worksheet

Sub Selection_AsPicture_NewWS()

'July 10, 2015

Const cel As String = "A1" '<< top left cell

If MsgBox("select a range ?", vbOKCancel) = vbCancel Then Exit Sub

Selection.CopyPicture xlScreen, xlBitmap

Set ws = Sheets.Add(before:=Sheets(1))

With ActiveSheet

.Paste Destination:=.Range(cel)

.Range(cel).Select

End With

ThisWorkbook.Save

End Sub

Was this answer helpful?

0 comments No comments

19 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-07-10T04:42:36+00:00

    I was referring to 2 workbooks, and I just edited my question.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2015-07-10T04:14:17+00:00

    Do you mean to say - the macro must exist in one workbook and executed on other workbooks ?

    Are you referring to two worksheets or two workbooks ?

    It is better for you to be aware of basic VBA programming language.

    Record macro option helps you to generate a macro code based on the actions/steps which you as a user execute and not beyond that.

    Knowing VBA coding could be necessary for writing more complex macros.

    Hope this is Helpful.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-07-10T03:37:52+00:00

    I'm sorry, what I meant is a new workbook.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2015-07-10T03:25:05+00:00

    Hi,

    try this sample

    select a range from Activesheet  and paste (as picture) in sheet2  (2nd sheet tab)

    (target range, top left cell is B2)

    Sub CopyPaste_AsPicture()

    'July 10, 2015

    Const cel As String = "B2" '<< target cell, in 2nd sheet tab

    If MsgBox("select a range ?", vbOKCancel) = vbCancel Then Exit Sub

    Dim ws As Worksheet

    Set ws = Sheets(2) '<< 2nd sht tab

    Selection.CopyPicture xlScreen, xlBitmap

    ActiveSheet .Paste Destination:=ws.Range(cel)

    End Sub

    Was this answer helpful?

    0 comments No comments