简体   繁体   English

未在范围内设置VBA动态范围公式

[英]VBA Dynamic Range Formula NOT Set in Range

I have a problem with my code. 我的代码有问题。 I run a simple VBA code with userform and in one of the workbook's worksheet I run my formula in specific range in specific columns. 我使用用户窗体运行一个简单的VBA代码,并且在工作簿的工作表之一中,在特定列的特定范围内运行公式。 Somehow, and I dont know why, the code runs on this lines without some error but the cells stay as it was without formula when the code ends. 不知何故,我不知道为什么,代码在此行上运行时没有出现任何错误,但是当代码结束时,单元格保持不变而没有公式。 If someone could analyze me problem here in columns AC and AD i will be greatful. 如果有人可以在AC和AD列中分析我的问题,我将非常感激。

here is the specific lines of code, the problem is Just with columns AC, AD: 这是特定的代码行,问题仅在于AC,AD列:

    VLR = MainWB.Worksheets(2).Cells(Rows.Count, "A").End(xlUp).Row

With MainWB.Worksheets(5)
    UniqueLRow = .Cells(Rows.Count, "E").End(xlUp).Row
End With

'Column AC
With MainWB.Worksheets(2)
    .Range("AC2").Activate
    .Range("AC2:AC" & VLR).FormulaR1C1 = "=IF(COUNTIF(Unique!R2C5:R " & UniqueLRow & " C5,visual!RC[-2]),1,"""")"
End With

HourLR = MainWB.Worksheets(6).Cells(Rows.Count, "E").End(xlUp).Row

'Column AD
With MainWB.Worksheets(2)
    .Range("AD2:AD" & VLR).FormulaR1C1 = "=IF(COUNTIF(HourCommissions!R2C5:R " & HourLR & " C5,visual!RC[-3])>0,1,"""")"
End With

here; 这里;

.Range("AD2").AutoFill Destination:=Range("AB2:AB" & VLR), Type:=xlFillDefault

Destination is the range in active sheet. 目标是活动工作表中的范围。 It will work if only it is Worksheet(2). 如果只有Worksheet(2),它将起作用。 Do you want this or just forget dot before Range? 您是否要这样做还是忘了Range之前的点?

Give this a try and see if that resolves your issue... 试试看,看看是否能解决您的问题...

With MainWB.Worksheets(2)
    .Range("AC2", "AC" & VLR).FormulaR1C1 = "=IF(COUNTIF(Unique!R2C5:R" & UniqueLRow & "C5,visual!RC[-2]),1,"""")"
End With

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

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