Public 用于搜索内部公式并进行更改的函数
Public function to search for inside formula and make changes
我写了一个函数,你把列名作为字符串和行号。然后它转到那个单元格并在 formula 内更改,让我们说 sheet1 到 sheet2
但是当我应用它时,很多次 excel 都关闭了。可以是什么原因。这是代码
Public Function formchange( c As String, n As Integer, Optional valueLookFor As Variant, Optional valueChangeTo As Variant) As Variant
valueLookFOr ='sheet1'
condOne = ThisWorkbook.ActiveSheet.Range(c & n).Find(What:= valueLookFor)
valueChangeTo = 'sheet2'
If condOne <> 0 Then
ThisWorkbook.ActiveSheet.Range(c&n).Replace(valueLookFor,valueChangeTo).Select
End If
End Function
正如评论中所说,一个函数不能更改它所在的单元格以外的单元格。所以,我宁愿写:
Sub frmCh_caller()
'
formchange "a", "6"
'
End Sub
和函数
Sub formchange(c As String, n As Integer, Optional valueLookFor As String, Optional valueChangeTo As String = "")
Dim dumstr As String, condOne as String
If valueLookFor = "" Then valueLookFor = "sheet1"
If valueChangeTo = "" Then valueChangeTo = "sheet2"
With ThisWorkbook.ActiveSheet.Range(c & n)
condOne = lcase(.Formula())
If Len(condOne) > 1 Then
dumstr = Replace(condOne, valueLookFor, valueChangeTo)
.Formula() = dumstr
End If
End With
End Sub
就是这样。使用 Caller-sub,事情就简单多了 :)
干杯
我写了一个函数,你把列名作为字符串和行号。然后它转到那个单元格并在 formula 内更改,让我们说 sheet1 到 sheet2
但是当我应用它时,很多次 excel 都关闭了。可以是什么原因。这是代码
Public Function formchange( c As String, n As Integer, Optional valueLookFor As Variant, Optional valueChangeTo As Variant) As Variant
valueLookFOr ='sheet1'
condOne = ThisWorkbook.ActiveSheet.Range(c & n).Find(What:= valueLookFor)
valueChangeTo = 'sheet2'
If condOne <> 0 Then
ThisWorkbook.ActiveSheet.Range(c&n).Replace(valueLookFor,valueChangeTo).Select
End If
End Function
正如评论中所说,一个函数不能更改它所在的单元格以外的单元格。所以,我宁愿写:
Sub frmCh_caller()
'
formchange "a", "6"
'
End Sub
和函数
Sub formchange(c As String, n As Integer, Optional valueLookFor As String, Optional valueChangeTo As String = "")
Dim dumstr As String, condOne as String
If valueLookFor = "" Then valueLookFor = "sheet1"
If valueChangeTo = "" Then valueChangeTo = "sheet2"
With ThisWorkbook.ActiveSheet.Range(c & n)
condOne = lcase(.Formula())
If Len(condOne) > 1 Then
dumstr = Replace(condOne, valueLookFor, valueChangeTo)
.Formula() = dumstr
End If
End With
End Sub
就是这样。使用 Caller-sub,事情就简单多了 :) 干杯