I am trying to copy either single cells values or rows of cells from a source workbook to a target workbook.
The user will have three workbooks open:
- Dashboard workbook
- Source workbook
- Target workbook
The sub reads the user input in Dashboard workbook, which will look like the following:
Source cells Target cells Cell/Row
G28 H30 Cell
G29 H31 Row
The sub is then supposed to look up cell G28 in Source workbook and copy that into H30 in Target workbook. Likewise the sub is supposed to look up cell G29 in Source workbook and copy that cell and everything to the right into H31 in Target workbook.
I managed to copy single cell values. I have not been able to implement the functionality for the row type input.
I indicated below where the error is.
Sub transferSub()
Dim wbMain As Workbook: Set wbMain = ThisWorkbook
Dim wbMainDashboard As Worksheet: Set wbMainDashboard = wbMain.Worksheets("Dashboard")
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
'Definition of file path for source and target workbooks
sourceModel = wbMainDashboard.Range("FILE_SOURCE") 'Pull from dashboard input
targetModel = wbMainDashboard.Range("FILE_TARGET") 'Pull from dashboard input
'Source and target workbooks
Dim wbSource As Workbook: Set wbSource = Workbooks(sourceModel) 'Workbook already open
Dim wbTarget As Workbook: Set wbTarget = Workbooks(targetModel) 'Workbook already open
'Source and target worksheet
Dim wskpInput_source As Worksheet: Set wskpInput_source = wbSource.Worksheets("INPUT (kp)")
Dim wsSCEInput_source As Worksheet: Set wsSCEInput_source = wbSource.Worksheets("INPUT (SCE)")
'Source and target worksheet
Dim wskpInput_target As Worksheet: Set wskpInput_target = wbTarget.Worksheets("INPUT (kp)")
Dim wsSCEInput_target As Worksheet: Set wsSCEInput_target = wbTarget.Worksheets("INPUT (SCE)")
'Procedures
Dim rng As Range: Set rng = wbMainDashboard.Range("Dashboard!E9:E15")
Dim i As Integer
For i = 1 To rng.Rows.Count
cell_source = rng.Cells(i, 1)
cell_target = rng.Cells(i, 1).Offset(0, 1)
cell_cellrow = rng.Cells(i, 1).Offset(0, 3)
If cell_cellrow = "Cell" Then 'If cell then copy paste value in that cell
wskpInput_target.Range(cell_target) = wskpInput_source.Range(cell_source).Value
ElseIf cell_cellrow = "Row" Then 'If row then copy and paste the row of cells
wskpInput_source.Range(cell_source, cell_source.End(xlToRight)).Copy _
wskpInput_target.Range(cell_target) '---NEED HELP WITH THIS PART---
End If
Next
End Sub
Option Explicit
to the top of the module. It looks likecell_source
is aString
- and you can't call.End
on aString
- i.e.cell_source.End(xlToRight)
. – BigBencell_source
(etc) are strings (addresses) then you probably needrange(cell_source).End(xlToRight)
. – SJRwbMainDashboard.Range("Dashboard!E9:E15")
is a little redundant. Since you already declaredwbMainDashboard
and are correctly qualifying theRange()
with that worksheet, you could just dowbMainDashboard.Range("E9:E15")
– BruceWayne