ที่ไม่คาดหวังพฤติกรรมของ"สำหรับแต่ละ wks ใน ActiveWindow.SelectedSheets"มันส่งผลกระทบต่ออีกคอลัมน์นั่นมันควรจะเป็น

0

คำถาม

ฉันทำเรื่องรหัสที่ใช้งานได้ค่อนข้างดี exept สุดท้ายนส่วนหนึ่ง:

คนพฤติกรรมของส่วนสุดท้ายน่าจะเป็น".รตกแต่งภายใน.สี"และ".ค่า"รับผลกระทบจนกระทั่งเมื่อ populated คอลัมน์แทนที่มันส่งผลกระทบต่อคนแรกมือถือของคนอื่นๆคอลัมน์ มีความคิดอะไรอีกไหม

  Sub Sample_Workbook()
        
        'Creation of new workbook
        Application.ScreenUpdating = False        
        Workbooks.Add
        
        Set wb = ActiveWorkbook
        wb.SaveAs ThisWorkbook.Path & "etc.xlsx"
        
        'following variable is declared for sending mail purpose
        SourceWorkbook = ActiveWorkbook.Name
        
        Set this = Workbooks("Sample")
        Set wb = ActiveWorkbook
        Set ws1 = wb.Sheets("Sheet1")
        wb.Sheets.Add After:=Sheets(1)
        Set ws2 = wb.Sheets(2)
        wb.Sheets.Add After:=Sheets(2)
        Set ws3 = wb.Sheets(3)
        ws1.Name = "Sheet1"
        ws2.Name = "Sheet2"
        ws3.Name = "Sheet3"
        
        
        'Model the new excel with the requirements:
        Dim Population, Population2 As Range
        Dim lastRow As Long, firstRow As Long
        Dim sampleSize As Long
        Dim unique As Boolean
        Dim i As Long, d As Long, n As Long
        
        
        'following function perfoms all the calculations and copy and pasting        
            
            doTheJob x, y, z, num, q           
            doTheJob x, y, z, num, q 
            doTheJob x, y, z, num, q 
                
        'copy and paste the remaining sheets from the sample files
            Workbooks.Open ThisWorkbook.Path & "Sample2.xlsx"
                Sheets("Sheetx").Copy After:= _
                 Workbooks(SourceWorkbook).Sheets(6)
            Workbooks("Sample2.xlsx").Close SaveChanges:=False
        
        Application.ScreenUpdating = True
        Application.CutCopyMode = False
        ws1.Select
        wb.Close SaveChanges:=True
        End Sub

'these will make the variable available to all modules of this macro Workbook
Public SourceWorkbook As String
Public this, wb As Workbook
Public data As Range
Public output As Range
Public ws1, ws2, ws3 As Worksheet
Public LastCol As Long
Public wks As Worksheet
Public iCol As Long




'FUNCTION
Sub doTheJob(x As String, y As String, z As String, num As Integer, q As String)

    'beginning logic.
    this.Worksheets(x).Activate

Set Population = Range("a3", Range("a3").End(xlDown))
    sampleSize = this.Worksheets("SNOW Reports").Range(y).Value

Set r = Population
    lastRow = r.Rows.Count + r.Row - 1
    firstRow = r.Row


    For i = 1 To sampleSize
   Do
   
    unique = True
    n = Application.WorksheetFunction.RandBetween(firstRow, lastRow)
    
        For d = 1 To i - 1
        'wb.Sheets(z).Activate
        
          If wb.Sheets(z).Cells(d + 1, 50) = n Then
            unique = False
            Exit For
            End If
        Next d
        
          If unique = True Then
          Exit Do
          End If
        
    Loop
    
    Set data = this.Worksheets(x).Range("a" & n, Range("a" & n).End(xlToRight))
    Set output = wb.Worksheets(z).Range("A" & i + 1)
     
    output.Resize(data.Rows.Count, data.Columns.Count).Value = data.Value
        'THE NEXT LINE IS JUST FOR DELETEING LAST COLUMN PURPOSE
    wb.Worksheets(z).Cells(1, 50) = "REF COL"
    wb.Worksheets(z).Cells(i + 1, 50) = n
    
 this.Worksheets(x).Activate
    
