当前位置: 首页 > 知识库问答 >
问题:

VBA Excel根据单元格值按正确顺序自动操作新表

那绪
2023-03-14

关于我的最后一个问题:

VBA Excel根据单元格值自动填充新表以增加单元格

我希望有此工作表填充在正确的顺序,因为我有超过1个工作表名称填充。

Sub Sheetwithnames2()
      Dim wsr As Worksheet, wso As Worksheet
      Dim i As Long, j As Long, xCount As Long, yCount As Long
      Dim SheetNames As Variant, LSheetNames As Variant   'This needs to be variant
      Dim sheetname As Variant, lsheetname As Variant
      Dim newsheet As Worksheet, lnewsheet As Worksheet, onewsheet As Worksheet, olnewsheet As Worksheet
      Dim lr As Long, lro As Long

      Set wsr = ThisWorkbook.Sheets("Vetro Area Map 1")
      Set wso = ThisWorkbook.Sheets("Area Map Op 1")

      lr = ThisWorkbook.Sheets("Frontsheet").Cells(Rows.Count, 4).End(xlUp).Row 'Get last row
      lro = ThisWorkbook.Sheets("Frontsheet").Cells(Rows.Count, 5).End(xlUp).Row 'Get last row
      'including empty cells either, but not creating new sheets for them
      SheetNames = ThisWorkbook.Sheets("Frontsheet").Range("D123:D" & lr)  

      SheetNames = Application.Transpose(Application.Index(SheetNames, , 1)) 'Converts the 2d array into a 1d array

      LSheetNames = ThisWorkbook.Sheets("Frontsheet").Range("E123:E" & lro)

      For i = 1 To ActiveWorkbook.Sheets.Count
      If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
      Next

      For j = 1 To ActiveWorkbook.Sheets.Count
      If InStr(1, Sheets(j).name, "Op") > 0 Then yCount = yCount + 1
      Next

      For Each sheetname In SheetNames
      wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount)
      Set newsheet = Sheets(wsr.Index + xCount)
      newsheet.name = "Vetro Area Map " & sheetname & " 1"
      xCount = xCount + 1 'Preserve order of sheets from range
      wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
      Set onewsheet = Sheets(wso.Index + yCount)
      onewsheet.name = "Area Map Op " & sheetname & " 1"
      yCount = yCount + 1

      Next

End Sub

 For i = 1 To ActiveWorkbook.Sheets.Count
 If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
 Next

 For j = 1 To ActiveWorkbook.Sheets.Count
 If InStr(1, Sheets(j).name, "Op") > 0 Then yCount = xCount + 2
 Next

 For Each sheetname In SheetNames
 wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount)
 Set newsheet = Sheets(wsr.Index + xCount)
 newsheet.name = "Vetro Area Map " & sheetname & " 1"
 xCount = xCount + 1 'Preserve order of sheets from range
 wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)
 Set onewsheet = Sheets(wso.Index + yCount)
 onewsheet.name = "Area Map Op " & sheetname & " 1"
 yCount = yCount + 1

 Next

下标超出范围

     wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + yCount)

Te这种方法会出现相同的错误:

  For i = 1 To ActiveWorkbook.Sheets.Count
  If InStr(1, Sheets(i).name, "Vetro") > 0 Then xCount = xCount + 1
  Next


 For Each sheetname In SheetNames
 wsr.Copy After:=ActiveWorkbook.Sheets(wsr.Index - 1 + xCount * 2 + 1)
 Set newsheet = Sheets(wsr.Index + xCount)
 newsheet.name = "Vetro Area Map " & sheetname & " 1"
 xCount = xCount + 1 'Preserve order of sheets from range
 wso.Copy After:=ActiveWorkbook.Sheets(wso.Index - 1 + xCount * 2 + 2)
 Set onewsheet = Sheets(wso.Index + xCount)
 onewsheet.name = "Area Map Op " & sheetname & " 1"
 xCount = xCount + 1

 Next

共有1个答案

魏朗
2023-03-14

我可能是这样做的:

'...
'...
For Each sheetname In SheetNames
     CopyTemplates sheetname 
Next
'...
'...

复印纸张的方法:

Sub CopyTemplates(newName As String)
    'these are the template worksheets
    Const WS_A As String = "Vetro Area Map 1"
    Const WS_B As String = "Area Map Op 1"
    
    Dim wsLast As Worksheet, i As Long, ws As Worksheet
    
    'find the last worksheet which looks like one of our templates
    '  (or a copy of one of the templates)
    For i = 1 To ThisWorkbook.Worksheets.Count
        Set ws = ThisWorkbook.Worksheets(i)
        If ws.Name Like "Vetro Area*" Or ws.Name Like "Area Map*" Then
            Set wsLast = ws
        End If
    Next i
    'copy the templates after the "last" copy and rename
    With ThisWorkbook.Worksheets
        .Item(Array(WS_A, WS_B)).Copy after:=wsLast
        .Item(wsLast.Index + 1).Name = "Vetro Area Map " & newName & " 1"
        .Item(wsLast.Index + 2).Name = "Area Map Op " & newName & " 1"
    End With

End Sub
 类似资料:
  • 我在更新JTable的单元格值时遇到了一个问题。我想做的是在从JTable中选择一个特定的单元格之后,我应该能够进行编辑,并且该操作必须反映后端的数据库。我在用HSQL。我的表有4列,有一个PK。请给我一个替代和/或提供一些代码,替换的*。我是新来的,只是个初学者。

  • 有人能帮忙吗?我有一个现有代码,该代码当前根据V列中的单元格值(上诉记录)将整行数据从一张表(质量日志)复制到另一张表(上诉日志)。 之前,它已经从原始(质量日志)表中删除了该行,但我现在希望更改代码,以便将单元格值更改为(正在上诉),然后将其移动到下一页(上诉日志)。 请参阅下面我的代码。我已用**表示我试图更改代码

  • 问题内容: 我的站点读取一个XML文件,该文件包含数据表的信息(值)。我使用CSS设置表格样式,并且一切正常。 为了获得更好的用户体验,我想知道是否有可能根据其值动态更改每个单元格的背景颜色? 例如: 每个包含小于5的数字的单元格都有红色背景色; 每个大于等于“ 5”的单元格具有绿色背景色。 我对此的第一个解决方案是使用Javascript-但我想知道是否有办法仅使用CSS样式来解决此问题? 问题

  • 我不是脚本大师,我从这里和那里混合了一点代码,并想出了一个非常简洁的代码来根据另一个单元格值清除所有行,然后将它们重新组织为有序而不会留下空白行。 问题是,代码从第一行开始获取值,其中包含我想要保护的公式。 你能帮我解决这个问题吗? 我希望代码从第6行开始工作并向前。 非常感谢你:)

  • 我想在单击该行中的一个按钮后更新primefaces数据表中的一列。 我的jsf页面: 我的访客Bean: visitService将间隙从OK更改为NOK,但我的datatables列没有更改。有什么想法吗? 表面5.2 我已经研究了如何只更新primefaces数据表中的特定单元格,但这似乎并没有解决它。

  • 我有一个工作表,我需要根据单元格值删除行。。 要检查的单元格位于A列中。。 如果单元格包含“-”。。删除行 我找不到这样做的方法...我打开一个工作簿,将所有内容复制到另一个工作簿,然后删除整个行和列,但必须根据单元格值删除特定的行。 这里需要帮助。 更新 我拥有的数据样本