简体   繁体   English

VBA 代码自动填充右侧的公式,如果满足某些条件则停止

[英]VBA code to autofill a formula to the right that stops if certain criteria is met

I'm currently using this code to automatically copy and paste a formula across a row to the last used column:我目前正在使用此代码自动将公式跨行复制并粘贴到最后使用的列:

Sub Autofill_To_The_Right()
Dim lngLastColumn As Long
lngLastColumn = Cells.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

Dim cell As Range
For Each cell In Selection.Columns(1).Cells
    Range(cell, Cells(cell.Row, lngLastColumn)).FillRight
Next

End Sub

While it works well, instead of it auto-filling to the last used column, I would like it to stop as soon as it encounters a completely blank column虽然它运行良好,而不是自动填充到最后使用的列,我希望它在遇到完全空白的列时立即停止

I sometimes have tables separated by a blank column (let's say Table 1 is on the left and Table 2 on the right) and I don't want it to overwrite data in table 2 when I try and use the macro in table 1 if that makes sense有时我的表格由一个空白列分隔(假设表 1 在左侧,表 2 在右侧),当我尝试使用表 1 中的宏时,我不希望它覆盖表 2 中的数据,如果那样的话说得通

Any help would be much appreciated任何帮助将非常感激

Thanks,谢谢,

Thomas托马斯

will try to adjust the last column find to support this, which just needs to handle an error if the adjacent cell is empty:将尝试调整最后一列 find 以支持这一点,如果相邻单元格为空,则只需要处理错误:

Dim r As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
    If Not IsEmpty(Cells(r, 2).Value) Then
        lc = Cells(r, 1).End(xlToRight).Column
        Cells(r, 1).Copy Range(Cells(r, 2), Cells(r, lc))
    End If
Next r

Edit:编辑:

Annotating code to give some more help.注释代码以提供更多帮助。 Note that you could also .fillright using this method, where last column is found per row.请注意,您也可以使用此方法 .fillright ,其中每行找到最后一列。

Sub fsda()
    Dim r As Long, lr As Long, lc As Long 'iterating row, last row, last column
    lr = Cells(Rows.Count, 1).End(xlUp).Row 'dynamically find last row of column 1, removing need for ".select/.activate" efforts
    For r = 2 To lr  'assumes start in row 2 as header is in row 1
        If Not IsEmpty(Cells(r, 2).Value) Then  'check for column 2 to make sure it isn't blank... this is needed for 2 reasons: 1) to ensure you don't see 'last column' as the first column of next table to the right and 2) to ensure you don't get an infinite output for lc (no error, just goes on forever)
            lc = Cells(r, 1).End(xlToRight).Column  'find last column in specific row
            Cells(r, 1).Copy Range(Cells(r, 2), Cells(r, lc))  'copies, then pastes code into specified range
        End If
    Next r
End Sub

Edit2:编辑2:

Using .fill right:使用 .fill 正确:

Dim r As Long, lr As Long, lc As Long
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
    If Not IsEmpty(Cells(r, 2).Value) Then
        lc = Cells(r, 1).End(xlToRight).Column
        Range(Cells(r, 1), Cells(r, lc)).FillRight
    End If
Next r

Have you tried Ctrl + R?你试过 Ctrl + R 吗? I know it's not exactly what you're looking for but it seems like it's probably the easiest solution我知道这不完全是你要找的,但似乎它可能是最简单的解决方案

It seems to me that you simply need a different way to find the last column.在我看来,您只需要一种不同的方式来找到最后一列。

This formula will give you the closest column where there is still data, meaning that the one just after is either blank or merged with the current one.此公式将为您提供仍然存在数据的最接近的列,这意味着紧随其后的是空白或与当前列合并。

dim row as long: row = 1 'the row number where you want to do the test
dim colOrigin as long: colOrigin = 1 'the starting column from where you want to check
with ThisWorkbook.ActiveSheet
    lColumn = .Cells(row, Application.Min(.Cells(row, colOrigin).End(xlToRight).Column + 1, Columns.Count)).End(xlToLeft).Column
end with

You can adapt it if you need it to start from a different position.如果您需要从不同的位置开始,您可以对其进行调整。

You could just use the following instead of lngLastColumn (as you start from first column in your code):您可以只使用以下而不是 lngLastColumn (当您从代码中的第一列开始时):

Dim lngLastNonBlankColumn As Long
lngLastNonBlankColumn = Range("A1").End(xlToRight).Column

Dim cell As Range
For Each cell In Selection.Columns(1).Cells
   Range(cell, Cells(cell.Row, lngLastNonBlankColumn)).FillRight
Next

Only the first table will be affected.只有第一个表会受到影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM