Solved

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

Posted on 2011-09-08
4
244 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now