Next i

    'delete REF COL:
       With wb.Sheets(z)
            .Columns(50).Delete
        End With
    
    'copy and paste header:
    Set data = this.Worksheets(x).Range("a2", Range("a2").End(xlToRight))
    Set output = wb.Sheets(z).Range("A1")
    
    output.Resize(data.Rows.Count, data.Columns.Count).Value = data.Value
     
'_________________________________________________________________________________________________________

'copy and paste into new sheet with recorded macro
    
   wb.Activate
   Sheets.Add(After:=Sheets(num)).Name = q
   wb.Worksheets(z).Cells.Copy Destination:=wb.Worksheets(q).Range("A1")
             
    'create columns and add color and text dinamically
    For Each wks In ActiveWindow.SelectedSheets
        With wks
            For iCol = .Cells.SpecialCells(xlCellTypeLastCell).Column To 2 Step -1
                .Columns(iCol).Insert
                With Cells(1, iCol)
                .Interior.Color = 65535
                .Value = Cells(1, iCol - 1) & " - Comparison"
                End With
            Next iCol
        End With
    Next wks

End Sub
excel foreach vba
2021-11-23 21:01:44
1

คำตอบที่ดีที่สุด

0

ถ้าผมเข้าใจสิ่งที่คุณตั้งใจว่าจะขว้างตรงต้องทำต่อไปนี้ทำสิ่งที่คุณต้องการ

  • รหัสอาจจะต่างออกเดินเข้ามาใกล้(และอาจจะทำมากกว่ามีประสิทธิภาพ)หาที่ใหญ่กว่าบคอนเท็กซ์ขนที่รู้จัก
  • อย่างไรก็ตามฉันรู้สึกถึงความนี้เป็นเวทีในการพัฒนา,ดังนั้นต้องอยู่กับวิธีการของคุณ(ไม่ว่าที่ไหนที่มีเหตุผลหน่อย).
' I suggest this goes to the top of the sub (no need for public declaration)
' Note the shorthand declaration: 'lgRow&' is the same as `lgRow as Long'
    Dim lgRow&, lgCol&, lgLastRow&
             

' Replaces the code starting with the next comment 
    'create columns and add color and text dynamically
    For Each wks In ActiveWindow.SelectedSheets
        With wks
            For lgCol = .Cells.SpecialCells(xlCellTypeLastCell).Column To 2 Step -1
                
                ' Insert a column (not sure why you're not doing this after the last column also)
                .Columns(lgCol).Insert
                
                ' Get last row with data in the column 1 to the left
                With .Columns(lgCol - 1)
                    lgLastRow = .Cells(.Cells.Count).End(xlUp).Row
                End With
                    
                ' In the inserted column:
                ' o Set cell color
                ' o Set value to corresponding cell to the left, appending ' - Comparison'
                For lgRow = 1 To lgLastRow
                    With .Cells(lgRow, lgCol)
                        .Interior.Color = 65535
                        .Value = .Offset(0, -1) & " - Comparison"
                    End With
                Next lgRow
            Next lgCol
        End With
    Next wks

ข้อ 1:ไม่แน่ใจว่าของเหตุผลแต่รหัสของคุณแทรกรอง'เปรียบเทียบคอลัมน์'หลังจากแต่ละคอลัมน์,ยกเว้นคนสุดท้ายคอลัมน์(ของคัดลอกข้อมูล). ถ้าผมเข้าใจความตั้งใจของคุอย่างถูกต้อง,ฉันเดาว่าคุณต้องการที่จะทำแบบนี้เพื่อคนสุดท้ายคอลัมน์ด้วยนะ ถ้ามันเป็นเรื่องจริง:

'change this line
    For lgCol = .Cells.SpecialCells(xlCellTypeLastCell).Column To 2 Step -1
