Excel VBA to replace value with same value + 1

ExcelDropper

New Member
Joined
Oct 25, 2020
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
I have a cell that is =vlookup(a1,a:c,2,0) but over and over again like =vlookup(a1,a:c,2,0)+vlookup(a1,a:c,2,0)+vlookup(a1,a:c,2,0)+vlookup(a1,a:c,2,0). Is there a fast way to make this lookup value increase by 1 every time it is repeated? so it would be vlookup(a1...)+vlookup(a2...)+vlookup(a3...) using VBA?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
this will do it 10 times , just change the loop as you require:
VBA Code:
Sub tst()
'=vlookup(a1,a:c,2,0)+
txt = ""
pls = ""
For i = 1 To 10
 txt = txt & pls & "vlookup(a" & i & ",a:c,2,0)"
 pls = "+"
Next i
Cells(5, 5) = "=" & txt
End Sub
 
Upvote 0
Hello Excel brother,
Can you explain problem a little bit better.
It's not clear are you want textual increasing like Offthelip have done,
or you want range result increasing. As second solution try this...
VBA Code:
Sub IncreaseFormula()

    Dim varWorksheet As Worksheet
    Dim varRange As Range
    Dim varNLoop As Long
    Dim varResult
    
    Set varWorksheet = ActiveSheet
    Set varRange = varWorksheet.Range("A1:C5")
    
    For varNLoop = 1 To varRange.Rows.Count
        varResult = Application.WorksheetFunction.VLookup _
            (ActiveSheet.Range("A" & varNLoop), varRange, 2, 0)
        MsgBox (varResult)
    Next
   
End Sub
 
Upvote 0
Hello Excel brother,
Can you explain problem a little bit better.
It's not clear are you want textual increasing like Offthelip have done,
or you want range result increasing. As second solution try this...
VBA Code:
Sub IncreaseFormula()

    Dim varWorksheet As Worksheet
    Dim varRange As Range
    Dim varNLoop As Long
    Dim varResult
   
    Set varWorksheet = ActiveSheet
    Set varRange = varWorksheet.Range("A1:C5")
   
    For varNLoop = 1 To varRange.Rows.Count
        varResult = Application.WorksheetFunction.VLookup _
            (ActiveSheet.Range("A" & varNLoop), varRange, 2, 0)
        MsgBox (varResult)
    Next
  
End Sub
Thank you for the solution, unfortunately I can't quite get it to work. What I am looking to do here is change already existing formulas, which is why offthelip's solution wouldn't work. By this I mean I already have a formula, lets say in C3 of my document, that goes like this: =VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0). I am happy with every part of this formula aside from the A1 which is repeating itself. For each time it repeats itself, I simply want it to increase by 1. So the end result would be =VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A2,H8:I8,2,0)+VLOOKUP(A2,H8:I8,2,0)+VLOOKUP(A2,H8:I8,2,0). The reason I can't use the first solution is because that simply inputs a new formula. I want to adjust the pre-existing formula. In my actual document, there will be many of these vlookups with different search ranges. I am already happy with the search ranges. I just want to change the A1 spam, which they will all have. What I am looking for is some sort of macro where I can input a range, let's say A1:C5, and replace all instances of these repeating A1s to become A1, A2, A3 and so on.
 
Upvote 0
Hello Excel brother,
Can you explain problem a little bit better.
It's not clear are you want textual increasing like Offthelip have done,
or you want range result increasing. As second solution try this...
VBA Code:
Sub IncreaseFormula()

    Dim varWorksheet As Worksheet
    Dim varRange As Range
    Dim varNLoop As Long
    Dim varResult
   
    Set varWorksheet = ActiveSheet
    Set varRange = varWorksheet.Range("A1:C5")
   
    For varNLoop = 1 To varRange.Rows.Count
        varResult = Application.WorksheetFunction.VLookup _
            (ActiveSheet.Range("A" & varNLoop), varRange, 2, 0)
        MsgBox (varResult)
    Next
  
