将用户窗体的结果插入标题

Insert result of userform into heading

我需要将用户表单的结果插入 header,但我不知道如何将我的代码组合到最终项目中:照片和代码下面

我需要 header 确定按钮 1:根据我的 header 代码根据我想要的 sheet 格式化 header,在这种情况下 sheet 称为金属

2。在它说 "Summary of Metals in "_____" <-(Soil/Sediment...etc 取决于选中哪个框)

3。将输入的文本插入到用户窗体文本框中。 (尚未编写代码)。

最终结果。 = 对于这个特定的 sheet 将是 header 说 "Summary of Metals in Soil, 100 Main Street, USA"

感谢所有帮助!

下面的代码将结果临时插入 A1 私人潜艇 Cancel_Click() Me.Hide 结束子

Private Sub OK_Click()

'--- Insert the correct matrix Wording ---
    If Check_Soil.Value = -1 Then
    Range("A1").Value = "Soil"

ElseIf Check_Sediment.Value = -1 Then
    Range("A1").Value = "Sediment"

ElseIf Check_Ground_Water.Value = -1 Then
    Range("A1").Value = "Ground Water"

ElseIf Check_Surface_Water.Value = -1 Then
    Range("A1").Value = "Surface Water"
End If
Me.Hide

MsgBox "Completed", vbOKOnly
End Sub

Private Sub Check_Soil_Click()

'--- Checks if the Soil Button is Clicked ---
If Check_Soil.Value = True Then
    Check_Surface_Water.Value = False
    Check_Ground_Water.Value = False
    Check_Sediment.Value = False
Else
    Check_Soil.Enabled = True
End If
End Sub

Private Sub Check_Surface_Water_Click()

'--- Checks if the Surface Water Button is Clicked ---
If Check_Surface_Water.Value = True Then
    Check_Soil.Value = False
    Check_Ground_Water.Value = False
    Check_Sediment.Value = False
Else
    Check_Surface_Water.Enabled = True
End If
End Sub

Private Sub Check_Ground_Water_Click()

'--- Checks if the Ground Water Button is Clicked ---
If Check_Ground_Water.Value = True Then
    Check_Surface_Water.Value = False
    Check_Soil.Value = False
    Check_Sediment.Value = False
Else
    Check_Ground_Water.Enabled = True
End If
End Sub

Private Sub Check_Sediment_Click()

'--- Checks if the Sediment Button is Clicked ---
If Check_Sediment.Value = True Then
    Check_Surface_Water.Value = False
    Check_Soil.Value = False
    Check_Ground_Water.Value = False
Else
    Check_Sediment.Enabled = True
End If
End Sub

我的其他代码:

SubSelect_Correct_Sheet()
' Select_Correct_Sheet Macro
Sheets("Metals").Select
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .PrintTitleRows = ""
    .PrintTitleColumns = ""
End With
Application.PrintCommunication = True
ActiveSheet.PageSetup.PrintArea = ""
Application.PrintCommunication = False
With ActiveSheet.PageSetup
    .LeftHeader = "&""Arial,Bold""Summary of Metals in "
    .CenterHeader = ""
    .RightHeader = ""
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""
    .LeftMargin = Application.InchesToPoints(0.7)
    .RightMargin = Application.InchesToPoints(0.7)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
    .PrintHeadings = False
    .PrintGridlines = False
    .PrintComments = xlPrintNoComments
    .PrintQuality = 600
    .CenterHorizontally = False
    .CenterVertically = False
    .Orientation = xlPortrait
    .Draft = False
    .FirstPageNumber = xlAutomatic
    .Order = xlDownThenOver
    .BlackAndWhite = False
    .Zoom = 100
    .PrintErrors = xlPrintErrorsDisplayed
    .OddAndEvenPagesHeaderFooter = False
    .DifferentFirstPageHeaderFooter = False
    .ScaleWithDocHeaderFooter = True
    .AlignMarginsHeaderFooter = True
    .EvenPage.LeftHeader.Text = ""
    .EvenPage.CenterHeader.Text = ""
    .EvenPage.RightHeader.Text = ""
    .EvenPage.LeftFooter.Text = ""
    .EvenPage.CenterFooter.Text = ""
    .EvenPage.RightFooter.Text = ""
    .FirstPage.LeftHeader.Text = ""
    .FirstPage.CenterHeader.Text = ""
    .FirstPage.RightHeader.Text = ""
    .FirstPage.LeftFooter.Text = ""
    .FirstPage.CenterFooter.Text = ""
    .FirstPage.RightFooter.Text = ""
End With
Application.PrintCommunication = True
End Sub

试试这两个变化

1- 将您的 OK_Click 更改为:

Private Sub OK_Click()
    Dim headerText As String
    Select Case True
        Case Check_Soil.value: headerText = "Soil"
        Case Check_Sediment.value: headerText = "Sediment"
        Case Check_Ground_Water.value: headerText = "Ground Water"
        Case Check_Surface_Water.value: headerText = "Surface Water"
    End Select

    headerText = headerText & ", " & TextBox1.value ' <-- assuming this is the name of your textbox
    FormatHeader headerText ' <-- now invoke the header formatting sub with parameter
    MsgBox "Completed"
End Sub

2- 更改您格式化 header 的例程(旧名称是 Select_Correct_Sheet 我给了它一个新名称,FormatHeader)。我应该在其声明中有一个参数 text 并且只有一行会更改,即分配文本以添加提供的参数的那一行。

Sub FormatHeader(text As String)
    ' ....
    .LeftHeader = "&""Arial,Bold""Summary of Metals in " & text '<-- add the text parameter into header here
    ' ....
End Sub