I've figured out most of this myself, but the part that is hanging me up is being able to paste the conditional formatting.
I want to be able to have a workbook open, run a VBA script to open another workbook, copy a range from it, then paste that to the original workbook.
The most success I've had is recording the macro and make this happen: With the original, target workbook open...
- Open the source workbook
- Copy range A1:X105
- Close source workbook
- Paste into worksheet titled "Temp" in target workbook
The problem is that the source workbook contains conditional formatting, and if the source workbook is closed before you paste into the target workbook, the conditional formatting isn't being pasted.
So either I need to find a way to paste the data with the conditional formatting, or I need to be able to switch back to the target workbook before closing the source workbook. This is a process that is going to need to be ran multiple times with different target workbooks, so the VBA code can't refer to a workbook filename for the target. The source workbook will always have the same path though.
Searching the site, I could only find solutions that specified the path for both workbooks.
This is what I have right now:
Sub CopyData()
Application.DisplayAlerts = False
Workbooks.Open filename:="source.xlsx", _
UpdateLinks:=3
Range("A1:X105").Select
Selection.Copy
ActiveWindow.Close
Sheets("Temp").Select
ActiveSheet.Paste
Application.DisplayAlerts = True
End Sub
I suppose what I need to implement into this is to declare the target workbook as a variable. Can someone help with that?
Sub CopyData() Application.DisplayAlerts = False Workbooks.Open filename:="source.xlsx", _ UpdateLinks:=3 Range("A1:X105").Select Selection.Copy ActiveWindow.Close Sheets("Temp").Select ActiveSheet.Paste Application.DisplayAlerts = True End Sub
– RobbyFileDialog
prompt and programmatically open the workbooks in question... :) – David Zemens