意想不到的行为的"每周在则activewindow.SelectedSheets",它影响到更多柱,它应该是

0

的问题

我没有这个代码的工作得很好除最后一部分:

该行为的最后部分应"。内部。颜色"和"。值"的影响,直到最后一个填充柱,而不是它影响到第一小区的许多其它列。 任何想法?

  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" 所有单元中的每个列,下到最后一个非空单元中的每一个'相比,'栏(包括空白细胞以上)。 如果你想要做的是写中的所有行复制数据的范围(是否细胞都是空白或不)你可以简化通过把以下:

' 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 在顶部的所有模块(只是节省了很多的调试由于打字错误)
  2. 有没有必要(而这不良好做法)宣布 Public 变量只使用本地给予 SubFunction. 相反,声明同本地(通常是在顶部 SubFunction).
  3. 这是很好的做法,使用的主角的变量名称的身份的数据类型。 可以是任何长度,但是通常1、2或3chars(编码器的偏好)。 如上所述,我用的 lg ID长的数据类型。 同样,我用的 in 对于 Integer, st 对于 String, rg 对于 Range等等。
2021-11-24 07:52:25

我不知道该怎样广泛地使用匈牙利表示法目前,总是有一个辩论是否这是一件好事。 我的意思是,这可能是有用的,只是国际海事组织费用的可读性(和一些简洁起见,这是二级).
Chris Strickland

再3)什么是你在提倡这里是"系统匈牙利",这是广泛地名誉扫地。 另一方面,"应用匈牙利语"可能是有用的。 一个很好的阅读 (不vba,但是仍然有关)
chris neilsen

@克里斯*特里克兰:商定有支持和反对。 在语言数据的类型是隐含的(相对明确),我选择的目的命名。 在语言(样vba)它是明确的,我坚持使用'尝试和证明',因为我找到它使得调试更加容易。
Spinner

其他语言

此页面有其他语言版本

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