Solved

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

Posted on 2011-09-08
4
247 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
  • 3
4 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 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

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

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

861 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