宏vba需要将公式应用于列,直到它在最后一行中有值 [英] macros vba need to apply formula to column till it has value in the last row

查看:831
本文介绍了宏vba需要将公式应用于列,直到它在最后一行中有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在整个C列中应用IF公式,直到使用VBA在表格中有最后一个值。

但我是如果我使用以下代码获取错误438。 Plz帮助我

but i am getting error 438 if i use the following code . Plz help me

Sub test11()

With Sheets("Sheet")
        .Range("c1:c" & .Cells(.Rows.Count, "A").End(xlUp).Row).Formula = "=IF(B1="",TRIM(A1),TRIM(B1))"

    End With



End Sub


推荐答案

所以你的工作表名称是工作表 Sheet1 ? OP提到的Sheet名称是 Sheet2 。这消除了一个错误。其次,您需要将 D 列设置为 .Cells(.Rows.Count,D)。End(xlUp).Row)而不是 A 列。

So your sheet name is Sheet or Sheet1? And OP mentioned Sheet name is Sheet2. That removes one error. Secondly, you need to set D column as .Cells(.Rows.Count,"D").End(xlUp).Row) instead of A column.

这是一个非常难看的代码:它需要最后在count中使用了一行 Long 变量。然后相应地设置范围以使用自动填充设置公式

Here is a very ugly code to try out: It takes last used row in count into the Long variable. Then set the range accordingly for setting up the formula using AutoFill.

Sub test11()
Dim l As Long    

l = Sheets(1).Range("d1:d" & Sheets(1).Cells(Sheets(1).Rows.Count, "D").End(xlUp).Row).Count
    With Sheets("Sheet1")
        .Range("d1").Formula = "=IF(IsNull(B1),TRIM(A1),TRIM(B1))"
        .Range("d1").AutoFill Destination:=Range("d1:d" & l), Type:=xlFillDefault
    End With
End Sub

这篇关于宏vba需要将公式应用于列,直到它在最后一行中有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