tweak existing code

Jaz05

New Member
Joined
May 11, 2005
Messages
37
Hi guru's,

I have a code, which does me well. Where "Data" column L has the GL account # 10346500 it takes some cell values from source sheet row and copies it into "OPLOSS" sheet. It copies the data from msSourceColumns to my Target sheet, but puts the data in columns A,B,C,D,E respectively.

I need to tweak the code so the target sheet "OPLOSS" values are in column U,M,W,C,D
in the last row (not A,B,C,D,E).

I've tried to play with it by creating Const msTargetColumns As String = ,U,M,W,C,Dand adding some additional Dim
but was failing at wsTo.Range
----------------------------------------------------------



Option Explicit
Const msEventColumn As String = "L"
Const msSourceColumns As String = ",A,D,E,L,E,"
Sub CopyData()
Dim iPtr As Integer
Dim lTargetRow As Long
Dim rCur As Range
Dim sFirstAddress As String
Dim saSourceCols() As String
Dim vaData() As Variant
Dim wsFrom As Worksheet, wsTo As Worksheet

Set wsFrom = Sheets("Data")
Set wsTo = Sheets("OPLOSS")
lTargetRow = wsTo.Cells(Rows.Count, "A").End(xlUp).Row

saSourceCols = Split(msSourceColumns, ",")
ReDim vaData(1 To 1, 1 To UBound(saSourceCols))
With wsFrom.Columns(msEventColumn)
Set rCur = .Find("10346500", LookIn:=xlValues)
If Not rCur Is Nothing Then
sFirstAddress = rCur.Address
Do
For iPtr = 1 To UBound(saSourceCols)
vaData(1, iPtr) = wsFrom.Cells(rCur.Row, saSourceCols(iPtr)).Value
Next iPtr
lTargetRow = lTargetRow + 1
wsTo.Range("A" & lTargetRow, Cells(lTargetRow, UBound(vaData, 2)).Address).Value = vaData
Set rCur = .FindNext(rCur)
If rCur Is Nothing Then Exit Do
Loop While rCur.Address <> sFirstAddress
End If
End With

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try something like this...

Wasn't sure how you wanted to determine the first target row on OPLOSS

Code:
Option Explicit
Sub CopyData()

    Dim iPtr   As Integer
    Dim lTargetRow As Long
    Dim rCur   As Range
    Dim sFirstAddress As String
    Dim saSourceCols As Variant
    Dim saTargetCols As Variant
    Dim wsFrom As Worksheet, wsTo As Worksheet
    
    Const msEventColumn As String = "L"
[COLOR="Blue"]    saSourceCols = Array("A", "D", "E", "L", "E")
    saTargetCols = Array("U", "M", "W", "C", "D")[/COLOR]
    Set wsFrom = Sheets("Data")
    Set wsTo = Sheets("OPLOSS")
    [COLOR="Red"]lTargetRow = wsTo.Cells(Rows.Count, "A").End(xlUp).Row + 1[/COLOR]
    
    Application.ScreenUpdating = False
    With wsFrom.Columns(msEventColumn)
        Set rCur = .Find("10346500", LookIn:=xlValues)
        If Not rCur Is Nothing Then
            sFirstAddress = rCur.Address
            Do
                For iPtr = LBound(saSourceCols) To UBound(saSourceCols)
                    wsTo.Range(saTargetCols(iPtr) & lTargetRow).Value = _
                        wsFrom.Range(saSourceCols(iPtr) & rCur.Row).Value
                Next iPtr
                lTargetRow = lTargetRow + 1
                Set rCur = .FindNext(rCur)
            Loop While rCur.Address <> sFirstAddress
        End If
    End With
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Thanks AlphaFrog.
Friday night here, so I'll test this Monday, but reading your additions, I think this will work.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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