如何使用宏自动填充包含值的最后一行,一列中有多个不同的公式?

How can I autofill to the last row containing a value using a macro, with multiple different formulas in one column?

我正在创建一个模板,其中 table 从报告下载的值可以粘贴到输入选项卡,并且 return 这些值以格式化的 table 粘贴到另一个选项卡之间有一些公式。

C、D 和 E 列包含项目代码的第 1、2 和 3 部分,F 列包含行标题(例如可用数量、开票订单等)。

之后的列(G 到 Z)主要包含查找,这些查找将查找 B 列中出现的 C、D、E 和 F 列的串联。但是,第 12 行和第 24 行具有不同的公式,第 15 和 16 行不包含公式。一项数据占用 13 行(第 12 行和第 24 行之间)。

我一直在尝试找到一个宏,就像我选择范围 B12:Z24、drag/autofill 公式和格式一样,直到包含值 [=44= 的最后一行]在A列中。A列中的"X"表示在输入选项卡的相应行中是否有值,并且可以在500行到8000行之间出现。

我有点初学者,所以我写的东西都很基础,但这是我尝试过的:

Sub FillDown()    
  Range("B12:Z24" & Range("A" & Rows.Count).End(xlUp).Row).FillDown    
End Sub

这与我从 google 提取的任何更复杂的代码一样有效。

这里是 current result

的例子

这是 desired result

的示例

我试过的只是 copies/autofills 第 12 行以下的公式和格式,而不是 table 的整个部分。有什么建议吗?

**

** 抱歉,几周前我想出了这个,但忘了在这里 post: 我没有创建新的部分(我调用第 12 行到第 24 行 "sections"),而是创建了一个包含一定数量的部分的 table,并通过循环将公式从第 13 行转换为第 23 行(我想将公式保留在第 12 行和第 24 行),并对以下部分执行相同的操作。在更大范围内需要一些时间,但可以完成工作。 代码:

Sub Pasteloop()

Application.ScreenUpdating = False

Dim i As Long
Dim ii As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim j As Long
Dim jj As Long

Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Output")

LastRow = 130 'a number 1 bigger than the last row to paste
i = 13       'start row of the 1st select
ii = 23     'end row of the 1st select
j = 13      'start row of the 2nd select
jj = 23     'end row of the 2nd select
k = 7      'column number of the left hand column of the range
p = 260      'column number of the right hand column of the range

'This is the beginning of the loop
Do While i < LastRow
'set the next range as the previous range values
sht1.Range(Cells(j, k), Cells(jj, p)) = sht1.Range(Cells(i, k), Cells(ii, 
p)).Value
'move along to the next selection by incrementing everything by 12
ii = ii + 12
i = i + 12
jj = jj + 12
j = j + 12
Loop

Application.ScreenUpdating = True

End Sub

这将每周更新报告的打开时间从几分钟缩短到几秒钟。

如果您只想让数据在除 "Available Qty" 行以外的所有行中都可见,请使用条件格式

您的示例从第 12 行开始

单元格C12 ....条件格式....使用公式确定单元格格式....公式.... =C11 ....白底白字(测试时设置为红色)

然后将 format 复制到三列中,直到你喜欢为止

我没有创建新的部分(我调用第 12 行到第 24 行 "sections"),而是创建了一个 table,其中包含一定数量的部分,并通过循环获得了一些帮助,该循环将将公式转换为第 13 行到第 23 行的值(我想将公式保留在第 12 行和第 24 行),并对以下部分执行相同的操作。在更大范围内需要一些时间,但可以完成工作。代码:

Sub Pasteloop()

Application.ScreenUpdating = False

Dim i As Long
Dim ii As Long
Dim LastRow As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim j As Long
Dim jj As Long

Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Output")

LastRow = 130 'a number 1 bigger than the last row to paste
i = 13       'start row of the 1st select
ii = 23     'end row of the 1st select
j = 13      'start row of the 2nd select
jj = 23     'end row of the 2nd select
k = 7      'column number of the left hand column of the range
p = 260      'column number of the right hand column of the range

'This is the beginning of the loop
Do While i < LastRow
'set the next range as the previous range values
sht1.Range(Cells(j, k), Cells(jj, p)) = sht1.Range(Cells(i, k), Cells(ii, 
p)).Value
'move along to the next selection by incrementing everything by 12
ii = ii + 12
i = i + 12
jj = jj + 12
j = j + 12 
Loop

Application.ScreenUpdating = True

End Sub

这将每周更新的报告的打开时间从几分钟减少到几秒钟,并且比执行相同工作的 copy/paste 代码更有效,并且使您能够在代码处于 运行 这是你无法用 copy/paste 代码做到的。