+ Reply to Thread
Results 1 to 10 of 10

Macro to copy row to specific sheet in closed workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    07-03-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Macro to copy row to specific sheet in closed workbook

    I have a spreadsheet with values in columns A-F.

    In column A is a closed workbook name/directory path.
    In column B is the sheet name within the workbook specified in column A.

    I need a macro which will run with a SUBMIT button in column G which will write all row values in columns C-F to a new row in the worksheet specified by A and B. Any further additions should be added to the next available row in the specific worksheet without overwriting previous data.

    Thanks
    Phill

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro to copy row to specific sheet in closed workbook

    Hi,

    is it allowed to open the workbook with vba and then close it again or is it used by another user?
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    07-03-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to copy row to specific sheet in closed workbook

    Thank you for the reply. The workbook can be opened and closed (if this is done immediately), but preferably it would be done without opening and closing.
    Also is there any way of deleting the row from the original sheet once it has been copied to the new sheet so that fresh data can be added?

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro to copy row to specific sheet in closed workbook

    Hi,

    try these 2 files: Source.xlsm Target.xlsx

    source contains:
    Option Explicit
    
    Private Sub cmdSubmit_Click()
        Dim xlWsActv As Worksheet, xlWsTrgt As Worksheet
        Dim xlRng As Range
        Dim aData
        Dim sAddr As String
        Dim i As Long, lngStart As Long
        
        On Error GoTo cmdSubmit_Click_ErrorHandler
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
        End With
    
        lngStart = 8 'first row of data
    
        Set xlWsActv = ActiveSheet
        With xlWsActv
            aData = .Range(.Cells(lngStart, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 6)).Value
            Set xlWsTrgt = Application.Workbooks.Open(.Cells(1, 1).Value).Worksheets(.Cells(2, 1).Value)
        End With
        
        With xlWsTrgt
            For i = LBound(aData, 1) To UBound(aData, 1)
                If Not IsEmpty(aData(i, 1)) Then
                    Set xlRng = .Columns(1).Find(What:=aData(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                    If Not xlRng Is Nothing Then
                        sAddr = xlRng.Address
                        Do
                            If xlRng.Offset(, 1).Value = aData(i, 2) Then
                                xlRng.Offset(, 2).Resize(, 4).Value = _
                                        Array(aData(i, 3), aData(i, 4), aData(i, 5), aData(i, 6))
                                        
                                xlWsActv.Cells(i + lngStart - 1, 1).Resize(, 6).Clear
                                Exit Do
                            End If
                            Set xlRng = .Columns(1).FindNext(xlRng)
                            If xlRng Is Nothing Then Exit Do
                            If sAddr = xlRng.Address Then Exit Do
                        Loop
                    End If
                End If
            Next i
        End With
        
    cmdSubmit_Click_Proc_Exit:
        On Error GoTo 0
        If Not xlWsTrgt Is Nothing Then xlWsTrgt.Parent.Close True
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
            .DisplayAlerts = True
        End With
        Exit Sub
    cmdSubmit_Click_ErrorHandler:
        MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'cmdSubmit_Click' of VBA Document 'Sheet1'.", vbOKOnly + vbCritical, "Error"
        Resume cmdSubmit_Click_Proc_Exit
    End Sub

  5. #5
    Registered User
    Join Date
    07-03-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to copy row to specific sheet in closed workbook

    Thanks, but it doesn't seem to write to the Target workbook? I click submit, but the data disappears from the Source workbook, but isn't on the Target.

  6. #6
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro to copy row to specific sheet in closed workbook

    Hi,

    did you experience that with the sample files with nothing changed except the filepath in source A1? very strange

  7. #7
    Registered User
    Join Date
    07-03-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to copy row to specific sheet in closed workbook

    Yes, I didn't change anything other than the file path to suit my PC, click submit, it deleted data in Source, and Target was unchanged.

  8. #8
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro to copy row to specific sheet in closed workbook

    Remove the code from the source and use this instead, the cells will not be cleared and the target will not be closed, inform me about any pop-ups or errors/crashes. We will implement everything back if we find out were the problem is.

    Private Sub cmdSubmit_Click()
        Dim xlWsActv As Worksheet, xlWsTrgt As Worksheet
        Dim xlRng As Range
        Dim aData
        Dim sAddr As String
        Dim i As Long, lngStart As Long
    
        lngStart = 8 'first row of data
    
        Set xlWsActv = ActiveSheet
        With xlWsActv
            aData = .Range(.Cells(lngStart, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 6)).Value
            Set xlWsTrgt = Application.Workbooks.Open(.Cells(1, 1).Value).Worksheets(.Cells(2, 1).Value)
        End With
        
        With xlWsTrgt
            For i = LBound(aData, 1) To UBound(aData, 1)
                If Not IsEmpty(aData(i, 1)) Then
                    Set xlRng = .Columns(1).Find(What:=aData(i, 1), LookIn:=xlValues, lookat:=xlWhole)
                    If Not xlRng Is Nothing Then
                        sAddr = xlRng.Address
                        Do
                            If xlRng.Offset(, 1).Value = aData(i, 2) Then
                                xlRng.Offset(, 2).Resize(, 4).Value = _
                                        Array(aData(i, 3), aData(i, 4), aData(i, 5), aData(i, 6))
                                        
                            End If
                            Set xlRng = .Columns(1).FindNext(xlRng)
                            If xlRng Is Nothing Then Exit Do
                            If sAddr = xlRng.Address Then Exit Do
                        Loop
                    End If
                End If
            Next i
        End With
    End Sub

  9. #9
    Registered User
    Join Date
    07-03-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Macro to copy row to specific sheet in closed workbook

    Thanks for the update.

    Cells C3-F4 are selected in the target file (this was happening before also).

    The source rows aren't deleting, however the target file remains unchanged when the submit button is clicked. I also tried adding data to the source file in row 10, clicked submit, and again no change to the target file.

    If it's easier, we can limit the input of one row at a time before clicking submit. Ideally we would want the source row to be deleted once it's been transferred to target.

  10. #10
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Macro to copy row to specific sheet in closed workbook

    replace the if block with the following and run
                            If xlRng.Offset(, 1).Value = aData(i, 2) Then
    
                                xlRng.Offset(, 2).Value = aData(i, 3)
                                xlRng.Offset(, 3).Value = aData(i, 4)
                                xlRng.Offset(, 4).Value = aData(i, 5)
                                xlRng.Offset(, 5).Value = aData(i, 6)
                                MsgBox "ok"
                            End If
    we expect a messagebox to appear 2 times

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1