Home > other >  Vba for help
Vba for help

Time:09-16

Bosses have a look at me this code where there are problems
Sub test ()

Dim date_ As String
Dim range_ As Range
Dim As Integer I
Dim range2 As Range

Date_="201801"

The Set range_=Range (Cells (8, 1), the Range (" A8 "). The End (xlDown))

The Do While date_ & lt;> "201813"

The Set range2=rowaa (range_ date_)

Date_=date_ + 1

Loop

End Sub


The following is a function

The Public Function rowaa (range_ As Range, date_ As String)

For Each I In range_

If Not (i.V alue Like (" * "& amp; Date_ & amp; "*")) Then

The Exit For

End the If

The Set rowaa=I

Next

End the Function

And the second time in the do while running to Set range2=rowaa (range_, date_) he suggests an error when the request object,

CodePudding user response:

Don't know understand right:
My understanding is that the original poster wants to down A column, A8 cell found in turn contains "201801", "201802",... , "201812" all characters of the last occurrence of case, and returns the character at a time,
If so, the two may be able to modify:
1, Dim range2 As Range: Dim range2 As String or not directly write the Dim range2
Reason: the rowaa return values stored in the range2 variable, and the return value is a string of rowaa
2, the function of rowaa cycle, if written in accordance with the original poster
If Not (i.V alue Like (" * "& amp; Date_ & amp; "*")) Then
The Exit For
So if contains the shape of the date_ did not appear in the A8, exit the loop directly, no longer traversal A8 downward cell
So suggest to:
For Each I In range_
If (i.V alue Like (" * "& amp; Date_ & amp; "*")) Then
Rowaa=I
End the If
Next



Finally the enclosed programs:
The Public Function rowaa (range_ date_)

For Each I In range_
If (i.V alue Like (" * "& amp; Date_ & amp; "*")) Then
Rowaa=I
End the If
'the Debug. Print rowaa
Next

End the Function


Sub test ()

Dim date_ As String
Dim range_ As Range
Dim As Integer I
Dim range2 As String

Date_="201801"

'Sheets (" Sheet1 "). The Activate

The Set range_=Range (Cells (8, 1), the Range (" A8 "). The End (xlDown))

The Do While date_ & lt;> "201813"

Range2=rowaa (range_ date_)

Sheets (" Sheet1 "). The Range (" A1 ")=range2

Date_=date_ + 1
'the Debug. Print date_

Loop

End Sub

I recently in a dig at VBA, watching the program is also an error many times today, summed up when it comes to using the set to the variable assignment:
To object variables (such as: range, worksheet, collection) assignment to the Set; Common variables (such as integer, string) assignment without (actually for ordinary variable assignment is to use the let, but can be omitted the let)
So in this program only need to use the set in range_ assignment,

Hope to help, thank you for your question, let me learn a lot today, ink,

CodePudding user response:

0 to learning VBA, do not have the resources cannot download!!!!!

CodePudding user response:

Can earn points after reply?
  • Related