samedi 11 août 2018

Excel VBA copy cells from row with check box into another workbook

I have a workbook where a table starts at row 8. In the column to the right of the last table column in rows 8, 9, and 10 I have check boxes called CHK8, CHK9, and CHK10. Check boxes are hidden until a value is entered in C8, C9, and/or C10. A value in any of those cells makes the checkbox for that row visible. In row 7 I have a check box (called "chkAll") that functions to check off any checkbox that is visible. I want a code where if a row is checked off, the user can click a button (called "CommandButton5") and certain cells get copied and pasted into another workbook. In the code I have, "data sheet" is the column C and in the workbook I want to open, "data sheet" is column Q. I want any row that is checked off to transfer the values in column C to column Q. So far, this code opens the template file I have but does not transfer the values in C to Q.

'Below is a code that will make the check boxes visible if a value is entered in the column for data sheets.
'Check boxes have to prexist. The code below simply makes them visible or invisible depending on if there's a value in the data sheet column for that row. Right nowI have only created check boxes in rows 8, 9, and 10.
'"Target" is being defined by the range C8:C10 which is the column/range for the “data shee”t name.
'Each check box must be labeled "CHK" (without the quotations) followed by the row number that check box is in.
'If you want 150 check boxes to show starting from row 8, you need to label the first check box CHK8 in row 8, the next CHK9 in row 9, and so on and so forth until CHK158 in row 158.

Private Sub Worksheet_Change(ByVal Target As Range)

If Intersect(Target, Range("C8:C10")) Is Nothing Then Exit Sub

If Target.Value <> "" Then
    Me.Shapes("CHK" & Target.Row).Visible = True
'If there is a value for data sheet, check box is visible

Else
    Me.Shapes("CHK" & Target.Row).Visible = False
'If there is no value for data sheet, check box is invisible
End If

End Sub

'Below makes it so if the "Check All" button is click, all visible check boxes are checked off.
'The "Check All" button must be called "chkAll" (without the quotation marks).
Private Sub chkAll_click()
    Dim x As Long

    For x = 8 To 10
        If Me.ChkAll.Value = True Then
            Me.OLEObjects("CHK" & x).Object.Value = True

        Else: Me.ChkAll.Value = False
            Me.OLEObjects("CHK" & x).Object.Value = False
        End If
        Next x
End Sub


Private Sub CommandButton5_Click()

Dim CHK As Boolean
Dim WS As Worksheet
Dim wbTarget As Workbook
Dim wbThis As Workbook

Dim LastCellA As Range
Dim LastCellRowNumber As Long

Set searchWS = Worksheets("Ozone Generator Skid")
    With searchWS
        Set LastCellC = .Cells(.Rows.Count, "C").End(xlUp)
        LastCellRowNumber = Application.WorksheetFunction.Max(LastCellC.Row)
End With

Application.CutCopyMode = False
Set wbThis = ActiveWorkbook
Set wbTarget = Workbooks.Open("C:\Users\ssonbati\Documents\Manual Valve List Template.xlsx")
wbTarget.Activate
For i = 8 To LastCellRowNumber
    If CHK = True Then
        wbTarget.Worksheets("Manual Valves").Cells(i - 2, "Q").Value = wbThis.Worksheets("Ozone Generator Skid").Cells(i, "C").Value
    End If
Next i
End Sub




Aucun commentaire:

Enregistrer un commentaire