2015-05-06 62 views
1

我使用了我在下面附加的鏈接中回覆的代碼,我遇到了一些問題,請你幫助我嗎?VBA對話框在不同的工作簿掛起時選擇範圍

VBA Dialog box to select range in different workbook

當我在RefEdit中單擊以能夠選擇我想用它掛起來,我得到當我點擊說,還有另外一個窗口的地方開一個聲音的範圍更高的優先級,但我找不到它。

基本上,我想要做的是從一個工作簿複製指定的列並將其粘貼到我的原始工作簿中的工作表上。我知道我的「copyButton_Click()」編碼是不正確的,但我無法深入調試來解決它。以下是我有:

MODULE 1:

Sub extractData() 
    Dim FName As Variant 
    Dim wb As Workbook 
    Dim destSheet As String 
    ' 
    Application.ScreenUpdating = False 
    destSheet = "NewData" 
    ' 
    'Selects and clears data 
    Sheets(destSheet).Select 
     Range("A2:I12000").Select 
     Selection.delete Shift:=xlUp 
     Range("A2").Select 
    ' 
    'Prompts user to select updated ILP file to copy data from: 
    FName = Application.GetOpenFilename(filefilter:="Excel Files (*.xls;*.xlsx;*.xlsm), *.xls;*.xlsx;*.xlsm") 
     If FName <> False Then 
     Set wb = Workbooks.Open(FName) 
    ' 
    ExtractCompareUserForm.Show vbModeless 
    ' 
     End If 

    Application.ScreenUpdating = True 

    End Sub 

窗體CODE:

Private Sub UserForm_Initialize() 
     Dim wb As Workbook 

    '~~> Get the name of all the workbooks in the combobox 
     For Each wb In Application.Workbooks 
      ComboBox1.AddItem wb.Name 
     Next 

     ComboBox1 = ActiveWorkbook.Name 
    End Sub 

    '~~> This lets you toggle between all open workbooks 
    Private Sub Combobox1_Change() 
     If ComboBox1 <> "" Then Application.Workbooks(ComboBox1.Text).Activate 

     Label1.Caption = "": RefEdit1 = "" 
    End Sub 

    '~~> This lets you choose the relevant range 
    Private Sub RefEdit1_Change() 
     Label1.Caption = "" 

     If RefEdit1.Value <> "" Then _ 
     Label1.Caption = "[" & ComboBox1 & "]" & RefEdit1 
    End Sub 

    Private Sub copyButton_Click() 
    Dim addr As String 
    ' 
    addr = RefEdit1.Value 
    ' 
    'Copy Data: 
     UserForm1.addr = Selection.Address 
     addr.Copy 

    End Sub 

    Private Sub PasteButton_Click() 
    Dim destSheet As String 
    ' 
    Workbooks(2).Close SaveChanges:=False 
    ' 
    '  Now, paste to working workbook: 
      Sheets("NewData").Activate 
      Range("B2").Select 
      ActiveSheet.Paste 
      Application.CutCopyMode = False 
     Unload Me 
     ' 
    Call CopyData 

    End Sub 

這裏是它掛起來,沒有讓我點擊任何東西。

回答

1

你必須關閉屏幕更新,你需要顯示您的用戶窗體之前激活它,在Sub extractData()末:

您有:

ExtractCompareUserForm.Show vbModeless 
End If 
Application.ScreenUpdating = True 
End Sub 

切換到:

Application.ScreenUpdating = True 
    ExtractCompareUserForm.Show vbModal 
End If 
End Sub 
+0

問題仍然存在,當我嘗試臨屋區t @ R3uK – JFro777

+0

嗯...我的壞,我忘了切換...:/請參閱編輯,但您可能已經嘗試過 我想你也嘗試了一個簡單的'ExtractCompareUserForm.Show vbModal'? – R3uK

+0

AHH是的! ExtractCompareUserForm.Show vbModal修復了這個問題!非常感謝你@ R3uK 如果你想調整上面的編輯,我會除了答案。 – JFro777

相關問題