SO close, yet so far -Macro to move balance data from wksht to another wksht-

wmehar

New Member
Joined
Jan 21, 2016
Messages
8
Thank you in advance for your help. I have two worksheets, one with census data (Sheet1) and another with account balance data (Table 1). I'm trying to come up with a code that transfers the individuals account balances between the sheets.

I'm able to get the code to match up names with the chart, check which account source ("Profit Sharing" in this example) and to bring the data where I want it. However, the only problem is that it pastes every iteration of every persons balance in each cell where they need to go, ultimately with the last balance pasted uniformly into Sheet1.

For example:

John: pastes Johns, Bills, Dillans, then pastes Jerry's Profit Sharing data in cell cell P1
Bill: pastes Johns, Bills, Dillans, then pastes jerry's Profit Sharing data in cell P2
.
.
Jerry: pastes Johns, Bills, Dills, then pastes Jerry's Profit Sharing data in cell P4.

The thing is, I don't want to change the coding I currently have too much, because I'm setting this up for difference census's with different reports, and would just like to automate doing this for 100's of people across different sheets with the same sheet names. So things need to be dynamic. Again I appreciate any kind of help on this.

I plan extrapolating so it wouldn't be just Profit Sharing, but 401k, Roth 401k, etc.

Here's my current code:

Sub ProfitSharing()


Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer


finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row


For Each rng In Sheets("Sheet1").Range("A1:A100")
For Each rng2 In Sheets("Table 1").Range("A1:A100")


If rng = rng2 Then


For i = 4 To finalrow


Sheets("Table 1").Range("A" & i).Select


If ActiveCell = "Profit Sharing" Then


ActiveCell.Offset(0, 6).Copy


rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats


End If


Next


End If


Next


Next


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Here's my current code:

Sub ProfitSharing()


Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer


finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row


For Each rng In Sheets("Sheet1").Range("A1:A100")
For Each rng2 In Sheets("Table 1").Range("A1:A100")


If rng = rng2 Then


For i = 4 To finalrow


Sheets("Table 1").Range("A" & i).Select


If ActiveCell = "Profit Sharing" Then


ActiveCell.Offset(0, 6).Copy


rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats


End If


Next


End If


Next


Next


End Sub

Btw, the code line that says : Sheets("Table 1").Range("A" & i).Select

Is there anyway I can use this without having the sheet "Table 1" being the active sheet?

Thanks
 
Upvote 0
wmehar,

It would help if you could post some sample data.

And oh by the way, when posting code, please use code tags...

Code:
Sub ProfitSharing()

Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer

finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row

For Each rng In Sheets("Sheet1").Range("A1:A100")
    For Each rng2 In Sheets("Table 1").Range("A1:A100")
        If rng = rng2 Then
            For i = 4 To finalrow
                Sheets("Table 1").Range("A" & i).Select
                If ActiveCell = "Profit Sharing" Then
                    ActiveCell.Offset(0, 6).Copy
                    rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats
                End If
            Next
        End If
    Next
Next

End Sub

Cheers,

tonyyy
 
Upvote 0
wmehar,

It would help if you could post some sample data.

And oh by the way, when posting code, please use code tags...

Code:
Sub ProfitSharing()

Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer

finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row

For Each rng In Sheets("Sheet1").Range("A1:A100")
    For Each rng2 In Sheets("Table 1").Range("A1:A100")
        If rng = rng2 Then
            For i = 4 To finalrow
                Sheets("Table 1").Range("A" & i).Select
                If ActiveCell = "Profit Sharing" Then
                    ActiveCell.Offset(0, 6).Copy
                    rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats
                End If
            Next
        End If
    Next
Next

End Sub

Cheers,

tonyyy

Sorry about that, thanks for the tip, I'm far behind the times. (never posted on forums or used them before). I'll try and post some sample data
 
