VBA to create formula - variable to how many cells contain value

Tosborn

New Member
Joined
May 24, 2016
Messages
44
Hi guys,

I have this code which works as intended but i need to expand it without much idea of where to begin.

the end result needs to look like this:

formula in cell:

="<<" &B1 & ","&B2 & ","&B3 & ","&B4 & ","&B5

actual value:

<<327020,327117,327123,327403,327410


My current code:

VBA Code:
Sub JnlLookup()

'to create a large journal lookup from a list of journals

Dim i As Integer

    For i = 1 To Range("D1")

        Cells(4, 4).Formula = "=""<<"" &B" & i & " & "","" "
        
    Next i

End Sub
which creates the formula:

="<<" &B1 & ","

that is the value:

<<327020,

if I run this as step into, on each run it goes down my list in the B column to give me

<<327020,

then

<<327117,
etc

with my list in column B looking like this:
327020
327117
327123
327403
327410

and the cell in D1 has a value of 5 which is counting how many cells that contain values.

I need this macro to change as the list in column B expands or contracts to have more or less numbers.

I hope this is clear enough. Greatly appreciate any assistance at all.

Many thanks!

Tim
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Something like this ...
VBA Code:
Sub JnlLookup()

    'to create a large journal lookup from a list of journals

    Dim i       As Integer
    Dim sTmp    As String

    sTmp = "=""<< """
    For i = 1 To Range("D1") - 1
        sTmp = sTmp & " & B" & i & " & "","""
    Next i
    sTmp = sTmp & " & B" & Range("D1") & ""
    Cells(4, 4).Formula = sTmp

End Sub
 
Upvote 0
In case you want only the values of the cells. Here is a macro for you to consider.

VBA Code:
Sub Test()
  [D4].Value = "<< " & Join(Application.Transpose(Range("B1:B" & [D1])), ",")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

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