Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Remove Excel VBA's $ sign from code

Tags:

excel

vba

I have the below code:

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

The problem is that it generates too many $ in the formula. For example, it gives: =($B$12-$C$12)*$B$3*$C$3

What I want is: =($B$12-C12)*$B$3*C3

How do I remove the extra $'s?

like image 579
Kelbe Avatar asked Jul 20 '15 01:07

Kelbe


Video Answer


1 Answers

Add (false, false) to the address you do not want absolute values for.

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
like image 156
Brian Gerhards Avatar answered Oct 03 '22 04:10

Brian Gerhards