VBA help to transfer additional data to ws

steve400243

Active Member
Joined
Sep 15, 2016
Messages
429
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello Forum, I use this code to copy data over from ws2 to ws1 when cell C3 is updated.
From ws2 I need the data in column "H" (10) to copy to ws1 column "N" (13) and I cannot figure out how to make this addition. I tried adding in "sn(i, 13) = arr(x, 10)" But this does not work. Thank you for all help, or advise.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" Then
    Range("A13", "H34").ClearContents
    Dim ws1 As Worksheet, ws2 As Worksheet, lr As Long, arr As Variant, sn As Variant
    Set ws1 = Sheets("CFS SHEET")
    Set ws2 = Sheets("DATA")
    lr = ws2.Range("A" & Rows.Count).End(xlUp).Row
    arr = ws2.Range("A2", "I" & lr)
    ReDim sn(UBound(arr), 8)
    i = 0
    For x = 1 To UBound(arr)
        If arr(x, 1) = Range("C3") Then
            sn(i, 0) = arr(x, 2)
            sn(i, 1) = arr(x, 3)
            sn(i, 2) = arr(x, 4)
            sn(i, 3) = arr(x, 5)
            sn(i, 4) = arr(x, 6)
            sn(i, 5) = arr(x, 7)
            sn(i, 6) = arr(x, 8)
            sn(i, 7) = arr(x, 9)
            
            i = i + 1
        End If
    Next
    If i > 0 Then ws1.Range("A13").Resize(i, 8) = sn
End If
End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Col H is col 8 & col N is 14, so do you want cols H to N or J to M?
 
Upvote 0
Ok, how about
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$3" Then
    Range("A13", "H34").ClearContents
    Dim ws1 As Worksheet, ws2 As Worksheet, lr As Long, arr As Variant, sn As Variant
    Set ws1 = Sheets("Data")
    Set ws2 = Sheets("pcode")
    lr = ws2.Range("A" & Rows.Count).End(xlUp).Row
    arr = ws2.Range("A2:J" & lr)
    ReDim sn(UBound(arr), 14)
    i = 0
    For x = 1 To UBound(arr)
        If arr(x, 1) = Range("C3") Then
            sn(i, 0) = arr(x, 2)
            sn(i, 1) = arr(x, 3)
            sn(i, 2) = arr(x, 4)
            sn(i, 3) = arr(x, 5)
            sn(i, 4) = arr(x, 6)
            sn(i, 5) = arr(x, 7)
            sn(i, 6) = arr(x, 8)
            sn(i, 7) = arr(x, 9)
            sn(i, 13) = arr(x, 10)
            i = i + 1
        End If
    Next
    If i > 0 Then ws1.Range("A13").Resize(i, 14) = sn
End If
End Sub
 
Upvote 0
Thank you Fluff I appreciate your help! That did it, after I changed the ws1, and ws2 names back to the correct.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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