Make last column in vba code dynamic

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
169
Office Version
  1. 365
Hi,

I have the below code that works great, however i need to update the last column depending on the number of columns in the file.

I am not sure how I can make the last column dynamic in the below code.


VBA Code:
    Dim i As Long
    Dim Lastrow As Long
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row

    For i = 2 To Lastrow

        Range("A" & i).Formula = Range("B" & i) & ";" & Chr(34) & Replace(Join(Application.Index(Range("c" & i & ":CS" & i).Value, 1, 0), """;"""), """""", "")
       
    Next i
 
Ok, how about
VBA Code:
    Dim i As Long
    Dim Lastrow As Long, LastCol As Long
    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
    LastCol = Cells(2, Columns.Count).End(xlToLeft).Column
   
    For i = 2 To Lastrow

        Range("A" & i).Formula = Range("B" & i) & ";" & Chr(34) & Replace(Join(Application.Index(Range("c" & i, Cells(i, LastCol)).Value, 1, 0), """;"""), """""", "")
      
    Next i
Thank you almost perfect, it is finding the last column but not adding the last " if there is data in the last column, example it is appearing as "ABC instead of "ABC"

When the last column is blank it is appearing as
;"
instead of
;

Thanks
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
VBA Code:
        Range("A" & i).Formula = Range("B" & i) & ";" & Chr(34) & Replace(Join(Application.Index(Range("c" & i, Cells(i, LastCol)).Value, 1, 0), """;""") & Chr(34), """""", "")
 
Upvote 0
Solution
Range("A" & i).Formula = Range("B" & i) & ";" & Chr(34) & Replace(Join(Application.Index(Range("c" & i, Cells(i, LastCol)).Value, 1, 0), """;""") & Chr(34), """""", "")
Sorry for the delay responding, works perfectly. Thank you
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,816
Members
449,049
Latest member
cybersurfer5000

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