如何从代码中删除Excel VBA的$符号
我有下面的代码:
Sub poorguy() Dim counter As Long Dim countto As Long Dim col1 As String Dim col2 As String Dim col3 As String Dim col4 As String Dim rngwrite As Range counter = 0 countto = 100 For i = 0 To countto col1 = Cells(12, 2).Offset(0, counter).Address col2 = Cells(12, 3).Offset(0, counter).Address col3 = Cells(3, 2).Offset(0, counter).Address col4 = Cells(3, 3).Offset(0, counter).Address Set rngwrite = Cells(58, 3).Offset(counter, counter) rngwrite.Value = "=(" & col1 & "-" & col2 & ")*" & col3 & "*" & col4 counter = counter + 1 Next Set rngwrite = Nothing End Sub
问题是它在公式中产生了太多的$。 例如,它给出: =($B$12-$C$12)*$B$3*$C$3
我想要的是: =($B$12-C12)*$B$3*C3
我如何删除多余的$?
将(false,false)添加到不需要绝对值的地址。
Sub poorguy() Dim counter As Long Dim countto As Long Dim col1 As String Dim col2 As String Dim col3 As String Dim col4 As String Dim rngwrite As Range counter = 0 countto = 100 For i = 0 To countto col1 = Cells(12, 2).Offset(0, counter).Address col2 = Cells(12, 3).Offset(0, counter).Address(false, false) col3 = Cells(3, 2).Offset(0, counter).Address col4 = Cells(3, 3).Offset(0, counter).Address(false, false) Set rngwrite = Cells(58, 3).Offset(counter, counter) rngwrite.Value = "=(" & col1 & "-" & col2 & ")*" & col3 & "*" & col4 counter = counter + 1 Next Set rngwrite = Nothing End Sub
一种方法是用Replace
string中的所有$
字符,在用地址设置之后,但在用于创build公式之前:
col2 = Replace(col2, "$", "")
(同上col4
)。
或者, Address
的前两个参数指示行和列部分是以相对(false)还是绝对(true,默认)生成的。 这样可以更好地控制生成的string。