?
Solved

VBA, Excel, Insert a String in every X Columns Starting From Column Y, to Column Z

Posted on 2011-09-08
4
Medium Priority
?
260 Views
Last Modified: 2012-05-12
I am trying to make a function that will do the following:

Function InsertString( X, Y, Z) 
' ROW.SELECT = 10
' Insert "Hello" in every X Columns of Row 10, starting from Column Y, ending in Column Z
End 

Open in new window


For example:
DoMe = InsertString( 3, "F", "AM")

Will insert the word "Hello" in steps of 3 columns starting from the Cell F, ending at Cell AM,
In particular, It will insert the String "Hello" in the following Columns (fixed Row = 10)
F, I, L, O, R, U,X,AA,AD,AG,AJ,AM

2nd example:
DoMe = InsertString( 3, "F", "AO")
 It will insert the String "Hello" in the following Columns
F, I, L, O, R, U,X,AA,AD,AG,AJ,AM

It will stop at column AM again. AJ + 3 Columns = AM. AM+ 3 Columns is AP which is beyond the "AO" range limit. AP will not be included in the "Hello" Insertion....

Any ideas of how I can achieve this?
Thanks Guys
 


0
Comment
Question by:New_Alex
[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
  • 3
4 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 2000 total points
ID: 36508273
Try the following .. per your intro the string Hello is a constant in the sub.

Chris
Sub InsertString(intSkip As Integer, strFirstCol As Variant, strLastCol As Variant)
Dim intCol As Integer

    For intCol = ActiveSheet.Columns(strFirstCol).Column To ActiveSheet.Columns(strLastCol).Column Step intSkip
        ActiveSheet.Cells(Application.ActiveCell.Row, intCol).Value = "Hello"
    Next

End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36508280
In the specific case you will note I have taken the row identity for the current cursor position on the activesheet and the target sheet to be the activesheet ... but it can be whatever you want though.

Chris
0
 
LVL 1

Author Comment

by:New_Alex
ID: 36508565
Thanks chris. This works like a charm .....

I had to modify it a bit because I wanted to display the Column Letter. This becomes...

Function InsertString(intSkip As Integer, strFirstCol As Variant, strLastCol As Variant)
Dim intCol As Integer
    For intCol = ActiveSheet.Columns(strFirstCol).Column To ActiveSheet.Columns(strLastCol).Column Step intSkip
       cAddress = ActiveSheet.Cells(Application.ActiveCell.Row, intCol).Address
       cAddArr = Split(cAddress, "$")
            MsgBox cAddArr(1)
          
    Next

End Function

Open in new window



Take care
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 36508685
For info

Whilst .address returns $a$1 .address(false,false) will return simply a1

Then the row can be used as a replacement so

       cAddress = ActiveSheet.Cells(Application.ActiveCell.Row, intCol).Address(false,false)
       CAddress = replace(caddress, caddress.row, "")

Sets caddress to the column address chars.

Not criticising ... Just advising a couple of bits of related data

Chris
       
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

770 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