2016-11-23 46 views
1

我試圖拿出一個粗略的數據列表,然後將其複製到預先格式化的組織形式。爲此,我設置了粗略列表,以便列表中的每個項目都按順序編號,而不管項目之間是否有空格。我試圖製作的宏將採用粗略列表並將其複製到沒有任何空格的表單中。忍受我,我一直在試圖自學Visual Basic,所以我有的代碼可能......凌亂。當前,我遇到的問題是,我得到我的溢出= I + 1檢查值是否存在,然後複製到另一個工作表

Sub Print_Sheet_Populate() 
' 
' Print_Sheet_Populate Macro 
' Takes Items from Raw Data sheet and puts them in Print Sheet sheet. 
' 

' 
Dim wsS1 As Worksheet 
Dim wsS2 As Worksheet 
Dim ending As Long 
Dim copy() As Long 
Dim i As Long 

Set wsS1 = Sheets("Raw Data") 
Set wsS2 = Sheets("Print Sheet") 

With wsS1.Range("A:A") 'To copy the item numbers in the rough data to an array 
    i = 1 
     Set c = .Find(i, LookIn:=xlValues) 
     If Not c Is Nothing Then 
      ReDim copy(i) 
      copy(i - 1) = c.Value 
      Do 
       i = i + 1 
       ending = i 
      Loop While Not c Is Nothing 
     End If 
End With 

With wsS2.Range("A24:A324") 'To paste the data from the array to the form 
    i = 1 
     If Not i = ending Then 
      Do 
       Worksheets("wsS2").Range("A" & i).Value = copy(i - 1) 
       i = i + 1 
      Loop While Not c Is Nothing 
     End If 
End With 
End Sub 

回答

0

Range.Find Method (Excel)摘自:

當搜索到達指定搜索範圍的結束,它 環繞到範圍的開始。當此環繞發生時停止搜索,保存找到的第一個單元的地址,然後 對照此保存的地址測試每個連續的發現單元地址。

+0

這樣做,謝謝EEM! –

+0

很高興幫助你!你是否嘗試用一種數據替換第一個循環,並用類似'Range.resize(ubound(array),ubound(array,2))的值替換第二個循環。value = Array'來發布整個陣列一次? – EEM

相關問題