Excel VBA Loop Rows Until Empty Cell

后端 未结 1 1343
挽巷
挽巷 2021-01-13 11:55

I have an excel document with some plain text in a row. A1:A5 contains text, then several houndred rows down, there\'s another few rows with text. Cells between are empty. I

相关标签:
1条回答
  • 2021-01-13 12:17

    Use a For Next Statement terminating in the last used cell in column A. Only increment y if there has been a value found and transferred and let the For ... Next increment x.

    Sub CopyTags_Click() 
    
         Dim assets As Workbook, test As Workbook
         Dim x As Long, y As Long
         Set assets = Workbooks.Open("file-path.xlsx")
         Set test = Workbooks.Open("File-path.xlsx")
         x = 1
         y = 1
         with assets.Worksheets(1)
             for x = 1 to .cells(rows.count, 1).end(xlup).row
                 if cbool(len(.Range("A" & x).value2)) then
                test.Worksheets(1).Range("A" & y) = assets.Worksheets(1).Range("A" & x)
                     y = y + 1
                 end if
             next x
             test.Worksheets(1).Range("A" & y).Value = "Hello"
        end with
    End Sub
    
    0 讨论(0)
提交回复
热议问题