End Sub
Just to clarify, imagine I have a formula in C1 that says =VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A1,H8:I8,2,0). In C2 I have another formula that is =VLOOKUP(A1,B3:B3,3,0)+VLOOKUP(A1,B3:B3,3,0)+VLOOKUP(A1,B3:B3,3,0)+VLOOKUP(A1,B3:B3,3,0). All I want to do is be able to input the range C1:C2 into a macro and have C1 change to =VLOOKUP(A1,H8:I8,2,0)+VLOOKUP(A2,H8:I8,2,0)+VLOOKUP(A3,H8:I8,2,0)+VLOOKUP(A4,H8:I8,2,0) while C2 changes to =VLOOKUP(A1,B3:B3,3,0)+VLOOKUP(A2,B3:B3,3,0)+VLOOKUP(A3,B3:B3,3,0)+VLOOKUP(A4,B3:B3,3,0). The entire formula will stay the same, apart from the A1 part.
 
Upvote 0
Here it is.
It's not some kind of smooth procedure, but can done job.
VBA Code:
Option Explicit

Sub ChangeFormula()
    
    Dim varWorksheet As Worksheet
    Dim varRange1, varRange2 As Range
    Dim varTempString, varTempString2, varTempString3, varTempString4, _
        varTempStringLeft, varTempStringMiddle, varTempStringRight As String
    Dim varNLoop, varNLoops, varSpace1, varSpace2, varChar As Integer
  
    Set varWorksheet = ActiveSheet
    Set varRange2 = varWorksheet.Range("C1:C2")
    
    For Each varRange1 In varRange2
       varTempString = varRange1.Formula
       varNLoops = (Len(varTempString) - Len(Replace(varTempString, "+", ""))) + 1
       varTempString2 = varTempString
       For varNLoop = 1 To varNLoops
            varSpace1 = InStr(1, varTempString, "A1")
            varSpace2 = InStr(1, varTempString, ",")
            varChar = varSpace2 - varSpace1
            varTempString3 = Left(varTempString2, varSpace1 + varSpace2)
            varTempStringLeft = Mid(varTempString3, 2, varSpace1 - varChar)
            varTempStringMiddle = "A" & varNLoop
            varTempStringRight = Mid(varTempString3, varSpace1 + _
                varChar, Len(varTempString3) - (varSpace1 + varChar - 1))
            varTempString4 = varTempString4 & varTempStringLeft & _
                varTempStringMiddle & varTempStringRight & "+"
       Next
       varTempString4 = Mid(varTempString4, 1, Len(varTempString4) - 1)
       varRange1.Formula = "=" & varTempString4
       varTempString4 = ""
     Next
     MsgBox ("You are successfully changed the ranges.")
     
End Sub
 
Upvote 0
Solution
Here it is.
It's not some kind of smooth procedure, but can done job.
VBA Code:
Option Explicit

Sub ChangeFormula()
   
    Dim varWorksheet As Worksheet
    Dim varRange1, varRange2 As Range
    Dim varTempString, varTempString2, varTempString3, varTempString4, _
        varTempStringLeft, varTempStringMiddle, varTempStringRight As String
    Dim varNLoop, varNLoops, varSpace1, varSpace2, varChar As Integer
 
    Set varWorksheet = ActiveSheet
    Set varRange2 = varWorksheet.Range("C1:C2")
   
    For Each varRange1 In varRange2
       varTempString = varRange1.Formula
       varNLoops = (Len(varTempString) - Len(Replace(varTempString, "+", ""))) + 1
       varTempString2 = varTempString
       For varNLoop = 1 To varNLoops
            varSpace1 = InStr(1, varTempString, "A1")
            varSpace2 = InStr(1, varTempString, ",")
            varChar = varSpace2 - varSpace1
            varTempString3 = Left(varTempString2, varSpace1 + varSpace2)
            varTempStringLeft = Mid(varTempString3, 2, varSpace1 - varChar)
            varTempStringMiddle = "A" & varNLoop
            varTempStringRight = Mid(varTempString3, varSpace1 + _
                varChar, Len(varTempString3) - (varSpace1 + varChar - 1))
            varTempString4 = varTempString4 & varTempStringLeft & _
                varTempStringMiddle & varTempStringRight & "+"
       Next
       varTempString4 = Mid(varTempString4, 1, Len(varTempString4) - 1)
       varRange1.Formula = "=" & varTempString4
       varTempString4 = ""
     Next
     MsgBox ("You are successfully changed the ranges.")
    
End Sub
TY for this! i really appreciate the effort that went into putting a macro together for something so strange lol. Seems to be working perfectly.
Much appreciated again, will save me a ton of time in the upcoming years
 
Upvote 0
Ok Excel brother,
I'm glad that we are create useful tool.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top