Solved

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

Posted on 2011-09-08
4
248 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

756 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