programing

특정 단어를 사용하여 엑셀의 한 줄을 복사하고 다른 엑셀 시트에 붙여넣는 방법은 무엇입니까?

mbctv 2023. 6. 15. 23:16
반응형

특정 단어를 사용하여 엑셀의 한 줄을 복사하고 다른 엑셀 시트에 붙여넣는 방법은 무엇입니까?

여러 게시물을 확인했는데 제가 찾고 있는 정확한 코드를 찾을 수 없는 것 같습니다.또한 저는 VBA를 사용해 본 적이 없어서 다른 게시물의 코드를 가져와서 작동하기 위해 제 정보를 입력하려고 합니다.아직 운이 없습니다.직장에서는 엑셀로 급여 체계를 갖추고 있습니다.제 이름을 검색하려고 합니다."Clarke, Matthew"그런 다음 해당 행을 복사하여 바탕 화면에 저장한 워크북에 붙여넣습니다."Total hours".

코드

Sub Sample()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim copyFrom As Range
    Dim lRow As Long '<~~ Not Integer. Might give you error in higher versions of excel
    Dim strSearch As String
    
    Set wb1 = ThisWorkbook
    Set ws1 = wb1.Worksheets("yourSheetName")
    
    strSearch = "Clarke, Matthew"
    
    With ws1

        '~~> Remove any filters
        .AutoFilterMode = False

        '~~> I am assuming that the names are in Col A
        '~~> if not then change A below to whatever column letter
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
       
        With .Range("A1:A" & lRow)
            .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
            Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
        End With
        
        '~~> Remove any filters
        .AutoFilterMode = False
    End With
    
    '~~> Destination File
    Set wb2 = Application.Workbooks.Open("C:\Sample.xlsx")
    Set ws2 = wb2.Worksheets("Sheet1")
    
    With ws2
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            lRow = .Cells.Find(What:="*", _
                          After:=.Range("A1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
        Else
            lRow = 1
        End If
    
        copyFrom.Copy .Rows(lRow)
    End With
    
    wb2.Save
    wb2.Close
End Sub

스냅샷

enter image description here

timrau가 그의 의견에서 말한 것을 확장하면, 당신은 AutoFilter 기능을 사용하여 당신의 이름이 있는 행을 찾을 수 있습니다. (원본 워크북이 열려 있다고 가정합니다.)

Dim curBook As Workbook
Dim targetBook As Workbook
Dim curSheet As Worksheet
Dim targetSheet As Worksheet
Dim lastRow As Integer

Set curBook = ActiveWorkbook
Set curSheet = curBook.Worksheets("yourSheetName")

'change the Field number to the correct column
curSheet.Cells.AutoFilter Field:=1, Criteria1:="Clarke, Matthew" 

'The Offset is to remove the header row from the copy
curSheet.AutoFilter.Range.Offset(1).Copy  
curSheet.ShowAllData 

Set targetBook = Application.Workbooks.Open "PathTo Total Hours"
Set targetSheet = targetBook.WorkSheet("DestinationSheet")

lastRow = Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

targetSheet.Cells(lastRow + 1, 1).PasteSpecial

targetBook.Save
targetBook.Close 

보시다시피 워크북의 특정 설정을 위해 자리 표시자를 배치했습니다.

이것이 오래된 것이라는 것은 알지만, 이 방법을 찾는 다른 사람들에게는 훨씬 더 직접적인 방법으로 수행될 수 있습니다.

Public Sub ExportRow()
    Dim v
    Const KEY = "Clarke, Matthew"
    Const WS = "Sheet1"
    Const OUTPUT = "c:\totalhours.xlsx"
    Const OUTPUT_WS = "Sheet1"

    v = ThisWorkbook.Sheets(WS).Evaluate("index(a:xfd,match(""" & KEY & """,a:a,),)")
    With Workbooks.Open(OUTPUT).Sheets(OUTPUT_WS)
        .[1:1].Offset(.[counta(a:a)]) = v
        .Parent.Save: .Parent.Close
    End With
End Sub

언급URL : https://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to-another-excel-s

반응형