[英]How do you auto adjust rows when text appears in column be within a certain range?
I have two questions that I've broken up one I already asked this is the most important one. 我有两个问题,我已经分解了一个,我已经问过这是最重要的一个。 I pull data from a web-based tool and it imports to excel.
我从基于Web的工具中提取数据,并将其导入Excel。
There is a Swimlane in column B and in that column it has different departments the problem is only some of the names are there. B列中有一个Swimlane,在该列中有不同的部门,问题是只有一些名称存在。 Some rows that don't have the name implies that the preceding department name in Column B is applied to that row and so forth.
一些没有名称的行意味着将B列中的先前部门名称应用于该行,依此类推。
I recorded a macro but the problem is rows can and do get deleted. 我记录了一个宏,但问题是行可以删除。 There are 50+ sheets to auto adjust each department name in column b so it's extremely cumbersome.
b栏中有50多个表格可以自动调整每个部门的名称,因此非常麻烦。
It starts at row 15 in Column B. Logic is: 它从B列的第15行开始。逻辑为:
IF Column B contains something then auto adjust or double click to make it copy all the way down until the next department name. 如果B列中包含某些内容,然后进行自动调整或双击以使其完全复制到下一个部门名称。 The BLANK in the example is the portion I need to double click "Grocery, Dairy, or Seafood" so it fills in the blank with the preceding department title.
示例中的空白是我需要双击“杂货,乳制品或海鲜”的部分,因此它用前面的部门标题填充了空白。
Example: Swimlane 例如:Swimlane
Row 1 "Grocery"
Row 2 BLANK
Row 3 BLANK
Row 4 BLANK
Row 5 "Dairy"
Row 6 BLANK
Row 7 BLANK
Row 8 BLANK
Row 9 "Seafood"
Row 10 BLANK
Row 11 BLANK
What I previously had was a recorded macro that looked like this: 我以前拥有的是一个录制的宏,看起来像这样:
Sub Swimlane()
'
' Swimlane Macro
'
' Keyboard Shortcut: Ctrl+s
'
Range("A107").Select
Selection.AutoFill Destination:=Range("A107:A108")
Range("A107:A108").Select
Range("A100").Select
Selection.AutoFill Destination:=Range("A100:A106")
Range("A100:A106").Select
ActiveWindow.SmallScroll Down:=-15
Range("A86").Select
Selection.AutoFill Destination:=Range("A86:A99")
Range("A86:A99").Select
Range("A80").Select
Selection.AutoFill Destination:=Range("A80:A85")
Range("A80:A85").Select
ActiveWindow.SmallScroll Down:=-15
Range("A69").Select
Selection.AutoFill Destination:=Range("A69:A79")
Range("A69:A79").Select
Range("A64").Select
Selection.AutoFill Destination:=Range("A64:A67")
End Sub
Any suggestions would be greatly appreciated !!! 任何建议将不胜感激 !!!
Here's a macro that will loop through each worksheet, autofit column B, then copy your data down: 这是一个宏,它将循环遍历每个工作表,自动拟合B列,然后向下复制数据:
Sub formatAndCopyDown()
Dim i As Integer, lastCol As Integer, lastRow As Long, newLastRow As Long
Dim ws As Worksheet, wb As Workbook
Dim startCell As Range, copyFrom As Range
Set wb = ActiveWorkbook
For Each ws In wb.Sheets
If WorksheetFunction.CountA(Range("B:B")) > 1 Then
Debug.Print "Something in col. B"
Columns(2).AutoFit
lastRow = Cells(1048576, 2).End(xlUp).Row ' Change this to Cells(..,1) if you have data in Col. A you want the titles to be next to
Set startCell = Cells(1, 2)
Do While startCell.Row < lastRow
If startCell.End(xlDown).Offset(-1, 0).Row > lastRow Then
newLastRow = lastRow
Else
' Edit this - this is the value newLastRow will be IF you reach the last entry in your col. B
newLastRow = startCell.End(xlDown).Offset(-1, 0).Row
End If
Set copyFrom = startCell
Range(Cells(startCell.Row, 2), Cells(newLastRow, 2)).Value = copyFrom.Value
Set startCell = startCell.End(xlDown)
Loop
End If
Next ws
End Sub
Note: This assumes your Column B is independent of any other range. 注意:这假定您的B列与任何其他范围无关。 Because of that, you'll notice the last cell with
Seafood
never gets copied down. 因此,您会注意到
Seafood
的最后一个单元永远不会被复制下来。 Why? 为什么? Because I don't know what the next
lastRow
will be. 因为我不知道下一个
lastRow
是什么。 If I copied down, it'd go to the end of the spreadsheet, and I don't think that's your intention. 如果我抄下来,它会转到电子表格的末尾,我认为这不是您的意图。
If, however, you have data like this: 但是,如果您有如下数据:
A B
1 Apples Grocery
2 Bananas
3 Oranges
4 Cherries
5 Milk Dairy
6 Yogurt
7 Cheese
8 Cheese
9 Fish Seafood
10 Talapia
11 Tuna
where you want your labels to be next to the items (ie Seafood should also be next to Talapia and Tuna), then change the line lastRow = Cells(1048576,2).End...
to lastRow = Cells(1048576,1).End...
您要在标签旁边贴上标签的位置(例如,海鲜也应该在罗非鱼和金枪鱼旁边),然后将行
lastRow = Cells(1048576,2).End...
更改为lastRow = Cells(1048576,1).End...
This will loop and, if your pasteRange is going to go to the end of the spreadsheet, it'll instead use Column A's last cell as the final cell row. 这将循环,如果您的pasteRange要转到电子表格的末尾,它将使用列A的最后一个单元格作为最终单元格行。 Does that make sense?
那有意义吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.