输出范围与输入范围相同 [英] Output Range same as input range
问题描述
我有一些使用VBA的历史,但是似乎找不到解决此问题的方法.我发现了一个迭代过程来选择一个单元格,执行一个过程,然后选择下一个单元格并再次执行该过程,直到NULL.将每个流程解决方案输出到下一列时遇到问题.这是我所拥有的:
I have some history working with VBA, but can't seem to find the solution to this problem. I found an iteration process to select a cell, do a process, and then select the next cell and do the process again, until NULL. I am having a problem outputting each of the processes solutions into the next column. Here is what I have:
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计数.
Range("I2 to I#").Value = MyString
is the line that I need help with. I need it to increment to I3, I4, I5, etc. until it reaches NumRows count.
推荐答案
使用单元格时,循环遍历它们的最佳方法是 Range中的每个单元格
,因此请按照注释中的说明进行操作避免选择,这对您有帮助:
When working with Cells the best way to loop through them is For Each Cell in Range
so taking this and as comments told you to avoid selecting, this should help you:
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
这篇关于输出范围与输入范围相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!