首页 > 解决方案 > 如何将字符串变量转换为 VBA 上的工作表对象?

问题描述

我的问题很复杂,不知道有没有人遇到过这个问题。我想在工作表中的特定情况下将字符串(“Sheet1”、“Sheet2”等)转换为对象。有了这个,我可以使用代码Sheet3.Range("A1").Value,但我想用我需要的任何替换数字 3。

我有一个带有名为“CSV”的工作表的工作簿,在此工作表中有一个名为“t_csv”的表。此工作表用于在西班牙语和英语之间翻译一些文本。

CSV 工作表示例

我对例程进行了如下编程(如果您在选项卡栏和 VBA 索引上具有相同的顺序,它就可以工作)。

Sub tranlation()
Dim lang As String
Dim Sheet As Integer
Dim vcell As String
Dim data As String
‘Get the language from a buttom btn_esp or btn_eng  from userform called “f_Login”
  If f_Login.btn_esp.Value = True Then lang = "Español"
  If f_Login.btn_eng.Value = True Then lang = "English"
‘Bucle to get the data from table “t_csv” located in sheet “CSV” (Sheet2)
  For i = 1 To Sheet2.ListObjects("t_csv").DataBodyRange.Rows.Count
     With Sheet2.ListObjects("t_csv")
          Sheet = .ListColumns("Hoja").DataBodyRange(i).Value
          vcell = .ListColumns("Celda").DataBodyRange(i).Value
          'It uses the lang variable to choose the respective column on table "t_csv"
          data = .ListColumns(lang).DataBodyRange(i).Value
          ‘write the text in corresnponding cell (code to be changed)
          **Sheets(Sheet).Range(vcell).Value = dato**
     End With
  Next i
End Sub

函数 Sheets() 或 Worksheets() 的问题在于它们调用工作表,对工作簿标签栏上的工作表进行编号(从左到右)。但我想用 VBA 工作表编号调用工作表,因为我在工作簿的标签栏上有特定的顺序。

在我的标签栏中订购

在我的 VBA 索引中排序

我尝试使用这样的代码(它不起作用,因为“Sheet”&Sheet是一个字符串变量):

Dim SheetIndex As Worksheet
     Set SheetIndex = “Sheet” & Sheet
     SheetIndex.Range(vcell).value = data

但如果我使用下一个代码:

Dim HojaIndex As Worksheet
     Set HojaIndex = Sheet3
     HojaIndex.Range(vcell).value = data

此代码有效,但我想用从表“t_csv”中获得的任何数字自动替换数字 3。

我猜解决方案是使用函数CallByName,但我不明白如何使用它。我看到了一些例子并尝试了,但我无法让它工作。

------- 更新 ------- 更新 ------- 更新 ------- 更新 ------- 更新 ------- 更新 -- -----

感谢@蒂姆·威廉姆斯。我可以解决我的问题,我在他的回答中做了一些修改。如果有人有同样的问题,我会分享最终答案。

Function SheetByCodeName(SheetCodeName As String) As Worksheet
Dim ws As Worksheet
  For Each ws In ThisWorkbook.Worksheets
    If ws.CodeName = SheetCodeName Then
      Set SheetByCodeName = ws
      Exit Function
    End If
  Next ws
End Function

Sub tranlation()
Dim lang As String
Dim Sheet As String    'Update variable type from Integer to String
Dim vcell As String
Dim data As String
Dim SheetName As Worksheet    'New variable to set the sheet name
‘Get the language from a buttom btn_esp or btn_eng  from userform called “f_Login”
  If f_Login.btn_esp.Value = True Then lang = "Español"
  If f_Login.btn_eng.Value = True Then lang = "English"
‘Bucle to get the data from table “t_csv” located in sheet “CSV” (Sheet2)
  For i = 1 To Sheet2.ListObjects("t_csv").DataBodyRange.Rows.Count
     With Sheet2.ListObjects("t_csv")
          Sheet = "Sheet" & .ListColumns("Hoja").DataBodyRange(i).Value
          vcell = .ListColumns("Celda").DataBodyRange(i).Value
          'It uses the lang variable to choose the respective column on table "t_csv"
          data = .ListColumns(lang).DataBodyRange(i).Value
          ‘*-NEW CODE*- write the text in corresnponding cell
          Set Sheet = SheetByCodeName(Sheet)
          Sheet.Range(vcell).Value = data
          ‘*-NEW CODE*- write the text in corresnponding cell
     End With
  Next i
End Sub

标签: excelvba

解决方案


Sub tester()

    Debug.Print SheetByCodename("xxx").Name

End Sub


'Return a worksheet from its codeName (or Nothing if not match)
'  Defaults to ThisWorkbook if a workbook is not passed
Function SheetByCodename(nm As String, Optional wb As Workbook = Nothing) As Worksheet
    Dim ws As Object
    If wb Is Nothing Then Set wb = ThisWorkbook
    For Each ws In ThisWorkbook.Worksheets
        If ws.CodeName = nm Then
            Set SheetByCodename = ws
            Exit Function
        End If
    Next ws
End Function

推荐阅读