Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

excel - VBA Create row below based on two criteria

I am having a hard time getting the below logic to work. The issue seems to be where the Year function is placed. It seems to be skipping over the logic completely, yet i'm not sure how to encorporate the year function without breaking the with loop.

Sub BlankLine()

Dim Col_year As Variant
Dim Col_st As Variant
Dim Col_btc As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Dim ws As Worksheet
Dim wb As Workbook
Dim state_ar() As Variant


Col_year = "E"
Col_st = "C"
Col_btc = "D"
StartRow = 1
BlankRows = 1
Set wb = ThisWorkbook
Set ws = wb.Worksheets("LU_WK_BENE_AMT")
LastRow = ws.Cells(Rows.Count, Col_year).End(xlUp).Row
Application.ScreenUpdating = False
state_ar = Array("02", "03")
For Each State In state_ar

    With ws
        For R = LastRow To StartRow + 1 Step -1
            If Year(.Cells(R, Col_year).Value) = 2020 And .Cells(R, Col_st) = i Then
                .Cells(R + 1, Col_year).EntireRow.Insert Shift:=xlDown
                .Cells(R + 1, Col_year).Value = 2021
                .Cells(R + 1, Col_st).Value = .Cells(R, Col_st).Value
                .Cells(R + 1, Col_btc).Value = .Cells(R, Col_btc).Value
            End If
        Next R
    End With
Next
Application.ScreenUpdating = True

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...