簡體   English   中英

如何考慮根據另一張表中單元格的值將數據放入一張表

[英]How to consider putting data into one sheet based on values from cells in another sheet

我在編寫一個模塊時會遇到一些問題,該模塊將從具有許多列的工作表中寫入一些數據,僅過濾我想要的列,然后將其輸入到同一工作簿和工作表中的新工作表中。

我需要做的是修復將數據寫入新表的過程,因為它看起來似乎無法正常工作。

例如:工作表A的列中的數據為:ABCDEFG,例如:A =名稱,B =電子郵件等。

I want take for example only A, B, D, E, F into a new sheet and order like this:
A = A (new sheet)
B = B (new sheet)
D = C (new sheet)

我這樣做的唯一原因是因為我需要比較一個單元格是否不為空,然后在新工作表中對其進行標記,僅是因為我會知道哪些單元格具有值並可以進行處理。

例如:A和B列將具有值,但有時只有C和E具有值,因此在新工作表上它將收到OK like標志。

我的模塊非常簡單:

*我的if語句不能很好地工作,我無法弄清楚問題出在哪里。

        Sub transport()
        Dim wb As ThisWorkbook
        Dim i As Integer

        'inputs
        Dim usr_name As String
        Dim usr_email As String
        Dim usr_id As String
        Dim total As Integer
        Dim cell_p1
        Dim cell_p2
        Dim cell_p3
        Dim cell_p4
        Dim cell_p5
        Dim cell_p6
        Dim cell_p7

        'outuputs
        Dim tgt_usr As String
        Dim tgt_email As String
        Dim tgt_usrid As String
        Dim p1 As String
        Dim p2 As String
        Dim p3 As String
        Dim p4 As String
        Dim p5 As String
        Dim p6 As String
        Dim p7 As String


        'results

        total = Worksheets("meta").Range("A" & Rows.count).End(xlUp).Row

            For i = 2 To total

                cell_p1 = ThisWorkbook.Sheets("meta").Range("K" & i)
                cell_p2 = ThisWorkbook.Sheets("meta").Range("L" & i)
                cell_p3 = ThisWorkbook.Sheets("meta").Range("M" & i)
                cell_p4 = ThisWorkbook.Sheets("meta").Range("N" & i)
                cell_p5 = ThisWorkbook.Sheets("meta").Range("O" & i)
                cell_p6 = ThisWorkbook.Sheets("meta").Range("P" & i)
                cell_p7 = ThisWorkbook.Sheets("meta").Range("Q" & i)

                p1 = ThisWorkbook.Sheets("transport").Cells(i, "D").Value
                p2 = ThisWorkbook.Sheets("transport").Cells(i, "E").Value
                p3 = ThisWorkbook.Sheets("transport").Cells(i, "F").Value
                p4 = ThisWorkbook.Sheets("transport").Cells(i, "G").Value
                p5 = ThisWorkbook.Sheets("transport").Cells(i, "H").Value
                p6 = ThisWorkbook.Sheets("transport").Cells(i, "I").Value
                p7 = ThisWorkbook.Sheets("transport").Cells(i, "J").Value

                usr_name = ThisWorkbook.Sheets("meta").Range("B" & i).Value
                usr_email = ThisWorkbook.Sheets("meta").Range("A" & i).Value
                usr_id = "'" & ThisWorkbook.Sheets("meta").Range("T" & i).Value

                tgt_usr = ThisWorkbook.Sheets("transport").Cells(i, "A").Value
                tgt_email = ThisWorkbook.Sheets("transport").Cells(i, "B").Value
                tgt_usrid = ThisWorkbook.Sheets("transport").Cells(i, "C").Value

                tgt_usr = usr_name
                tgt_email = usr_email
                tgt_usrid = usr_id

                If cell_p1 <> "" Then
                    p1 = "'1"
                        If cell_p2 <> "" Then
                            p2 = "'1"
                        If cell_p3 <> "" Then
                            p3 = "'1"
                        If cell_p4 <> "" Then
                            p4 = "'1"
                        If cell_p5 <> "" Then
                            p5 = "'1"
                        If cell_p6 <> "" Then
                                p6 = "'1"
                            If cell_p7 <> "" Then
                                p7 = "'1"
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
            Next i

   End Sub

代碼內的解釋。

Sub transport()
    'So sad that You set up an object without using it :/
    Dim wb As ThisWorkbook
    'Integer isnt bad, not good enough :P
    Dim i As Long
    Dim j As Long

    'inputs
    Dim usr_name As String
    Dim usr_email As String
    Dim usr_id As String
    Dim total As Integer
    'I prefer using an array, easier to call and loop
    Dim cell() As Variant

    'outuputs
    Dim tgt_usr_rng As Range
    Dim tgt_email_rng As Range
    Dim tgt_usrid_rng As Range
    'Dim it as an 2-D array, easier to give values into range
    Dim p(1 To 1, 1 To 7) As String

    'results

    total = Worksheets("meta").Range("A" & Rows.Count).End(xlUp).Row

    For i = 2 To total

        'You can feed a range into a variant array directly
        cell = ThisWorkbook.Sheets("meta").Range("K" & i, "Q" & i).Value
        '#Notice#
        'Since we give it a 1-row, 7-column range, the array is a 2-D array with 1 row and 7 columns.
        'So calling the first value by cell( 1 , 1), the second by cell( 1 , 2 ) and so on.

        '##### Note
        usr_name = ThisWorkbook.Sheets("meta").Range("B" & i).Value
        usr_email = ThisWorkbook.Sheets("meta").Range("A" & i).Value
        usr_id = "'" & ThisWorkbook.Sheets("meta").Range("T" & i).Value

        Set tgt_usr_rng = ThisWorkbook.Sheets("transport").Cells(i, "A")
        Set tgt_email_rng = ThisWorkbook.Sheets("transport").Cells(i, "B")
        Set tgt_usrid_rng = ThisWorkbook.Sheets("transport").Cells(i, "C")

        tgt_usr_rng = usr_name
        tgt_email_rng = usr_email
        tgt_usrid_rng = usr_id
        '#####

        'This will check the 7 columns one by one. If there is a value, gives a flag
        For j = 1 To 7
            If cell(1, j) <> "" Then
                p(1, j) = "'1"
            Else
                p(1, j) = ""
            End If
        Next j
        ThisWorkbook.Sheets("transport").Range("D" & i).Resize(1, 7) = p
    Next i
End Sub

注意:如果不再需要tgt_usr_rng,則可以像ThisWorkbook.Sheets("transport").Cells(i, "A").Value = usr_name這樣簡單地編碼ThisWorkbook.Sheets("transport").Cells(i, "A").Value = usr_name甚至可以將范圍值分配給另一個范圍。

因為看起來每個循環您只使用兩次該變量,第一次用於讀取數據,第二次用於提供數據。 您可以同時讀取並提供數據,從而節省時間和內存。 因此,如果您不需要進行其他操作,則該塊可以寫為

ThisWorkbook.Sheets("transport").Cells(i, "A") = ThisWorkbook.Sheets("meta").Range("B" & i).Value
ThisWorkbook.Sheets("transport").Cells(i, "B") = ThisWorkbook.Sheets("meta").Range("A" & i).Value
ThisWorkbook.Sheets("transport").Cells(i, "C") = "'" & ThisWorkbook.Sheets("meta").Range("T" & i).Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM