扫描变量并选择该行
我有一个很大的数据表,我需要一个代码来扫描AX的列(屏幕截图“选择”)或添加复选框。如果有AX,则应选择行,并且某些列应转置到新表中。
我有一个代码来扫描列的X,并具有代码来转换所需的列,但是我需要一些帮助才能将这些代码组合在一起。
- 扫描列(选择)x,然后选择
- 所选行的某些单元格(选定的colums)到新表(我有代码)
For Each c In Range("K:K")
' If c.Value = "x" Then
' MsgBox "x found at " & c.Address
'End If
'Next c
Sub TransposeColumn2Row()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Myarray() As Variant
Dim LastRow As Integer, LastColumn As Integer
Dim StartCell As Range
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim i As Long
Dim j As Long
Set StartCell = ws1.Range("A1")
LastRow = ws1.Cells(ws1.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = ws1.Cells(StartCell.Row, ws1.Columns.Count).End(xlToLeft).Column
'copy specific columns into worksheet 2
j = 1
For i = 1 To LastColumn Step 1
Select Case i
Case 1, 4, 8, 6, 9, 3, 5 'target columns to copy
With ws1
Myarray() = .Range(.Cells(1, i), .Cells(LastRow, i)).Value
End With
With ws2
.Range(.Cells(j, 1), .Cells(j, LastRow)) = Application.WorksheetFunction.Transpose(Myarray())
End With
j = j + 1
Case Else
End Select
Next i
Erase Myarray()
End Sub
i have a big datasheet and i need a code to scan a column( screenshot "selection") for a x or add a checkbox. If there is a x the row should be selected and some of the columns should be transposed into a new table.
i have an code to scan the column for x and have the code to transpose the columns i need but i need some help to combine these codes together.
- scan column(selection) for an x and select the rows
- transpose some cells(selected colums) of the selected row into a new table ( i have the code)
For Each c In Range("K:K")
' If c.Value = "x" Then
' MsgBox "x found at " & c.Address
'End If
'Next c
Sub TransposeColumn2Row()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Myarray() As Variant
Dim LastRow As Integer, LastColumn As Integer
Dim StartCell As Range
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Dim i As Long
Dim j As Long
Set StartCell = ws1.Range("A1")
LastRow = ws1.Cells(ws1.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = ws1.Cells(StartCell.Row, ws1.Columns.Count).End(xlToLeft).Column
'copy specific columns into worksheet 2
j = 1
For i = 1 To LastColumn Step 1
Select Case i
Case 1, 4, 8, 6, 9, 3, 5 'target columns to copy
With ws1
Myarray() = .Range(.Cells(1, i), .Cells(LastRow, i)).Value
End With
With ws2
.Range(.Cells(j, 1), .Cells(j, LastRow)) = Application.WorksheetFunction.Transpose(Myarray())
End With
j = j + 1
Case Else
End Select
Next i
Erase Myarray()
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是一次性任务,我将使用一个公式:
确切的符号取决于本地设置(在我的情况下,公式看起来不同)。但是,可以将此与
一起评估
在VBA中。在这里,我们将列标记要使用数组{1,0,1,1,1,1,1,1,0,1,1,0}
,然后用“ x” <
>过滤行。 /代码>在K列中。对于VBA,这种情况更容易使用
listObject
在一般情况下。但是,我们还可以与标记的行和
的感兴趣的列相交
。If this is a one-time task, I would use a formula:
The exact notation depends on local settings (the formula looks different in my case). But this one can be used with
Evaluate
in VBA. Here we mark columns to copy with an array{1,0,1,1,1,1,0,1,1,0}
and then filter the rows with"x"
in the column K.As for VBA, this case would be easier to solve with
ListObject
in general case. But we can alsoIntersect
columns of interest with marked rows and.Copy toDestination
: