运行时错误“5”无效的过程调用或参数:find使用前16个字符后查找/提取字符的特定文本

我是VBA编码的新手。 我已经使用了三种不同types的代码,这些代码与我正在尝试完成的代码相近,但是不断出现错误。 有人可能会build议什么是错的?

在列(3)下面的单元格更改ProfitCenterName&CouponTypes,我有昏暗的variables作为string。 我命名了variables的范围来查找位置。

我使用的是位于“封闭的date/时间”的ProfitCenter,因为这个variables名不会改变,但是优惠券和利润中心的名字是这样做的。

我使用偏移量search“Closed Date / Time”,使用“Right和lens”定位和提取文本string中的名称,然后使用偏移来放置提取的名称,并使用循环删除原始string。

我也想把提取的名字放在实际交易的旁边,但不知道该怎么办。

ProfitCenter Coupontype **Profit Center : Copper Oak(6)** **Coupon : 20 Yards $25(303)** Closed Date/Time Copper Oak(6) 20 Yards $25(303) 09/27/2015 18:15 Copper Oak(6) 21 Yards $25(303) 09/27/2015 20:04 Coupon Total Coupon : Dells Deals 10%(247) Dells Deals 10%(247) 09/27/2015 18:15 Coupon Total Profit Center Total **Profit Center : High Stakes Bar(2)** **Coupon : SILVER $5(298)** Closed Date/Time High Stakes Bar(2) SILVER $5(298) 10/24/2015 16:44 High Stakes Bar(2) SILVER $5(298) 10/24/2015 16:44 High Stakes Bar(2) SILVER $5(298) 10/24/2015 16:44 

和代码:

 Sub CopyDataFrCellAndPlaceInCellNext() Dim ProfitCenterName As String Dim ProfitCenter As Range Dim Coupontype As Range Dim firstaddress As String Dim CouponName As String 'In column (3)I want to select ProfitCenterName cell "Profit Center : Copper Oak(6)" 'extract only the name of the ProfitCenterName "Copper Oak(6)" 'Then place the ProfitCenterName "Copper Oak(6)" into Column(1) 'delete the original cell in column(3)"Profit Center : Copper Oak(6)" With ActiveSheet.Columns(3) Set ProfitCenter = Cells.find(What:="Closed Date/Time", After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlPart) ProfitCenterName = Right(ProfitCenterName, Len(ProfitCenterName) - 16) If Not ProfitCenter Is Nothing Then firstaddress = ProfitCenter.Address Do ProfitCenter.Offset(-2, 0).Copy ProfitCenter.Offset(1, -2) ProfitCenter.Offset(-2, 0).Clear Set ProfitCenter = .FindNext(ProfitCenter) Loop While (Not ProfitCenter Is Nothing) And ProfitCenter.Address <> firstaddress End If End With 'In column (3) I want to select ProfitCenterName cell "Coupon : 20 Yards $25(303)" 'extract only the name of the ProfitCenterName "20 Yards $25(303)" 'Then place the ProfitCenterName "20 Yards $25(303)" into Column(2) 'delete the original cell in column(3)"Coupon : 20 Yards $25(303)" With ActiveSheet.Columns(3) Set Coupontype = .find(What:="Closed Date/Time", After:=ActiveCell, LookIn:= _ xlFormulas, LookAt:=xlPart) CouponName = Right(ProfitCenterName, Len(ProfitCenterName) - 9) If Not Coupontype Is Nothing Then firstaddress = Coupontype.Address Do Coupontype.Offset(-1, 0).Copy Coupontype.Offset(1, -1) Coupontype.Offset(-1, 0).Clear Set Coupontype = .FindNext(Coupontype) Loop While (Not Coupontype Is Nothing) And Coupontype.Address <> firstaddress End If End With End Sub 

  1. ProfitCenterName = Right(ProfitCenterName, Len(ProfitCenterName) - 16)抛出一个错误,因为当你开始使用ProfitCenterName时它是空的 ,你应该使用类似于ProfitCenterName = ProfitCenter.Offset(-2, 0)

  2. 你可以在同一个循环中进行两次string提取,看看结果。

  3. 我将ProfitCenter.Offset(-2, 0).Copy ProfitCenter.Offset(1, -2)更改为ProfitCenter.Offset(1, -2) = ProfitCenter.Offset(-2, 0) ,效率更高!
    当使用多个单元格Range时,只需在两侧添加.Value (对于一个单元格Range是隐含的)来完成相同的操作


这里是你的代码修改(我只是改变ProfitCenterNamefCell ):

 Sub CopyDataFrCellAndPlaceInCellNext() Dim fCell As Range, _ FirstAddress As String, _ ProfitCenterName As String, _ CouponName As String With ActiveSheet.Columns(3) Set fCell = .Cells.Find(What:="Closed Date/Time", _ After:=ActiveCell, _ LookIn:=xlFormulas, _ LookAt:=xlPart) If Not fCell Is Nothing Then FirstAddress = fCell.Address Do ProfitCenterName = fCell.Offset(-2, 0).Value2 Debug.Print ProfitCenterName Debug.Print Len(ProfitCenterName) ProfitCenterName = Trim(Right(ProfitCenterName, Len(ProfitCenterName) - 16)) CouponName = fCell.Offset(-1, 0).Value2 CouponName = Trim(Right(CouponName, Len(CouponName) - 9)) fCell.Offset(1, -2) = ProfitCenterName fCell.Offset(1, -1) = CouponName fCell.Offset(-2, 0).Clear fCell.Offset(-1, 0).Clear Set fCell = .FindNext(fCell) Loop While (Not fCell Is Nothing) And fCell.Address <> FirstAddress End If End With End Sub