Solved

Coding a Variable Range in one Column in Excel

Posted on 2003-11-05
4
405 Views
Last Modified: 2010-04-17
I'm trying to automate a function in Excel where a specific column receives the end result of concatenating LName,FName MI into an inserted column.   I receive an Excel file daily with the information where this function needs to be done.

My problem is that every day I don't receive the same amount of rows of names that need to be concatenated and I can't seem to figure out how to NOT hard code the Range and get it to work.  

In the code below you'll notice the Range is from rows B2 to B25.  The next day, I may only get 10 rows or the following day 50 rows, etc, etc.  

If I expand the Range out beyond the amount of rows I know I'll get (lets say B100), I end up with a column of "," (commas) after the concatenated names and then the other function I have beyond that (save as a CSV file), I end up with all the extra commas in the end that I don't want.   I couldn't seem to reverse engineer anything to get that working.

I was hoping not to have to go in and change the hard code every time I get a new file as I wanted to just have the user click the button and it will adjust to the amount of rows for that file for that day.


Is there code that will allow this to happen?  

I'm quite new at VB so if you can offer assistance, I'm in the "literal...need it spelled out" mode my learning curve is still high.  Any assistance would be greatly appreciated, as my multiple attempts have not worked.
TY...Shannon

This is the code I have thus far....
'Changes name format

    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=RC[1]&"",""&RC[2]&"" ""&RC[3]"
    Selection.AutoFill Destination:=Range("B2:B25"), _Type:=xlFillDefault
0
Comment
Question by:DURRER
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
JohnChapin earned 50 total points
ID: 9687120
You are also getting an extra space that you cannot see.

Try this macro1 from your button:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 4/4/2003 by CHAPINJ
'
Dim i As Integer
Dim str As String

    For i = 1 To 18
        If Trim(Cells(i, 1).Value) <> "" Then
            str = Trim(Cells(i, 1).Value)
            If Trim(Cells(i, 2).Value) <> "" Then
                str = str & ", " & Trim(Cells(i, 2).Value)
                If Trim(Cells(i, 3).Value) <> "" Then
                    str = str & " " & Trim(Cells(i, 3).Value)
                End If
            End If
            Cells(i, 4).Value = str
        End If
       
    Next i
End Sub


and set the i range to guarantee that you will cover all the possible rows.
(You can test thenumber of filled rows ahead of time, but I see no need here.)

Need MOre. Let me know.
John
0
 
LVL 2

Expert Comment

by:wellilein
ID: 9687132
If all rows contain a value, you might use this:

    Dim row As Long
    row = 2
    While (Range("B" & row).FormulaR1C1 > "")
        row = row + 1
    Wend
    MsgBox ("number of rows:" & row - 1)

It stops if an empty cell was found.
0
 
LVL 2

Expert Comment

by:wellilein
ID: 9687207
Whole code would be

    Columns("B:B").Select
    Selection.Insert Shift:=xlToRight
   
    Dim row As Long
    row = 2
    Do
        row = row + 1
    Loop While (Range("C" & row).FormulaR1C1 > "")
   
    Range("B2").Select
    ActiveCell.FormulaR1C1 = "=RC[1]&"",""&RC[2]&"" ""&RC[3]"
    Selection.AutoFill Destination:=Range("B2:B" & row - 1), Type:=xlFillDefault

   
0
 

Author Comment

by:DURRER
ID: 9687868
Thank you John for the code...it works great.  It even solved a second issue with the extra space for those that did not have a middle initial.  Many many thanks!!!

I tried Wellilein's code as well and I couldn't get it working YET...that is, I'm going back to try it again because I never know when I'll need code for certain situations...I thank you as well for posting.

Regards...Shannon
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now