English 中文(简体)
确定最后浏览量以复制含有对另一工作表格特定价值的浏览量
原标题:Define finalrow to copy rows containing a specific value to another worksheet
  • 时间:2021-06-18 16:18:28
  •  标签:
  • excel
  • vba

我想复制载有对另一张工作单具体价值的浏览器。

我遵循以下公式:。 https://www.you Programme.com/watch?v=-QFjJoRGCtU&t=332

It only copied the first row to another worksheet. I m guessing I didn t define finalrow properly.

Sub Search_Extract()
    Dim resultnumber As Integer
    Dim finalrow As Long
    Dim datasheet As Worksheet, reportsheet As Worksheet
    Dim i As Integer  rowcounter

    Set datasheet = Sheet4
    Set reportsheet = Sheet3
    resultnumber = reportsheet.Range("A1").Value

    reportsheet.Range("D5:F7000").ClearContents

    datasheet.Select
    finalrow = Cells(Rows.Count, 3).End(xlUp).Row

    For i = 1 To finalrow
        If Cells(i, 3) = resultnumber Then
            Range(Cells(i, 1), Cells(i, 3)).Copy
            reportsheet.Select
            Range("D6700").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
              :=False, Transpose:=False
        End If
    Next i

    reportsheet.Select
End Sub

”/

问题回答

A VBA Lookup

  • I m keeping it close to the initial idea. It can be improved on multiple accounts e.g. by using arrays, AutoFilter, AdvancedFilter, and whatnot.
  • Feel free to rename the variables back to their initial names. The only new variable is dRow which keeps track of the current destination row so you don t need the ...(xlup).Offset(1) business . Note the dRow = dRow + 1.
  • Most of the time you don t want to use Select or Activate. Qualify your ranges instead i.e. note the sws. and dws. in front of Range, Cells and Rows (Each range belongs to a worksheet). This should be your most important lesson learned from this code.

www.un.org/spanish/ecosoc

Option Explicit

Sub SearchExtract()
    
      Source (DataSheet) - being read from
    Dim sws As Worksheet   Worksheet
    Dim sRow As Long   Current Row
    Dim slRow As Long   Last row
    
      Destination (ReportSheet) - being written to
    Dim dws As Worksheet   Worksheet
    Dim dRow As Long   Current Row
    
      Other
    Dim SearchNumber As Long   Criteria
    
      Source
    Set sws = Sheet4
    slRow = sws.Cells(sws.Rows.Count, "C").End(xlUp).Row
    
      Destination
    Set dws = Sheet3
    dws.Range("D5:F" & dws.Rows.Count).ClearContents
    dRow = 5
    SearchNumber = dws.Range("A1").Value
    
      Loop & Copy Values
    For sRow = 1 To slRow   use 2 if you have headers in the first row
        If sws.Cells(sRow, "C").Value = SearchNumber Then
            dws.Range(dws.Cells(dRow, "D"), dws.Cells(dRow, "F")).Value _
                = sws.Range(sws.Cells(sRow, "A"), sws.Cells(sRow, "C")).Value
            dRow = dRow + 1
        End If
    Next sRow

End Sub




相关问题
import of excel in SQL imports NULL lines

I have a stored procedure that imports differently formatted workbooks into a database table, does work on them then drops the table. Here is the populating query. SELECT IDENTITY(INT,1,1) AS ID ...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

Excel date to Unix timestamp

Does anyone know how to convert an Excel date to a correct Unix timestamp?

C# GemBox Excel Import Error

I am trying to import an excel file into a data table using GemBox and I keep getting this error: Invalid data value when extracting to DataTable at SourceRowIndex: 1, and SourceColumnIndex: 1. As ...

Importing from excel "applications" using SSIS

I am looking for any tips or resources on importing from excel into a SQL database, but specifically when the information is NOT in column and row format. I am currently doing some pre-development ...

热门标签