Upvote 0
[TABLE="width: 1168"]
<colgroup><col><col><col span="2"><col span="2"><col><col><col><col span="2"><col></colgroup><tbody>[TR]
[TD]HESTER, JEREMY[/TD]
[TD="align: right"][/TD]
[TD]JEREMY[/TD]
[TD]HESTER[/TD]
[TD="align: right"][/TD]
[TD="align: right"] [/TD]
[TD]M[/TD]
[TD]K[/TD]
[TD]N[/TD]
[TD="align: right"]$1,958.30[/TD]
[TD="align: right"]$0.00[/TD]
[TD="align: right"]0.0000[/TD]
[/TR]
[TR]
[TD]HESTER, KEVIN[/TD]
[TD="align: right"][/TD]
[TD]KEVIN[/TD]
[TD]HESTER[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD]M[/TD]
[TD]K[/TD]
[TD]H[/TD]
[TD="align: right"]$86,526.31[/TD]
[TD="align: right"]$136,455.35[/TD]
[TD="align: right"]0.0000[/TD]
[/TR]
[TR]
[TD]ALIZADEH, RACHAEL[/TD]
[TD="align: right"][/TD]
[TD]RACHAEL[/TD]
[TD]ALIZADEH[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD]F[/TD]
[TD]N[/TD]
[TD]N[/TD]
[TD="align: right"]$17,337.50[/TD]
[TD="align: right"]$0.00[/TD]
[TD="align: right"]0.0000[/TD]
[/TR]
[TR]
[TD]BAYHAM, MICHELLE[/TD]
[TD="align: right"][/TD]
[TD]MICHELLE[/TD]
[TD]BAYHAM[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD]F[/TD]
[TD]N[/TD]
[TD]N[/TD]
[TD="align: right"]$50,962.00[/TD]
[TD="align: right"]$0.00[/TD]
[TD="align: right"]0.0000[/TD]
[/TR]
[TR]
[TD]BECNEL, TESSA[/TD]
[TD="align: right"][/TD]
[TD]TESSA[/TD]
[TD]BECNEL[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD]F[/TD]
[TD]N[/TD]
[TD]N[/TD]
[TD="align: right"]$0.00[/TD]
[TD="align: right"]$0.00[/TD]
[TD="align: right"]0.0000[/TD]
[/TR]
</tbody>[/TABLE]


Above is the table where I'd like to have the account balance data moved, to the far right column.


[TABLE="width: 750"]
<colgroup><col><col><col><col span="2"><col><col><col><col></colgroup><tbody>[TR]
[TD="colspan: 9"]KEVIN J. HESTER, D.D.S. PROFIT SHARING PLAN VALUATION AS OF 12/31/2014
Summary Of Accounts[/TD]
[/TR]
[TR]
[TD]Account Description[/TD]
[TD]Prior
Account Balance[/TD]
[TD]Transfers Withd(-)[/TD]
[TD]Earnings Losses(-)[/TD]
[TD]Forfeiture[/TD]
[TD]Current Contrib[/TD]
[TD]Current
Account Balance[/TD]
[TD]Pct Vested[/TD]
[TD]Vested
Account Balance[/TD]
[/TR]
[TR]
[TD]HESTER, KEVIN[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]PROFIT SHARING[/TD]
[TD]1,045,567.51[/TD]
[TD]0.00[/TD]
[TD]75,445.09[/TD]
[TD]0.00[/TD]
[TD]52,000.00[/TD]
[TD]1,173,012.60[/TD]
[TD]100.00[/TD]
[TD]1,173,012.60[/TD]
[/TR]
[TR]
[TD]Safe Harbor[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]200.00[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]401k Deferral[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]BAYHAM, MICHELLE[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]PROFIT SHARING[/TD]
[TD]37,873.28[/TD]
[TD]0.00[/TD]
[TD]2,732.82[/TD]
[TD]0.00[/TD]
[TD]2,548.10[/TD]
[TD]43,154.20[/TD]
[TD]100.00[/TD]
[TD]43,154.20[/TD]
[/TR]
</tbody>[/TABLE]


Above here is where I'm extracting the data. So far I have the code to match up by name, then to move numbers under the column "Current account balance" to the furthest right column in the prior table. After identifying if there's a match between the two, it would extract the account balance should it be "profit sharing" and paste it.

The key here, is that I have other spreadsheets similar to the 2nd table I've pasted by format, however the account balance names differ. It may say "PS" as opposed to "Profit Sharing". i have gone through and created a bank of every combination of spelling variations for all the account sources. So I plan on expanding the code to accommodate those spelling variations if possible. I hope this helps
 
Upvote 0
You might give this a try...

Code:
Sub ProfitSharing()

Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer
Application.ScreenUpdating = False

finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row

