我有一些使用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 SubRange("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更多推荐
输出范围与输入范围相同
发布评论