• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

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

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
New_Alex
Asked:
New_Alex
  • 3
1 Solution
 
Chris BottomleyCommented:
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
 
Chris BottomleyCommented:
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
 
New_AlexAuthor Commented:
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
 
Chris BottomleyCommented:
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now