Code Modification

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
This code was provided to me by this forum and it works perfect. I want to expand it, but I have tried lots of different mods to it unsuccessfully.

The goal is to allow any number of columns to be copied over to "Hstry" worksheet, based on the valve ("4"), column H on sheet "WO"

What controls the limit in the number of columns to be copied over to "Hstry" sheet or does it? The unbound (Data),1 to 8 is what I don't quite get.

Code:
Private Sub CommandButton1_Click()
Dim r As Long, X As Long, Data As Variant, Result As Variant
  Data = Range("A1", Cells(Rows.Count, "H").End(xlUp))
  ReDim Result(1 To UBound(Data), 1 To 8)
  
 X = Sheets("WO").Range("A" & Rows.Count).End(xlUp).Row

For r = 1 To UBound(Data)
    If Data(r, 8) = "4" Then ' This controls what column get copied
        X = X + 1
        With Sheets("Hstry")
            .Cells(X, 1).Value = Data(r, 2) 'Rpt-Chklst-Col.B to WO-Col.A
            .Cells(X, 2).Value = Data(r, 3) 'Rpt-Chklst-Col.C to WO-Col.B
            .Cells(X, 3).Value = Data(r, 4) 'Rpt-Chklst-Col.D to WO-Col.C
            .Cells(X, 8).Value = Data(r, 8) 'Rpt-Chklst-Col.H to WO-Col.H
        End With
    End If
Next r
MsgBox "Data Transfered"
End Sub
 
In that case try
Code:
Private Sub CommandButton1_Click()
Dim r As Long, X As Long, Data As Variant, Result As Variant
  Data = Range("A1", Cells(Rows.Count, "H").End(xlUp))
  ReDim Result(1 To UBound(Data), 1 To 8)
  
 X = Sheets("Hstry").Range("A" & Rows.Count).End(xlUp).Row

For r = 1 To UBound(Data)
    If Data(r, 8) = "4" Then ' This controls what column get copied
        X = X + 1
        With Sheets("Hstry")
            .Cells(X, 1).Resize(, 7).Value = Application.Index(Data, r, 0) 'Rpt-Chklst-Col.B to WO-Col.A
        End With
    End If
Next r
MsgBox "Data Transfered"
End Sub
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Fluff,
Its works perfect. What or where is the control of the number of columns copied?
Resize(, 7).Value - Is 7 the number of columns?

Thanks
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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