如何在excel中通过单元格值更新vba中工作表名称

k10s72fa  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(166)

我目前有以下代码在VBA这是工作正常,但我需要一种方法来改变“工作表(数组(……)”的一部分,使其更加用户友好。
我想有一个链接到一个单元格或单元格范围,使用户可以更新它,而不需要进入VBA

Sub ExportAsPDF()
   Dim FolderPath As String
   
   Sheets(Array("4", "5", "7", "8", "9", "10", "11", "12", "14", "15", "17", "18", "19", _
                "21", "22", "24", "25", "29", "30", "33", "34", "35", "36", "37", "38", _
                "39", "40", "41", "43", "44", "47", "48", "50", "51", "52", "54", "55", _
                "57", "59", "60", "61", "62", "63", "64", "67", "68", "69", "70", "72", _
                "75", "76", "77", "79", "80", "81", "82", "83", "84", "85", "86", "87", _
                "88", "89", "90", "92", "93", "94", "101", "102", "104", "105", "106", _
                "107", "108", "109")).Select
                
   ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=Range("A1") + "All FV.pdf", _
                                   openafterpublish:=False, ignoreprintareas:=False
   Sheets("Figures").Select
   ActiveWorkbook.Save
   MsgBox "All PDF's have been successfully exported."
End Sub

字符串
在导出过程中未选择“数字”工作表,并且数字不连续,有些数字丢失。
所有选定工作表上的单元格A1都包含在同一工作表上运行的另一个代码的目标文件夹。
图纸编号列表已经在“图”表中,是否有任何方法可以使用?A4:A78
TIA
我还没有尝试过任何东西,因为不确定该用什么。

z3yyvxxp

z3yyvxxp1#

您可以在任何列中保留工作表名称(即列C)。

Sub ExportAsPDF()
    Dim FolderPath As String
    Dim shtNames, sht As Worksheet
    Set sht = Sheets("Figures")
    shtNames = Application.Transpose(sht.Cells(1, 3).CurrentRegion.Value)
    For i = 1 To UBound(shtNames)
        shtNames(i) = CStr(shtNames(i)) 'Covert Sheet name to a string
    Next
    Sheets(shtNames).Select
    FolderPath = "C:\Users\Trainee1\Desktop\PDFs"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sht.Range("A1") + "All FV.pdf", _
        openafterpublish:=False, ignoreprintareas:=False
    sht.Select
    ActiveWorkbook.Save
    MsgBox "All PDF's have been successfully exported."
End Sub

字符串

8gsdolmq

8gsdolmq2#

您可以在工作表上有一个工作表列表,并使用它来创建工作表数组

Sub MakeArraySheets()
    Dim sh As Worksheet
    Dim ArraySheets() As String
    Dim x As Variant
    Dim ws As Worksheet
    Dim c As Range, rng As Range
    
    Set ws = Sheets("SheetList") 'name of sheet with the sheet names in column A
    Set rng = ws.Range("A1:A" & ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
    
    For Each c In rng.Cells

        ReDim Preserve ArraySheets(x)
        ArraySheets(x) = c
        x = x + 1
        
    Next c

    Sheets(ArraySheets).Select

'    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
'                                    ThisWorkbook.Path & "\" & ThisWorkbook.Name, _
'                                    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
'                                    IgnorePrintAreas:=False, OpenAfterPublish:=True


End Sub

字符串

相关问题