简体   繁体   中英

Excel VBA code to search cells for a string, report the row, and replace row with provided material

I am a bit stuck and hoping some of you can help me. It's been about 5 years since I coded in C++, so I am a bit rusty and have never touched VBA before.

I am looking to batch replace a row of an excel workbook that contains a number (but might be text). In column B, there is a part number. The user should be able to put the part number in a field of the workbook containing the macro, as well as paste in the entire row (~30 columns worth of data) that needs to be inserted.

Example: Part Number: 123456 Replaced with: x 123457 Widget, Top 5 x 9 x 10 Plastic

The macro should run through all of the workbooks in a folder, search for the part number in column B (this part number is not always on the first sheet, sometimes it is on the second or third sheet) and replace the entire row with the row specified in the macro workbook. The row being copied and pasted will fit the destination formatting perfectly.

The workbook needs to be saved, closed, and the next operated on until all are complete.

I can iterate through the workbooks fine, but I am not sure how to search and return a specific row or how to copy paste the entire row from one workbook to another.

It would also be useful to record each file name that has been changed in the master workbook, but this is not a huge deal. I can grab it from a cell in the workbook.

I appreciate any help I can get, I am very lost with VBA. -Sean

Edit: Here is my code:

Sub BatchReplace()
'For batch replacement of part number in ML's
'Edit with part to be replaced and with replacement row for ML
'Place subject MLs in "BATCH EDIT" folder.
'search for workbooks
'open first of x workbooks
'count sheets in workbook
'search for OldPN
'if found, return row of OldPN
'paste New Part in row
'continue search to B100
'search next sheet until sheet==sheetCount
'save
'
Dim strOldPN As String
Dim intRow As Integer
Dim rngNewPart As Range
Dim rngOldPN As Range
Dim wbMaster As Workbook, wbOp As Workbook
Dim i As Integer
Dim j As Integer

Set wbMaster = ThisWorkbook
Let strOldPN = wbMaster.Sheets("Sheet1").Cells(3, "E").Value 'Old part to search for
Set rngNewPart = wbMaster.Sheets("Sheet1").Range("A8,AE8") 'Copies new part row to replace

Application.ScreenUpdating = True
Application.DisplayAlerts = True

MsgBox "Part Number to replace is " & strOldPN 'Display part number to be replaced

With Application.FileSearch
.NewSearch
.LookIn = "C:\ExcelBatchReplace\BATCHEDIT\" 'put files in this folder to change
.SearchSubFolders = True
.Filename = ".xls" 'all files ending in xls
.FileType = msoFileTypeExcelWorkbooks

If .Execute() > 0 Then
    MsgBox "There were " & .FoundFiles.Count & " file(s) found." 'Displays number of documents in folder

Dim i As Integer

For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i), 0

'Do
'activeWorkbook, sheet1
'search Column B8:B100 for strOldPN
'intRow = Application.WorksheetFunction.Match(strOldPN, Range("B1:B100"), 0) 'Will this work?
        'if found
            'replace row with rngNewPart
            'Continue search through B100
        'else
            'Sheet++
 'While Sheet <= 4


ActiveWorkbook.Save
ActiveWorkbook.Close
Next i
Else
MsgBox "There were no files found."
End If
End With


Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub    

My psuedocode is where I am stuck. I've tried a few different methods but can't really get it to do what I want (or anything).

I would like to avoid the pitfalls of Active Workbook and refer to them as wbMaster (where the macro is located) and wbOp (where I am operating) but I couldn't get the later part to cooperate.

You can use the Range.Find and the Application.Save method to achieve this. The MSDN Developer Reference for Excel will be helpful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM