2014-04-26 48 views
1

我想用一個用戶表單替換一個msgbox,但是當我顯示用戶表單時,它沒有像在msgbox中那樣的文本框中的數據,是否需要向文本框添加一些代碼?這裏是我所做的vba用用戶表單替換msgbox

Sub unknown() 
    Dim iListCount As Integer 
    Dim iCtr As Integer 



    ' Turn off screen updating to speed up macro. 
    Application.ScreenUpdating = False 

    ' Get count of records to search through (list that will be deleted). 
    iListCount = Sheets("sheet2").Cells(Rows.Count, "A").End(xlUp).Row 

    ' Loop through the "master" list. 
    For Each x In Sheets("Sheet1").Range("A1:A" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row) 
    ' Loop through all records in the second list. 
     For iCtr = iListCount To 1 Step -1 
      ' Do comparison of next record. 
      ' To specify a different column, change 1 to the column number. 
      If x.Value = Sheets("Sheet2").Cells(iCtr, 1).Value Then 
       ' If match is true then delete row. 
       Sheets("Sheet2").Cells(iCtr, 1).EntireRow.Delete 
      End If 
     Next iCtr 
    Next 
    Application.ScreenUpdating = True 
    If Application.WorksheetFunction.CountA(Range("A:A")) = 0 Then 
     Exit Sub 
    End If 
    For Each r In Intersect(Range("A:A"), ActiveSheet.UsedRange) 
     If r.Value > "" Then 
      msg = msg & vbCrLf & r.Value 
     End If 
    Next r 
    ' this msgbox works ok 
    'MsgBox msg, vbOKOnly, "Unknown servers" 

    frmunknownservers.Show 
    Textunknownservers.Text = msg 

End Sub 

回答

1

我猜Textunknownservers是表單中的標籤的名稱?您需要使用它所在的表單對其進行限定。另外,除非在表單上將ShowModal設置爲false,否則您需要在顯示錶單之前設置文本。否則,直到用戶關閉表單後纔會設置文本。

frmunknownservers.Textunknownservers.Text = msg 
frmunknownservers.Show 

對於更好的封裝,可以創建表格中的一個方法,以顯示形式和顯示消息。這個小組會去你的表單的代碼:

Public Sub ShowWithMessage(msg As String) 
    Textunknownservers.Text = msg 
    Me.Show 
End Sub 

然後顯示你的表單,你會寫這個。

frmunknownservers.ShowWithMessage "Hello, world"