I'm using GetOpenFilename to load a source file date to my target file - and it works, but I want to bring over some formatting also (in this case, text color).
My source cells have any of four font colors, but I'm passing only the cell Value (line in blue below in sample code), so it's only showing up in the target file as black font.
Is there a Range property or method that will also pass along the cell's format?
Code:
Private Sub CommandButton3_Click()
Dim sourceBook As Workbook
Dim filter As String
Dim caption As String
Dim sourceFilename As String
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
' get the source workbook
filter = "Excel files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
sourceFilename = Application.GetOpenFilename(filter, , caption)
Set sourceWorkbook = Application.Workbooks.Open(sourceFilename)
' copy data from source to target workbook
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Set sourceSheet = sourceWorkbook.Worksheets(1)
targetSheet.Range("B6", "D45").Value = sourceSheet.Range("A2", "C45").Value
' Close source workbook
sourceWorkbook.Close
End Sub
My source cells have any of four font colors, but I'm passing only the cell Value (line in blue below in sample code), so it's only showing up in the target file as black font.
Is there a Range property or method that will also pass along the cell's format?
Code:
Private Sub CommandButton3_Click()
Dim sourceBook As Workbook
Dim filter As String
Dim caption As String
Dim sourceFilename As String
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Set targetWorkbook = Application.ActiveWorkbook
' get the source workbook
filter = "Excel files (*.xlsx),*.xlsx"
caption = "Please Select an input file "
sourceFilename = Application.GetOpenFilename(filter, , caption)
Set sourceWorkbook = Application.Workbooks.Open(sourceFilename)
' copy data from source to target workbook
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet
Set targetSheet = targetWorkbook.Worksheets(1)
Set sourceSheet = sourceWorkbook.Worksheets(1)
targetSheet.Range("B6", "D45").Value = sourceSheet.Range("A2", "C45").Value
' Close source workbook
sourceWorkbook.Close
End Sub