VBA Excel - Copy Data to Sheet (pre-check if not empty)

BukiSP84

New Member
Joined
Sep 9, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I am trying to create a cash flow workbook. Currently, I am working on a projections part of my idea.
Basically, the user must plan ahead funds to be spent in the future.

My workbook has the following sheets:

Cash Flow - in this sheet, Range (G18:L41) the user enters projections (in green cells). In cell P10 there is an installment picker. Selected installment affects G15:L15 values (months).
The idea is for a user to enter projections for a selected month, then click on Copy CF Data.
CPP Raw - here is where data is stored from the Cash Flow sheet. The user can retrieve data from CPP Raw by clicking on Retrieve CF Data in the Cash Flow worksheet.
ParCF - this is a parameters worksheet, where I defined named ranges, etc.

Here is an example of the idea:

1. the user creates the projection for Sep21. Then stores data in CPP Raw by clicking on Copy CF Data.
2. the user wants to change the projections. To do so, the user clicks on Retrieve CF Data.
3. the user gets old values. Replaces them with the new.
4. the user clicks on Copy CF Data again.

I have a problem with my code, as follows:

VBA Code:
Sub CF_cpData()
    Dim rng As Range, trgtCell As Range, src As Worksheet, trgt As Worksheet
    Set src = Worksheets("Cash Flow")
    Set trgt = Worksheets("CPP Raw")
    Application.ScreenUpdating = False
    With src
        For Each rng In src.Range("G15:L15")
            Set trgtCell = trgt.Rows(1).Find(rng.Value, LookIn:=xlValues, lookat:=xlWhole)
            If Not trgtCell Is Nothing Then
                .Range(rng.Offset(1), .Cells(.Rows.Count, rng.Column).End(xlUp)).Copy
                With trgt
'Here is where I need the code:
                    .Range(Split(trgtCell.Address, "$")(1) & .Cells(.Rows.Count, trgtCell.Column).End(xlUp).Row + 1).PasteSpecial xlPasteValues
                End With
            End If
        Next rng
    End With
    Application.ScreenUpdating = True
End Sub

My code works fine for the first-time entry of data.

For later changes, my code keeps adding the data to the subsequent rows. Basically, I want to replace the data, not to continue adding rows.

Here is the file (it has multiple worksheets within the workbook):
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,223,714
Messages
6,174,050
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top