Excelmacros中string+variables的连接

在cnt1的部分,我试图使用一个整数(称为A),我将增加一个循环,所以它从shtSheet1 shtSheet5。 在我的代码中,我有shtSheet $ A来澄清我想要做什么,但是我不知道visual basic是否允许这种连接。 我只用于编码bash脚本。

Sub RunCompare() Call compareSheets("PROD", "OSA", "UAT", "INT", "TEST") End Sub Sub compareSheets(shtSheet1 As String, shtSheet2 As String, shtSheet3 As String, shtSheet4 As String, shtSheet5 As String) Dim c As Integer, j As Integer, i As Integer, mydiffs As Integer, cnt1 As Integer, cnt2 As Integer, A As Integer, B As Integer Dim noexist As Integer A = 1 B = 2 cnt1 = Worksheets(shtSheet).Cells.SpecialCells(xlCellTypeLastCell).Row cnt2 = Worksheets(shtSheetB).Cells.SpecialCells(xlCellTypeLastCell).Row 'For each cell in sheet2 that is not the same in Sheet1, color it yellow LoopStart: For i = 1 To cnt2 For j = 1 To cnt1 If ActiveWorkbook.Worksheets(shtSheet2).Cells(i, 1).Value = ActiveWorkbook.Worksheets(shtSheet1).Cells(j, 1).Value Then For c = 2 To 22 If Not ActiveWorkbook.Worksheets(shtSheetB).Cells(i, c).Value = ActiveWorkbook.Worksheets(shtSheet2).Cells(j, c).Value Then ActiveWorkbook.Worksheets(shtSheet2).Cells(i, c).Interior.Color = vbYellow mydiffs = mydiffs + 1 End If Next Exit For End If If j = cnt1 Then ActiveWorkbook.Worksheets(shtSheet2).Cells(i, 1).Interior.Color = vbRed End If Next Next A = A + 1 B = B + 1 If A <= 4 Then GoTo LoopStart End If End Sub Sub Clear_Highlights_this_Sheet() ActiveSheet.UsedRange. _ Interior.ColorIndex = xlNone End Sub Sub Clear_Highlights_All_Sheets() Dim sht As Worksheet For Each sht In sheets sht.UsedRange.Interior.ColorIndex = xlNone Next End Sub 

我更新了我的全部macros,所以更容易理解。 它比较2张,我想修改它比较5张,但以一种特定的方式,只比较1-2,2-3,3-4,4-5。

我的想法是使用A和B以及goto来做我想要的。

使用ParamArray将表名传递给你的子表单,然后遍历它们。

 Sub compareSheets(ParamArray sheets() As Variant) Dim vSheet As Variant For Each vSheet In sheets cnt = Worksheets(vSheet).Cells.SpecialCells(xlCellTypeLastCell).Row ' ... Next vSheet 

当然你也可以使用索引:

  For i = 0 to 3 cnt1 = Worksheets(sheets(i)).Cells.SpecialCells(xlCellTypeLastCell).Row ' ... Next i