VBA:如何find部门,并将其replace为另一个部门?

我很难find解决这个问题的方法。 我正在尝试findstringdepartment ,并将其replace为另一个部门。 每个工作簿中有16-10张需要格式化。 其中一种格式需要移动/复制第first department string (在某些行中伸出到columns B,C列中)并将其粘贴到last department所在的位置,但是第A列中某个工作表的第一个部门没有上面的部门它但是member number 。 原因是为了让这个部门成为一个头衔。

底部的图片应该提供更多的信息。 在这里输入图像说明

这是我迄今为止,但不符合。 我将不胜感激这方面的一些帮助。 请提出问题并提出build议。 谢谢。

  Sub format() Dim ws As Worksheet Dim frstDept As Long Dim lastDept As Long Dim rng As Long Dim n As Long Dim nlast As Long Dim rw As Range Dim i As Integer Dim iVal As Integer iVal = Application.WorksheetFunction.CountIf(Range("A1:A20000"), "Department") Debug.Print iVal Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows nlast = rw.Count For Each ws In Sheets On Error GoTo NextWS For i = iVal To 1 Step -1 With ws frstDept = Range("A:A").Find(What:="Department", LookIn:=xlValues, LookAt:=xlPart).Row lastDept = Range("A:A").Find(What:="Member Number", LookIn:=xlValues, LookAt:=xlPart).Row If .frstDept.Value = .lastDept.Value Then frstDept.Selection.Offset(0, 2).Cut Destination:=Range("lastDept" + 1).Resize(1) End If End If End With NextWS: Err.Clear Next ws End Sub 

我相信下面的代码将工作:

 Sub format() Dim ws As Worksheet Dim r As Long Dim lastDept As String Dim nextDept As String Dim rw As Long For Each ws In Worksheets ' Changed from "Sheets" to "Worksheets" to avoid ' problems if any "Charts" exist With ws rw = .Cells(.Rows.Count, "A").End(xlUp).Row 'Grab the details of the last "Department" and then clear the cells lastDept = RTrim(.Cells(rw, "A") & .Cells(rw, "B") & .Cells(rw, "C")) .Range("A" & rw & ":C" & rw).ClearContents For r = rw To 2 Step -1 If Trim(.Cells(r, "A").Value) = "MEMBER NUMBER" Then nextDept = RTrim(.Cells(r - 1, "A") & .Cells(r - 1, "B") & .Cells(r - 1, "C")) .Range("A" & r - 1 & ":C" & r - 1).ClearContents .Cells(r - 1, "A").Value = lastDept lastDept = nextDept End If Next End With Next ws End Sub 

每次在A列中find“MEMBER NUMBER”时,它会抓取上一行A:C列中的信息,然后清除这些单元格,并将列A中的连接值replace为前一次遇到“MEMBER NUMBER ”。 (我假设连接列A:C到一个string是一件好事,如果不是,我可以编辑它,使值保存为单独的string。


编辑search“部门”(依靠第1行总是空白)。

 Sub format() Dim ws As Worksheet Dim r As Long Dim lastDept As String Dim nextDept As String Dim rw As Long 'Initialise "lastDept" to blank so that the first replacement we make '(ie the one on the last row) is to an empty value lastDept = "" 'Loop through every worksheet 'Use "Worksheets" collection rather than "Sheets" collection so that 'we don't try to do any processing on Charts ("Sheets" contains both 'Worksheet and Chart objects) For Each ws In Worksheets 'Use a "With" block so that we don't have to constantly write '"ws.Cells", "ws.Range", "ws.Rows", etc - we can just use '".Cells", ".Range", ".Rows", etc instead With ws 'Find the end of the data by finding the last non-empty cell in column A rw = .Cells(.Rows.Count, "A").End(xlUp).Row 'Loop (backwards) through each row in the worksheet For r = rw To 1 Step -1 'We only need to process something when Column A starts with '"Department", or if this is row 1 (which will be empty) If Left(.Cells(r, "A").Value, 10) = "Department" Or r = 1 Then 'Temporarily store the current values of this row's columns A:C 'Join all three cells into one string to make it look "nicer" nextDept = RTrim(.Cells(r, "A").Value & _ .Cells(r, "B").Value & _ .Cells(r, "C").Value) 'Clear out what was in A:C .Range("A" & r & ":C" & r).ClearContents 'Write the contents of "lastDept" into column A .Cells(r, "A").Value = lastDept 'Change "lastDept" to be the value which we stored in our 'temporary variable "nextDept" (The temporary variable was 'needed because, by the time we get to this line, we have 'written over the top of the information in the worksheet 'cells.) lastDept = nextDept End If Next ' end of processing of a row End With Next ws ' end of processing of a Worksheet End Sub 

您将firstrstDept设置为与lastDept相同的行。 我相信你打算把这些设定在一个范围内。 尝试这个:

 Sub format() Dim ws As Worksheet Dim frstDept As Range Dim lastDept As Range Dim rng As Long Dim n As Long Dim nlast As Long Dim rw As Range Dim i As Integer Dim iVal As Integer Set rw = ActiveWorkbook.ActiveSheet.UsedRange.Rows nlast = rw.Count For Each ws In Sheets ws.Activate iVal = Application.WorksheetFunction.CountIf(ws.Range("A:A"), "*Department*") Debug.Print iVal On Error GoTo NextWS For i = iVal To 1 Step -1 ' You dont have a next statement closing this for loop. With ws Set frstDept = Range("A:A").Find(What:="Department", LookIn:=xlValues, LookAt:=xlPart) Set lastDept = Range("A:A").Find(What:="Department", LookIn:=xlValues, LookAt:=xlPart) If .frstDept.Value = .lastDept.Value Then lastDept.Offset(-1, 0).Value = frstDept.Offset(1, 0).Value End If End If End With NextWS: Err.Clear Next ' This should close the for loop for your iVal iteration. Next ws End Sub 

此外,你的发现声明是相同的,理论上应该返回相同的范围。 我无法理解你为什么在你的问题中使用相同的查询语句,所以我没有关于如何改进它的build议。

上面的代码可能不会解决你的问题,但它应该让你更接近。 我最好的select是,每当你试图获得你的第一个和最后一个variables的值时,你的On Error语句就被激活了,所以它一直没有做任何事情。

我希望这有帮助。