Solved

Coding a Variable Range in one Column in Excel

Posted on 2003-11-05
4
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

733 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