输出范围与输入范围相同

时间:2019-05-28 07:04:59

标签: excel vba range

我有一些使用VBA的历史,但是似乎找不到解决此问题的方法。我找到了一个迭代过程来选择一个单元格,执行一个过程,然后选择下一个单元格并再次执行该过程,直到NULL。将每个流程解决方案输出到下一列时遇到问题。这是我所拥有的:

Sub Name ()

Dim X As Integer
Dim MyString as String

Application.ScreenUpdating = False
NumRows = Range("D2", Range("D2").End(xlDown)).Rows.Count
Range("D2").Select
For X = 1 To NumRows
    MyString = ActiveCell.Value
    MyString = Right(MyString, Len(MyString)-6)
    Range("I2 to I#").Value = MyString
    ActiveCell.Offset(1,0).Select
Next X

End Sub

Range("I2 to I#").Value = MyString是我需要帮助的那一行。我需要它递增到I3,I4,I5等,直到达到NumRows计数。

2 个答案:

答案 0 :(得分:1)

与Cells一起使用时,最好的循环方法是For Each Cell in Range,因此考虑到注释并告诉您避免选择它,这应该对您有帮助:

Option Explicit
Sub Name()

    Dim C As Range, MyRange As Range
    Dim LastRow As Long

    Application.ScreenUpdating = False
    With ThisWorkbook.Sheets("MySheet") 'Change MySheet for your working sheet name
        LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 'last row on column D
        Set MyRange = .Range("D2:D" & LastRow) 'declare your working range
        For Each C In MyRange
            If Not C = vbNullString Then .Cells(C.Row, "I") = Right(C, Len(C) - 6)
        Next C
    End With
    Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

另一个解决方案是// infix x = (c OR (a AND B)) OR (NOT D) // postfix x = A B AND C OR D NOT OR // evaluated like so: A is a var, push on stack B is a var, push on stack AND - perform op, pop value from stack, store result in stack.top() C is a var, push on stack OR - perform op, pop value from stack, store result in stack.top() D is a var, push on stack NOT is unary. store !stack.top() in stack.top(). DO NOT POP! OR - perform op, pop value from stack, store result in stack.top() // the stack should contain the result. 。如果数据中间没有空单元格,则可以使用此方法。

Do Until