如何将单元格的内容用作必须是字符串的变量

How can I use the contents of a cell as a variable which must be a string

我正在使用下面的代码刷新 HFM 工作表并更新 POV 中的两个变量,年份和期间。

我需要将 "Period#Mar" 中的 'Mar' 替换为单独工作表中单元格的内容(当前为 'Mar',但每个月都会更改)。

我尝试创建一个范围对象,但是 HypSetPOV 函数需要一个字符串,而不是一个范围。所以我不确定我是否需要以某种方式将我的范围转换为字符串,或者做一些不同的事情。

没有范围的代码(将周期更新为 Mar):

Sub HFM_Refresh()

Dim SheetName As String
Dim sts As Long
SheetName = "1 - PII PL Reporting Month"

ActiveWorkbook.Worksheets(SheetName).Visible = True
ActiveWorkbook.Worksheets(SheetName).Activate
ActiveWorkbook.Worksheets(SheetName).Range("A1").Activate

X = HypSetPOV(SheetName, "Year#2019", "Period#Mar")

sts = HypMenuVRefresh()

If sts <> 0 Then
MsgBox "Error - refresh not complete on worksheet " & SheetName
End If

End Sub

我尝试使用范围的代码(根本不更新周期):

Sub HFM_Refresh()

Dim SheetName As String
Dim sts As Long
Dim MonthMember As Range
SheetName = "1 - PII PL Reporting Month"
Set MonthMember = ActiveWorkbook.Worksheets("Update").Range("D9")

ActiveWorkbook.Worksheets(SheetName).Visible = True
ActiveWorkbook.Worksheets(SheetName).Activate
ActiveWorkbook.Worksheets(SheetName).Range("A1").Activate

X = HypSetPOV(SheetName, "Year#2019", "Period#MonthMember")

sts = HypMenuVRefresh()

If sts <> 0 Then
MsgBox "Error - refresh not complete on worksheet " & SheetName
End If

End Sub

提前致谢:)

你可以这样做:

X = HypSetPOV(SheetName, "Year#2019", _
              "Period#" & ActiveWorkbook.Sheets("Udatate").Range("D9").Value)

你可以使用 REPLACE():

Sub HFM_Refresh()
Dim ws As Worksheet

Set ws = Worksheets("1 - PII PL Reporting Month")

ws.Visible = True

Dim replCell As Range, targetRng As Range
' replCell will be the cell you want to use
' to find the replacement.
Set replCell = ws.Range("A1")

' targetRng is the range of cells you want to
' replace `Period#Mar` with (or any other such
' string, like `Period#May`, etc.

Set targetRng = ws.Range("B1:B10")

Dim cel As Range
targetRng.Replace "Period#*", "Period#" & replCell.Value, xlPart

End Sub