Transfer Data based off a criteria to certain Column

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
893
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I need some help to transfer my data to specific columns on my destination sheet. The ability to modify it if it changes I have a line of code that transfers based off a criteria but now to specific columns adds a bit of complexity for me. See for example the below.


Source data - the amount of rows may change but always starts on row 11:
Book2
ABCDEFGHIJKLMNOPQ
1
2
3
4
5
6
7
8
9
10
11ConcatenateDateAccountIndicatorGroupCurrencyPriceUnit HoldersValueFactorAverageTotal AssetsTotal LiabilitiesRate 1Rate 1 %Rate 2Rate 2 %
12AAAF1/10/2025AAAF1USD$101000$1000.0001$10100$500.00510%0.09590%
Source


Destination - With a header starts on row 2. Should look like this:
Book2
ABCDEFGHIJKLMNOPQR
1DateMain AccountUnique IDIndicatorMain Account GroupCurrencyPriceUnit HoldersValueFactorPre PriceAverageTotal AssetsTotal LiabilitiesRate 1Rate 1 %Rate 2Rate 2 %
2AAAF1USD1010001000.000110100500.0050.10.0950.9
3
Destination


The data must transfer based off the criteria in column D = F. As there will also be other indicators of AZ on it that I do not want to bring over. Thanks in advance - any help is always appreciated. This is a mockup by actual data is over 300 rows if it matters.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
The ability to modify it if it changes I have a line of code that transfers based off a criteria but now to specific columns adds a bit of complexity for me. See for example the below.

Since some of the names in the headers are different, the simplest thing to do is to route the source column to the destination column.
Array 'a' is the source and array 'b' is the one that receives the data.
Review the code and if you have any questions ask me.

If the headers could be the same then the code could be shortened.

VBA Code:
Sub TransferData()
  Dim a As Variant, b As Variant
  Dim i As Long
  
  a = Sheets("Source").Range("A12:Q" & Sheets("Source").Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 18)
  
  For i = 1 To UBound(a, 1)
    If a(i, 4) = "F" Then
      b(i, 1) = a(i, 2)   'date
      b(i, 2) = a(i, 3)   'acc
      b(i, 3) = ""        'unique id
      b(i, 4) = a(i, 4)   'Indicator
      b(i, 5) = a(i, 5)   'group
      b(i, 6) = a(i, 6)   'currency
      b(i, 7) = a(i, 7)   'price
      b(i, 8) = a(i, 8)   'unit
      b(i, 9) = a(i, 9)   'value
      b(i, 10) = a(i, 10) 'factor
      b(i, 11) = ""       'pre price
      b(i, 12) = a(i, 11) 'average
      b(i, 13) = a(i, 12) 'tot assets
      b(i, 14) = a(i, 13) 'tot lia
      b(i, 15) = a(i, 14) 'rate 1
      b(i, 16) = a(i, 15) 'rate 1%
      b(i, 17) = a(i, 16) 'rate 2
      b(i, 18) = a(i, 17) 'rate 2%
    End If
  Next
  
  With Sheets("Destination")
    .Rows("2:" & Rows.Count).ClearContents
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
End Sub

😅
 
Upvote 0
Yeah I can't get the header names to match. One comes from a hard coded ledger systems so I can't control the source. And the destination is how its delivered to downstream consumers so I require it that way. So they are different unfortunately.

On the macro I noticed that the F criteria can be scattered. For example rows 11-100 could be F, and 101-200 AZ, then 201-300 again F. What I noticed is that second set 201-300 F go on the destination not after the next open row but the equivalent row from where it came from. So what I end up with is a block of like 100 rows blank then the 2nd set of F criteria appears. Any way to have it be on the destination all together?
 
Upvote 0
So what I end up with is a block of like 100 rows blank then the 2nd set of F criteria appears. Any way to have it be on the destination all together?
Try this:

VBA Code:
Sub TransferData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  a = Sheets("Source").Range("A12:Q" & Sheets("Source").Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 18)
  
  For i = 1 To UBound(a, 1)
    If a(i, 4) = "F" Then
      k = k + 1
      b(k, 1) = a(i, 2)   'date
      b(k, 2) = a(i, 3)   'acc
      b(k, 3) = ""        'unique id
      b(k, 4) = a(i, 4)   'Indicator
      b(k, 5) = a(i, 5)   'group
      b(k, 6) = a(i, 6)   'currency
      b(k, 7) = a(i, 7)   'price
      b(k, 8) = a(i, 8)   'unit
      b(k, 9) = a(i, 9)   'value
      b(k, 10) = a(i, 10) 'factor
      b(k, 11) = ""       'pre price
      b(k, 12) = a(i, 11) 'average
      b(k, 13) = a(i, 12) 'tot assets
      b(k, 14) = a(i, 13) 'tot lia
      b(k, 15) = a(i, 14) 'rate 1
      b(k, 16) = a(i, 15) 'rate 1%
      b(k, 17) = a(i, 16) 'rate 2
      b(k, 18) = a(i, 17) 'rate 2%
    End If
  Next
  
  With Sheets("Destination")
    .Rows("2:" & Rows.Count).ClearContents
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
End Sub

🫡
 
Upvote 0
While testing could there be a minor variation. If a cell value equals zero can it bring over "" and if not zero the actual record? For example on the source if there is a zero value I don't want zero coming over need the field to be blank only if there is a non zero record I want to see it. I noticed it as I was testing so sorry it didn't make the first iteration
 
Upvote 0
f a cell value equals zero can it bring over ""

Try:

VBA Code:
Sub TransferData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  
  a = Sheets("Source").Range("A12:Q" & Sheets("Source").Range("A" & Rows.Count).End(3).Row).Value
  ReDim b(1 To UBound(a, 1), 1 To 18)
  
  For i = 1 To UBound(a, 1)
    If a(i, 4) = "F" Then
      k = k + 1
      b(k, 1) = IIf(a(i, 2) = 0, "", a(i, 2))     'date
      b(k, 2) = IIf(a(i, 3) = 0, "", a(i, 3))     'acc
      b(k, 3) = ""                                'unique id
      b(k, 4) = IIf(a(i, 4) = 0, "", a(i, 4))     'Indicator
      b(k, 5) = a(i, 5)                           'group
      b(k, 6) = IIf(a(i, 6) = 0, "", a(i, 6))     'currency
      b(k, 7) = IIf(a(i, 7) = 0, "", a(i, 7))     'price
      b(k, 8) = IIf(a(i, 8) = 0, "", a(i, 8))     'unit
      b(k, 9) = IIf(a(i, 9) = 0, "", a(i, 9))     'value
      b(k, 10) = IIf(a(i, 10) = 0, "", a(i, 10))  'factor
      b(k, 11) = ""                               'pre price
      b(k, 12) = IIf(a(i, 11) = 0, "", a(i, 11))  'average
      b(k, 13) = IIf(a(i, 12) = 0, "", a(i, 12))  'tot assets
      b(k, 14) = IIf(a(i, 13) = 0, "", a(i, 13))  'tot lia
      b(k, 15) = IIf(a(i, 14) = 0, "", a(i, 14))  'rate 1
      b(k, 16) = IIf(a(i, 15) = 0, "", a(i, 15))  'rate 1%
      b(k, 17) = IIf(a(i, 16) = 0, "", a(i, 16))  'rate 2
      b(k, 18) = IIf(a(i, 17) = 0, "", a(i, 17))  'rate 2%
    End If
  Next
  
  With Sheets("Destination")
    .Rows("2:" & Rows.Count).ClearContents
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
  End With
End Sub

🫡
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,736
Members
453,369
Latest member
juliewar

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