'To:
    For lgCol = .Cells.SpecialCells(xlCellTypeLastCell).Column + 1 To 2 Step -1

ข้อที่ 2:รหัสของฉันเปลี่ยนแปลงเขียน <cell value> & " - Comparison" ต้องทุกเซลล์ที่อยู่ในแต่ละคอลัมน์,ลงไปที่เมื่อคนที่ไม่ใช่ช่องว่างเซลล์ที่อยู่ในแต่ละ'เมื่อเทียบกับ'คอลัมน์(รวมถึงช่องว่างเซลล์อยู่เหนือมัน). ถ้าคุณอยากจะทำอย่างนั้นเขียนสำหรับจำนวนแถวในการคัดลอกข้อมูลช่วง(ไม่ว่าอลล์เซลล์ว่างเปล่าหรือไม่)คุณจะ simplify รหัสโดยต้องต่อไปนี้:

' Insert this:
    lgLastRow = .Cells.SpecialCells(xlCellTypeLastCell).Row
'above line:
    For lgCol = ....

และลบอย่างลำบากขนาดนั้น

    ' Get last row with data in the column 1 to the left
    With .Columns(iCol - 1)
        lgLastRow = .Cells(.Cells.Count).End(xlUp).Row
    End With

อีกข้อ/ชี้แนะ:

  1. ขอแนะนำ Option Explicit อยู่ทางด้านบนของมอดูล(แค่บันทึกของดีบั๊กโปรแกรมเนื่องจาก typos)
  2. ไม่จำเป็นต้อง(และมันไม่ดีซ้อมที่จะประกาศ Public ตัวแปรที่ใช้ในท้องถิ่นในบ Sub หรือ Function. แทนที่จะประกาศเดียวกันในท้องถิ่น(ปกติจะอยู่ที่บนสุดของคน Sub หรือ Function).
  3. มันเป็นเรื่องดีซ้อมที่จะใช้นำหน้าตัวอักษรของตัวแปรรายชื่อที่ต้องขอข้อมูลประเภทนี้ได้นะ สามารถมีความยาวแต่ก็ได้ 1,2 หรือ 3 chars(coder นชอบ). e.g. อยู่เหนือฉันเคย lg การระบุตัวตนขนานข้อมูลประเภท. เหมือนๆกัน,ฉันใช้ in สำหรับ Integer, st สำหรับ String, rg สำหรับ Rangeเป็นต้น
2021-11-24 07:52:25

ฉันไม่แน่ใจว่า widely เคยฮังการีเครื่องหมายไว้ที่ปฎิทินปัจจุบันอยู่และมีเสมอ"แถลงนโยบายหรือไม่มันเป็นสิ่งที่ดี ฉันหมายถึง,มันสามารถเป็นประโยชน์แค่ IMO ในรายการใช้จ่ายของ readability(และบาง brevity ซึ่งเกิดเหตุที่สอง).
Chris Strickland

เป 3)สิ่งที่คุณเคยให้การสนับสนุนให้นี่เป็น"บระบบคนฮังการี"ซึ่งเป็น widely discredited. ในทางกลับกัน"Apps ฮังการี"ได้เป็นประโยชน์ ดีอ่าน (ไม่เกี่ยวกับเรื่อง vba แต่ยังคงที่เกี่ยวข้อง)
chris neilsen

@คริสติ๊กแลนด์:ตกลงนั่นสำหรับและต่อต้าน. ในภาษาที่ไหนข้อมูลประเภทคือที่แยกกำหนด(ปัจจุบันเปรียบเทียบกับ explicit)ฉัน opt สำหรับจุดมุ่งหตั้งชื่อ. ในภาษา(เหมือน vba)ที่มันเป็น explicit,ฉันอยู่ข้างบนพยายามและได้รับการพิสูจน์แล้วว่า'ถ้าฉันเจอมันทำให้ดีบั๊กโปรแกรมง่ายขึ้น
Spinner

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................