I am trying to do a few things with this subroutine. First i need to insert a row after every 18th row Then i need to insert a string in column A based on the string in the above row (ie 17th row in the first iteration). Finally i need to insert a string in column B of the ith row again based on the previously established string.
So in Layman's terms, row 17 will say 120F_CASH i want to find "_ " and take the characters in the string from the left of the "_ ", then in row 18 i want to put 120F & "_MULTI_STRAT" in column A in column B of row 18, i want to insert "SSELECT PERF.IMST.INDEX WITH ENTITY = " & "120F" & "AND WITH CATEGORY1 = ""01"""
here's my code:
Option Explicit
Sub InsertRowEveryXrows()
Dim r As Long, lr As Long
Dim code As String
lr = Range("A" & Rows.Count).End(xlUp).Row
For r = 18 To lr Step 18
Rows(r).Insert Shift:=xlDown
Set code = Left(Range(Cells(r - 1, "A"), InStr(1, "_", Cells(r, "A")) - 1))
Cells(r, "A") = code & "_MULTI_STRAT"
Cells(r, "B") = "SSELECT PERF.IMST.INDEX WITH ENTITY = " & code & "AND WITH CATEGORY1 = ""01"""
Next r
End Sub
i am getting a compile error: Wrong number of arguments or invalid property assignments on this line:
Set code = Left(Range(Cells(r - 1, "A"), InStr(1, "_", Cells(r, "A")) - 1))
the string's location is in row 17 (r-1) in column A & the number of characters is found by looking for "_"
What am i missing? *fyi i had to add a space after the underscore for it to show up properly here, however, there SHOULD NOT be a space after the underscore.
Set code = Left(Range(Cells(r - 1, "A"), InStr(1, "_", Cells(r, "A")) - 1))
The Left
function wants a string and an integer, but you haven't provided the integer. Here's the same expression, without the outer Left
function call:
Set code = Range(Cells(r - 1, "A"), InStr(1, "_", Cells(r, "A")) - 1)
Notice anything? The result of InStr
is being passed as the 2nd argument to the (unqualified) Range
property, and that isn't what you intended.
In fact, you don't need that Range
call at all. This should be closer to your intent:
Set code = Left(Cells(r - 1, "A"), InStr(1, "_", Cells(r, "A")) - 1)
Note that there is quite a bit of implicit code going on here; made explicit, reads like this:
Set code = Left(ActiveSheet.Cells(r - 1, "A").Value, InStr(1, "_", ActiveSheet.Cells(r, "A").Value) - 1)
Do you really intend to be working of just whatever worksheet happens to be active? Most likely not, but if so, consider explicitly qualifying these Cells
calls with ActiveSheet
. If you mean to be working with a specific sheet, use that sheet object as a qualifier instead.
Now, you're using Set
for this assignment, but code
is not an object reference, and that is another problem.
Strings are not objects in VBA, they're assigned with the regular value assignment syntax. That is, without a Set
keyword (you could have the legacy Let
keyword there if you wanted, but it's not needed):
code = Left(ActiveSheet.Cells(r - 1, "A").Value, InStr(1, "_", ActiveSheet.Cells(r, "A").Value) - 1)
Now, Range.Value
(explicit here, implicit in your code) will be a Variant
, not a String
. In most cases, it won't matter.
Until one cell has a Variant/Error
subtype (think #N/A
, or #VALUE!
worksheet errors); then everything blows up with a type mismatch error. To avoid this, you can use the Text
of the cell instead of its Value
, or you can pull that value into its own local Variant
variable, and only proceed to treat it like a string when IsError
returns False
for it.
You could use something like so
Function Pop_String(strInput As String, strDelim As String, Optional lngInstance = 1)
Dim aTemp() As String
aTemp = Split(strInput, strDelim, lngInstance + 1)
Pop_String = aTemp(lngInstance - 1)
End Function
Calling like so
Pop_String("abcdef","-",4)
returns d Pop_String("abcdef","-",2)
returns b
I would add some error checking too, the default (without 3rd argument) is the first section, so
Pop_String("test-string","-")
would return test and Pop_String("test-string","-",2)
would return string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.