扫描变量并选择该行

发布于 2025-01-25 19:42:28 字数 1519 浏览 3 评论 0原文

我有一个很大的数据表,我需要一个代码来扫描AX的列(屏幕截图“选择”)或添加复选框。如果有AX,则应选择行,并且某些列应转置到新表中。

我有一个代码来扫描列的X,并具有代码来转换所需的列,但是我需要一些帮助才能将这些代码组合在一起。

  1. 扫描列(选择)x,然后选择
  2. 所选行的某些单元格(选定的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.

  1. scan column(selection) for an x and select the rows
  2. 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

Help me combine these codes
Thx in advance
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下的泡沫是彩色的 2025-02-01 19:42:28

如果这是一次性任务,我将使用一个公式:

=TRANSPOSE(FILTER(FILTER(A:J, {1,0,1,1,1,1,0,1,1,0}), K:K="x"))

确切的符号取决于本地设置(在我的情况下,公式看起来不同)。但是,可以将此与一起评估在VBA中。在这里,我们将列标记要使用数组{1,0,1,1,1,1,1,1,0,1,1,0},然后用“ x” <>过滤行。 /代码>在K列中。

对于VBA,这种情况更容易使用listObject在一般情况下。但是,我们还可以与标记的行和的感兴趣的列相交

Sub CopyMarked()
Dim Source As Worksheet
Dim Destination As Worksheet
Dim Data As Range
Dim Criteria As Range
    Set Source = ActiveSheet
    Set Destination = Worksheets.Add(After:=Source)
    Set Data = Source.Range("A:A, C:F, H:I")       ' columns 1,4,8,6,9,3,5
    Set Criteria = Source.Columns("K").SpecialCells(xlCellTypeConstants).EntireRow
    Intersect(Data, Criteria).Copy 
    Destination.Range("a1").PasteSpecial xlPasteValues, Transpose:=True
End Sub

If this is a one-time task, I would use a formula:

=TRANSPOSE(FILTER(FILTER(A:J, {1,0,1,1,1,1,0,1,1,0}), K:K="x"))

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 also Intersect columns of interest with marked rows and .Copy toDestination:

Sub CopyMarked()
Dim Source As Worksheet
Dim Destination As Worksheet
Dim Data As Range
Dim Criteria As Range
    Set Source = ActiveSheet
    Set Destination = Worksheets.Add(After:=Source)
    Set Data = Source.Range("A:A, C:F, H:I")       ' columns 1,4,8,6,9,3,5
    Set Criteria = Source.Columns("K").SpecialCells(xlCellTypeConstants).EntireRow
    Intersect(Data, Criteria).Copy 
    Destination.Range("a1").PasteSpecial xlPasteValues, Transpose:=True
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文