VBA code for repeating on all cells until a blank cell is found

时间:2018-12-03 13:00:38

标签: excel vba

We receive spreadsheets with insurer information. Often Excel doesn't recognise the dates as dates, so it doesn't filter or pivot table.

If you f2 to go into the cell then it clicks it, and recognises it correctly.

As we get thousands of lines, we can't do this to each one. I recorded the macro of f2 enter which gave me:

Sub enter_f2()
    ' enter_f2 Macro
    ' Will open a cell and move to the next one
    ActiveCell.FormulaR1C1 = ""
    Range("D7").Select
End Sub

I want to adapt this to run from wherever the highlighted cell is, in a loop until it hits a blank column and then stop.

3 个答案:

答案 0 :(得分:0)

Edit the code below for either DMY (xlDMYFormat) or MDY (xlMDYFormat) regional date format.

sub fixDates()
  with worksheets("sheet1")
    .range("d:d").TextToColumns Destination:=.range("D1"), DataType:=xlFixedWidth, _
                                FieldInfo:=Array(0, xlMDYFormat)
  end with
end sub

For any single column selected,

sub fixDatesBtSel()
  with selection.columns(1)
    .TextToColumns Destination:=.cells(1), DataType:=xlFixedWidth, _
                   FieldInfo:=Array(0, xlMDYFormat)
  end with
end sub

答案 1 :(得分:0)

Try,

Option Explicit

Sub DoUntilCellIsEmpty()

    Do Until IsEmpty(ActiveCell.Value)
        Debug.Print ActiveCell.Value 'If cell is not empty print activecell value to immediate window 
        ActiveCell.Offset(1, 0).Select
    Loop

End Sub

答案 2 :(得分:0)

首先Select您要处理并运行的单元格集:

Sub RefreshCells()
    Dim r As Range, rr As Range
    Set rr = Selection
    For Each r In rr
        r.Select
        If r.Value = "" Then Exit Sub
        Application.SendKeys "{F2}"
        Application.SendKeys "{ENTER}"
        DoEvents
    Next
End Sub

在工作表处于活动状态时运行它,而不是从VBE窗口中运行它。因为它使用Select / Edit,所以会有点慢。