修改Excel VBA过程 – 清理列中的单元格

我已经创build了这个程序,它可以很好地清理列AF中的空白单元格。 我想修改这个作为variables传递列号,所以我可以使用相同的过程为其他列。

Z栏是我的临时工作栏。

有任何想法吗?

Private Sub clean_com_cells() Dim counter As Integer, i As Integer, lastrow As Integer lastrow = Mysheet.Range("AF65536").End(xlUp).Row counter = 0 For i = 1 To lastrow If Mysheet.Cells(i, 32).Value <> "" Then Mysheet.Cells(counter + 1, 26).Value = Mysheet.Cells(i, 32).Value counter = counter + 1 End If Next i Mysheet.Range("AF1:AF" & lastrow).Value = "" Mysheet.Range("AF1:AF" & lastrow).Value = Mysheet.Range("Z1:Z" & lastrow).Value Mysheet.Range("Z1:Z" & lastrow).Value = "" End Sub 

这不是我将如何做你想做的事情,但是这可以让你参数化你试图“清除”的列号。 我认为这将有助于解释你想要更明确地完成什么,但是这个代码应该能够得到你所需要的。 注意,你需要lastrow是一个Long,因为Integers只能从-32k到32k(大约) 。 FYI,Longs在最近版本的VBA中performance比Integers好,因为Integers被转换成Longs。 切勿使用整数。 另一方面,如果数据符合该configuration文件(0到255),则字节的性能会更好。

 Private Sub clean_com_cells(column_number as integer) Dim counter As Integer, i As Integer, lastrow As long Dim clear_rng as range lastrow = Mysheet.cells(65536,column_number).End(xlUp).Row counter = 0 For i = 1 To lastrow If Mysheet.Cells(i, column_number).Value <> "" Then Mysheet.Cells(counter + 1, 26).Value = Mysheet.Cells(i, column_number).Value counter = counter + 1 End If Next i with mysheet set clear_rng = Range(.cells(1,column_number), .cells(lastrow,column_number)) clear_rng.Value = .Range("Z1:Z" & lastrow).Value Mysheet.Range("Z1:Z" & lastrow).Value = "" end with End Sub 

你可以调整函数来获取一个input参数,我在下面命名为TargetColNumber 。 我还添加了方便的function来查找工作表中的最后一行…未来的重构可能涉及将Worksheet传递到清理例程。

无论如何,用一个号码调用函数,你应该很好…

 Option Explicit 'this is the routine that cleans your cells Sub clean_com_cells_in_col(TargetColNumber As Long) Dim counter As Long, i As Long, lastrow As Long Dim MySheet As Worksheet Set MySheet = ThisWorkbook.ActiveSheet lastrow = FindLastRow(MySheet) counter = 0 For i = 1 To lastrow If MySheet.Cells(i, TargetColNumber).Value <> "" Then MySheet.Cells(counter + 1, 26).Value = MySheet.Cells(i, TargetColNumber).Value counter = counter + 1 End If Next i With MySheet .Range(.Cells(1, TargetColNumber), .Cells(lastrow, TargetColNumber)).Value = "" .Range(.Cells(1, TargetColNumber), .Cells(lastrow, TargetColNumber)).Value = _ .Range(.Cells(1, 26), .Cells(lastrow, 26)).Value .Range(.Cells(1, 26), .Cells(lastrow, 26)).Value = "" End With End Sub 'we'll use this function to identify the last row in a worksheet Public Function FindLastRow(flrSheet As Worksheet) As Long If Application.WorksheetFunction.CountA(flrSheet.Cells) <> 0 Then FindLastRow = flrSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Else FindLastRow = 1 End If End Function 'this is our test Sub TestItYall() Call clean_com_cells_in_col(4) '<~ did it work? End Sub 

你可以像使用lastrow一样为你使用一个stringvariables。 这里的例子使用了一个名为“col”的variables:

 Private Sub clean_com_cells() Dim counter As Integer, i As Integer, lastrow As Integer,col as string col = "AF" 'Change this to vary the column lastrow = Mysheet.Range(col & "65536").End(xlUp).Row counter = 0 For i = 1 To lastrow If Mysheet.Cells(i, 32).Value <> "" Then Mysheet.Cells(counter + 1, 26).Value = Mysheet.Cells(i, 32).Value counter = counter + 1 End If Next i Mysheet.Range(col & "1:" & col & lastrow).Value = "" Mysheet.Range(col & "1:" & col & lastrow).Value = Mysheet.Range("Z1:Z" & lastrow).Value Mysheet.Range("Z1:Z" & lastrow).Value = "" End Sub