【问题标题】:Insert result of userform into heading将用户表单的结果插入标题
【发布时间】:2017-02-13 04:29:09
【问题描述】:

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

我需要标题 OK 按钮 1:根据我想要的表格,根据我的标题代码格式化标题,在本例中称为金属

2。在它说“_____”中的金属摘要之后

3。将输入的任何文本插入用户表单文本框中。 (还没有写代码)。

最终结果。 = 对于这张特殊的表格,标题将是“Summary of Metals in Soil, 100 Main Street, USA”

感谢所有帮助!

下面的代码只是临时将结果插入 A1 私人子 Cancel_Click() 我隐藏 结束子

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

【问题讨论】:

    标签: excel vba formatting userform


    【解决方案1】:

    试试这两个改变

    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- 更改标题格式的常规(旧名称是 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
    

    【讨论】:

    • ....谢谢!你的解决方案是完美的,比我写的更有说服力。我要向你扔一个曲线球.....如果我有多个名称不同的工作表,可以说 sheet1、sheet2、sheet 3。有没有办法添加代码来表示。土壤中 Sheet1 的摘要”然后下一张是“土壤中 Sheet2 的摘要”......等等?......我不能强调我非常感谢你的帮助!
    • 会在OK_Click:headerText = "Summary of &amp; ActiveSheet.name &amp; " in " &amp; headerText &amp; ", " &amp; TextBox1.value '中生成所有标题文本。然后在Sub FormatHeader.LeftHeader = "&amp;""Arial,Bold""" &amp; text
    • 你的答案太棒了
    • ....我的好朋友 ash,如果我的工作表名称是“LOL”但我希望标题部分说“Laugh out Loud”怎么办?
    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多