Solved

Coding a Variable Range in one Column in Excel

Posted on 2003-11-05
4
407 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 86
How can i compile this github project?? 2 84
Recommendation vb6 to vb.net or others 14 149
Removing line numbers from left column in config file 7 52
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…
This article will show, step by step, how to integrate R code into a R Sweave document
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 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…

773 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