For Each rng In Sheets("Sheet1").Range("A1:A100")
    For Each rng2 In Sheets("Table 1").Range("A1:A100")
        If rng = rng2 Then
            For i = rng2.Offset(1, 0).Row To finalrow
                If UCase(Sheets("Table 1").Range("A" & i).Value) = UCase("Profit Sharing") Then
                    Sheets("Table 1").Range("A" & i).Offset(0, 6).Copy
                    rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats
                    Exit For
                End If
            Next
        End If
    Next
Next

Application.ScreenUpdating = True
End Sub

And yes, you can run this without "Table 1" being the active sheet.

Oh, I had to remove the merged cells to get the macro to run.

Cheers,

tonyyy
 
Last edited:
Upvote 0
You might give this a try...

Code:
Sub ProfitSharing()

Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer
Application.ScreenUpdating = False

finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row

For Each rng In Sheets("Sheet1").Range("A1:A100")
    For Each rng2 In Sheets("Table 1").Range("A1:A100")
        If rng = rng2 Then
            For i = rng2.Offset(1, 0).Row To finalrow
                If UCase(Sheets("Table 1").Range("A" & i).Value) = UCase("Profit Sharing") Then
                    Sheets("Table 1").Range("A" & i).Offset(0, 6).Copy
                    rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats
                    Exit For
                End If
            Next
        End If
    Next
Next

Application.ScreenUpdating = True
End Sub

And yes, you can run this without "Table 1" being the active sheet.

Oh, I had to remove the merged cells to get the macro to run.

Cheers,

tonyyy


Ahh man, it seems that there is one tiny problem though; When the code runs and pastes the information, for people who DON'T have an account source that the person below them has (For example, let's say Michelle Has Profit Sharing but Kevin does not, It would paste Michelle's profit sharing balance in Kevin's cell when he doesn't actually have a profit sharing balance.

The sample data was such that both individuals had "Profit Sharing" so this problem wouldn't occur. However the tables I'm going to use for reference will have individuals who may not have Profit Sharing.

(I used Option Compare Text to ignore upper/lowercase btw).

Any ideas to tweak it? I've tried throwing an ElseIf before the Exit For code, like this
Code:
Elseif (Sheets("Table 1").Range("A" & i).Value) <> ("Profit Sharing") Then rng.offset(0,17).value = 0[code]

but to no avail
 
Upvote 0
Code:
Sub ProfitSharing()



Sub testing()


Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer
Application.ScreenUpdating = False


finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row


For Each rng In Sheets("Sheet1").Range("A1:A100")
    For Each rng2 In Sheets("Table 1").Range("A1:A100")
        If rng = rng2 Then
            For i = rng2.Offset(1, 0).Row To finalrow
                If (Sheets("Table 1").Range("A" & i).Value) = ("Safe Harbor") Then
                    Sheets("Table 1").Range("A" & i).Offset(0, 6).Copy
                    rng.Offset(0, 17).PasteSpecial xlPasteFormulasAndNumberFormats
                        If (Sheets("Table 1").Range("A" & i).Value) <> ("Safe Harbor") Then
                        rng.Offset(0, 17).Value = 0
                        End If
                    Exit For
                End If
            Next
        End If
    Next
Next
Application.ScreenUpdating = True


End Sub

Here's what I tried, but the outcome is the same.
 
Upvote 0
Code:
[COLOR=#a9a9a9]Sub ProfitSharing()

Dim rng As Range
Dim rng2 As Range
Dim i As Integer
Dim finalrow As Integer
Application.ScreenUpdating = False

finalrow = Sheets("Table 1").Range("A100").End(xlUp).Row

For Each rng In Sheets("Sheet1").Range("A1:A10")
    For Each rng2 In Sheets("Table 1").Range("A1:A10")
        If rng = rng2 Then
            For i = rng2.Offset(1, 0).Row To finalrow
               [/COLOR] If InStr(Sheets("Table 1").Range("A" & i).Value, ",") > 0 Then
                    Exit For
                ElseIf UCase(Sheets("Table 1").Range("A" & i).Value) = UCase("Profit Sharing") Then[COLOR=#a9a9a9]
                    Sheets("Table 1").Range("A" & i).Offset(0, 6).Copy
                    rng.Offset(0, 15).PasteSpecial xlPasteFormulasAndNumberFormats
                    Exit For
                End If
            Next
        End If
    Next
Next

Application.ScreenUpdating = True
End Sub[/COLOR]

The code modification assumes only names will contain the comma (,) character. So, once it finds a name match the code will first determine if it encounters another name - and if so, exit the loop - otherwise continue looking for "Profit Sharing."
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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