2015-04-02 35 views
0

我有我有命令按鈕和輸入文本框的用戶窗體。複製範圍到另一個工作表,並從輸入框中插入名稱與此副本

我想從一張工作表中複製指定範圍,然後命名並粘貼到另一張工作表中。 我的代碼看起來像這樣,但它不起作用。

Private Sub CommandButton1_Click() 
Dim i, LastRow 
Dim ws As Worksheet 
Dim k As Integer 
Set ws = Worksheets("Vali") 
LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 
For i = 4 To LastRow 'find fulfiled rows 
If Sheets("Sheet1").Cells(i, "D").Value = 1 Then 
Sheets("Sheet1").Range(Cells(i, "B"), Cells(i, "D")).Copy Destination:=Sheets("Vali").Range("A" & Rows.Count).End(xlUp).Offset(1) 
End If 
Next i 
Dim i As Integer 
'Next we use a looping process 'We start the loop from row 2 because our worksheet has headers in row 1 
For k = 2 To 100 
'Now we define a condition that only if there is data under the headers ItemID, Description, 
If Cells(k, "A").Value <> "" And Cells(k, "B").Value <> "" And Cells(k, "C").Value <> "" And Cells(k, "D").Value <> "" And Cells(k, "E").Value = "" Then 
Cells(k, "D").Value = Me.txtname.Value 
End If 
Next 
Range("E:E").EntireColumn.AutoFit 
Range("B4:D21").ClearContents 'clear content on previos sheet, from where we made copy 
ActiveWorkbook.Save 
ValiFinish.Hide 
End Sub 
+2

PLZ使用{}圖標以可讀的方式重新排列代碼。當錯誤提示時它說什麼?並在哪一行? – R3uK 2015-04-02 13:07:35

+0

謝謝,我無法理解,有些時候它說無效使用了Me關鍵字,但有時它以某種方式使其動作,但沒有數據輸入到E列中。還有關於如果沒有阻止的話。我認爲IF條件不是故意的。請考慮命令按鈕在工作表1上。 – 2015-04-02 19:51:13

+0

命令按鈕僅從一張紙複製到另一張,但當我在輸入框中輸入名稱時,名稱未插入。 – 2015-04-03 07:30:24

回答

0

不知道你是想用你的測試做你第二個循環,因爲沒有紙基準的,所以我選擇,讓我知道這是不是

Private Sub CommandButton1_Click() 
    Application.ScreenUpdating = False 
    Dim LastRow As Double 
    Dim ws As Worksheet 
    Dim Wv As Worksheet 
    Dim k As Integer 
    Dim i As Integer 
    Dim Ti() 
    ReDim Ti(0) 
    Dim StartPaste As Double 
    Dim EndPaste As Double 
    Dim PastedRange As String 

    Set ws = Worksheets("Sheet1") 
    Set Wv = Worksheets("Vali") 

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row 
StartPaste = Wv.Range("A" & Rows.Count).End(xlUp).Offset(1).Row 

For i = 2 To LastRow 
    If ws.Cells(i, "D").Value = 1 Then 

     ws.Range(ws.Cells(i, "A"), ws.Cells(i, "D")).Copy _ 
      Destination:=Wv.Range("A" & Rows.Count).End(xlUp).Offset(1) 
     Ti(UBound(Ti)) = i 
     ReDim Preserve Ti(UBound(Ti) + i) 
     EndPaste = Wv.Range("A" & Rows.Count).End(xlUp).Offset(1).Row - 1 

     '2 options because i'm not sur where you want to add the text : 
     'First one (write on Vali, I think that's what you are looking to do) : 
     If Wv.Cells(EndPaste, "A").Value <> "" And Wv.Cells(EndPaste, "B").Value <> "" And Wv.Cells(EndPaste, "C").Value <> "" _ 
      And Wv.Cells(EndPaste, "D").Value <> "" And Wv.Cells(EndPaste, "E").Value = "" Then 
       Wv.Cells(Wv.Range("A" & Rows.Count).End(xlUp).Row, "E").Value = ValiFinish.TxTNaMe.Value 
     End If 
     'Second one (write on Sheet1) : 
     If ws.Cells(i, "A").Value <> "" And ws.Cells(i, "B").Value <> "" And ws.Cells(i, "C").Value <> "" _ 
      And ws.Cells(i, "D").Value <> "" And ws.Cells(i, "E").Value = "" Then 
       ws.Cells(ws.Range("A" & Rows.Count).End(xlUp).Row, "E").Value = ValiFinish.TxTNaMe.Value 
     End If 
     'end of options 
    End If 
Next i 

PastedRange = "" & Wv.Name & "!R" & StartPaste & "C1:R" & EndPaste & "C3" 
ActiveWorkbook.Names.Add Name:=ValiFinish.TxTNaMe.Value, RefersToR1C1:=PastedRange 




'clear content on previous sheet, from where we made copy 
For i = LBound(Ti) To UBound(Ti) - 1 
    ws.Range("$B$" & Ti(i) & ":$D$" & Ti(i)).ClearContents 
Next i 


    Wv.Range("E:E").EntireColumn.AutoFit 
    Set ws = Nothing 
    Set Wv = Nothing 

    ActiveWorkbook.Save 
    ValiFinish.Hide 
    Application.ScreenUpdating = True 

End Sub 
+0

非常感謝您的支持,也許我的問題是錯誤的, 第1步。 我有用戶表單,包括命令按鈕和輸入文本框。我有兩個工作表 - Sheet1和Vali 第3步。 4步驟。 我想給名稱複製Vali(Sheet)上的範圍。 所以,我已經複製範圍在哪裏是一些數據,我想給這個範圍的名稱和粘貼在Vali(表) 我試過這段代碼,但它是給我{運行時錯誤424} – 2015-04-03 11:46:50

+0

我編輯它,讓我知道!如果這是好的PLZ驗證答案關閉主題 – R3uK 2015-04-03 12:08:33

+0

它給了我{運行時錯誤424} – 2015-04-03 13:32:49

相關問題