Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

excel - How do I set a variable in VBA to equal the title of a workbook? And how do I allow it change based on the cell it references?

I am trying to have a cell (A37) equal a variable in VBA and have that variable change which workbook it references based on Cell A37. In A37 I plan to change the name of the workbook to change which workbook it pulls data from.

Cell A37 will have the name of the workbook I want to output from, and Cell A38 will have the name of the worksheet I want to output from (within the workbook from Cell A37).

Here is the code I wrote but I keep receiving errors when I set the OPEXsht equal to a cell. (I'm very new at VBA coding)

Sub CellName ()
Dim OPEXwbk As String
Dim OPEXsht As String

OPEXsht = Workbooks("VBA TRIAL.xlsb").Sheets("Sheet2").Range("A38")

OPEXwbk = Workbooks("VBA TRIAL.xlsb").Sheets("Sheet2").Range("A37")

Workbooks(OPEXwbk).Sheets(OPEXsht).Range("B22").Copy

Workbooks("VBA TRIAL.xlsb").Sheets("Sheet2").Range("A42").PasteSpecial Paste:=xlValues

End Sub

Any help would be greatly appreciated.

question from:https://stackoverflow.com/questions/66068785/how-do-i-set-a-variable-in-vba-to-equal-the-title-of-a-workbook-and-how-do-i-al

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add some error checking at each stage of the process.

Option Explicit

Sub CellName()

    ' This macro in VBA TRIAL.xlsb
    Dim wb As Workbook, ws As Worksheet
    Dim sOPEXwbk As String, sOPEXsht As String
    Dim msg As String, bOK As Boolean
   
    With ThisWorkbook.Sheets("Sheet2")
        sOPEXwbk = .Range("A37") ' book
        sOPEXsht = .Range("A38") ' sheet
    End With
   
    ' check not blank
    If Len(sOPEXwbk) = 0 Or Len(sOPEXsht) = 0 Then
        MsgBox "No workbook or worksheet configured on Sheet2 A37,A38", vbCritical
        Exit Sub
    End If
   
    ' check workbook open
    For Each wb In Workbooks
        msg = msg & vbCrLf & wb.Name
        If wb.Name = sOPEXwbk Then bOK = True
    Next
   
    ' workbook not open
    If bOK = False Then
        MsgBox "'" & sOPEXwbk & "' not in list" & msg, vbCritical, "Workbook not open"
        Exit Sub
    Else
        ' check worksheets
        msg = ""
        bOK = False
        For Each ws In Workbooks(sOPEXwbk).Sheets
            msg = msg & vbCrLf & ws.Name
            If ws.Name = sOPEXsht Then bOK = True
        Next
        
        ' worksheet not found
        If bOK = False Then
            MsgBox "'" & sOPEXsht & "' not in list" & msg, vbCritical, "Sheet Not Found"
            Exit Sub
        Else
            ' OK
            ThisWorkbook.Sheets("Sheet2").Range("A42") = Workbooks(sOPEXwbk).Sheets(sOPEXsht).Range("B22").Value
            MsgBox "Copied B22 from " & sOPEXwbk & " Sheet " & sOPEXsht, vbInformation
        End If
    End If

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...