Macro to copy from one sheet to another with options on which column to copy and which column to paste to and more

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

This seamed easy when I thought it up but is beyond me so please help if you can.

I have a document called "This Hours"

in it I have tabs that are named after the week they represent like "21 November 2017" etc.

every week I send over to my guys a simple excel doc with the names of clients on it and they send back the hours they spent with each client.

I then manually copy this into my "This Hours" Document for the week it represents.

I was thinking of getting a macro to do this for me but need help.

this is what I need.

assumptions are: the only excel documents open are "This Hours" and the one I want to copy from that I wont know the name of?
Both document will be on the sheet I want the data taken from and copied to.

Now heres the problems,
first I will need a way to identify which column I'm coping from and to, I would think a pop up box saying "please enter From Column" and "please enter To Column" with a way to input the column letters would be best.

then once it knows which column to copy from it cant just copy every row, it needs to look down that column until it finds a cell that is not empty then look at the name in Column C
goto the "This hours" doc look down column C to find the name and paste the data into that cell of the column I suggested?

table below to help show what I mean
Message BoxReply Area
Please Enter From ColumnG
Please Enter To ColumnE

<tbody>
</tbody>






So I input into the reply area,
the macro then find the other open workbook selects it (as I say I don't know its name)
on the sheets that's open Looks down Column G for data

A
B
C
D
E
F
G
H
I
J
K
L
1
HEADINGS
2
MORE HEADINGS
3
MORE HEADINGS
4
MORE HEADINS
Client
5
STARTS FROM HERE!
Woolys
12
6
Henley
11
7
Boots
14
16
8
Clarks
15
9
Ravens
10
superdrug
11
Toy R Us

11
12
Tv Store
11
13
Currys
12
14
Maplin
11
15

Palins
11
1
16
Sams
17
Tents
11
18
19

<tbody>
</tbody>
So as you can see we have 3 lots of data in column G that needs the be copied
So It then goes back to "This Hours" document current sheet,
Looks Down column C and finds "Boots" and adds the data to Column "E" of that row and does the same for the reast.

Please help if you can

Thanks

Tony
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
It does sound like that task would be best handled by a macro. I'm sure this will need a little tweaking to get it working, but I think this gets us close.

This is designed to be run out of your PERSONAL.xlsb file when you have the submitted workbook open and active (the one your transferring hours from). This allows it to function without a hardcoded workbook name or separate function to pick the workbook. This also allows you to have other workbooks open.

Assumptions:
  • The This Hours workbook is open.
  • The client names in either workbook are unique without duplicates in their respective table.
  • Client names in the submission workbook are in column C.
  • The tables in both workbooks are in the first indexed worksheet of their workbook.
  • User always picks the correct columns (would require manual correction if an error is made; i.e. exit workbook without saving).
  • The cell the hours are being entered into is always empty (doesn't check for possible data loss).

Code:
Sub HoursTransfer()
    Dim oSubmitWks As Worksheet
    Dim oMasterWks As Worksheet
    Dim i As Integer
    Dim k As Integer
    Dim n As Integer
    Dim x As Integer
    Dim sClients As String
    Dim colFrom As String
    Dim colTo As String
    Dim finalRowSubmit As Integer
    
    Set oSubmitWks = ActiveWorkbook.Sheets(1)
    Set oMasterWks = Workbooks("This Hours.xlsx").Sheets(1)
    finalRowSubmit = Cells(Rows.Count, 3).End(xlUp).Row
    
    colFrom = InputBox("Please enter the column letter of the column you " _
        & "wish to transfer FROM.", "Select the FROM column", vbOKCancel)
    If colFrom = "" Then Exit Sub
    
    colTo = InputBox("Please enter the column letter of the column you wish " _
        & "to transfer TO.", "Select the TO column", vbOKCancel)
    If colTo = "" Then Exit Sub
        
    For i = 5 To finalRowSubmit
        If Range(colFrom & i).Value <> "" Then
            On Error GoTo MissingClient
            x = oMasterWks.Columns(1).Find(What:=oSubmitWks.Cells(i, 3).Value, After:=oMasterWks.Cells(1, 1), _
                LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=True).Row
            On Error GoTo 0
            
            oMasterWks.Range(colTo & x).Value = oSubmitWks.Range(colFrom & i)
            n = n + 1
        End If
NextClient:
    Next i
    
    MsgBox "The import has completed." & vbCr & vbCr & n & " clients have had their hours updated." _
        & IIf(k > 0, vbCr & k & " clients were not found on the This Hours workbook:" & sClients, "")
        
    Exit Sub
MissingClient:
    k = k + 1
    sClients = sClients & vbCr & vbTab & oSubmitWks.Cells(i, 3).Value
    Resume NextClient
End Sub

I threw a little extra in there to inform the user if some of the clients weren't found in the main workbook. Give this a whirl and let me know if we need to adjust anywhere.
 
Upvote 0
Hi AFPathfinder,
This is a great bit of code,
Its not a problem that I have to tweak it a little as I can do that no problem but if it works as I read it I think I will be fine.

Thank you so much for your help

Tony
 
Upvote 0

Forum statistics

Threads
1,215,133
Messages
6,123,231
Members
449,091
Latest member
jeremy_bp001

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