Search code examples
excelvbaworksheet

How Can I link the codes to Command Button?


I have a some part of the codes of a macro it's working below changing of the cell value. But I want to replace them as linking a command button + getting data from a closed workbook. Can someone help me about re-edit them?

Thank you for help!

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Chck As Integer, Cnt As Integer
    Dim Save As String
    Dim Subjt As Integer
    
    If Not Intersect(Range("A1"), Target) Is Nothing And Not Target = "" Then
        With Workbooks("Data2.xlsm").Worksheets("Datas")
            Application.EnableEvents = False
            Worksheets("Sheet1").Cells.Clear
            For Chck = 2 To .Cells(Rows.Count, "C").End(xlUp).Row
                Select Case .Cells(Chck, "C")
                    Case "Number"
                        Subjt = Chck
                    Case ""
                        If Save <> "" Then
                            Save = "C" & Subjt & ":Q" & Subjt & Save
                            .Range(Save).Copy
                            Cnt = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
                            If Cnt > 1 Then Cnt = Cnt + 2
                            Worksheets("Sheet1").Range("A" & Cnt).PasteSpecial
                            Save = ""
                        End If
                    Case Target
                        If .Cells(Chck, "B") = "Number" Then Save = Save & ", C" & Chck & ":Q" & Chck
                End Select
            Next
            Application.EnableEvents = True
        End With
    End If

End Sub

Solution

  • Try this in a regular module:

    EDIT: made a few fixes

    Sub CopyDataValues()
    
        Dim Chck As Long, Cnt As Long
        Dim Save As String
        Dim Subjt As Long, valA1
        Dim ws1 As Worksheet, wsData As Worksheet
        
        Set ws1 = ThisWorkbook.Worksheets("Sheet1")
        Set wsData = Workbooks("Data2.xlsm").Worksheets("Datas")
        
        Cnt = ws1.Range("A" & Rows.Count).End(xlUp).Row
        Cnt = Cnt + 2
        
        valA1 = ws1.Range("A1").Value
        
        If Len(valA1) > 0 Then
            With wsData
                Application.EnableEvents = False
                'ws1.Cells.Clear
                For Chck = 2 To .Cells(Rows.Count, "C").End(xlUp).Row
                    Select Case .Cells(Chck, "C")
                        Case "NUMBER"
                            Subjt = Chck
                        Case ""
                            If Save <> "" Then
                                Save = "C" & Subjt & ":Q" & Subjt & Save
                                .Range(Save).Copy ws1.Range("A" & Cnt)
                                Cnt = Cnt + 2
                                Save = ""
                            End If
                        Case valA1
                            If .Cells(Chck, "B") = "REAL" Then
                                Debug.Print "matched " & valA1 & " on row " & Chck
                                Save = Save & ", C" & Chck & ":Q" & Chck
                            End If
                    End Select
                Next
                Application.EnableEvents = True
            End With
        End If
    
    End Sub