2016-02-11 48 views
0

我在這裏幾個月一直是一個沉默的讀者,但一直在努力與此代碼一個星期了,所以想我會看看是否有人可以幫助。動態表命名和複製數據

我有一個工作表,其中工作表1包含用戶輸入數據的信息。 列A提出問題,C列是用戶輸入答案的地方。 第4行詢問將會有多少配置。取決於他們輸入的數字取決於有多少單元點亮到右側,即如果1,則D4變黃,如果2,則D4和E4變黃(使用條件格式) 然後用戶將標題輸入到突出顯示的單元格中D4,E4,F4等) 我想爲每個配置在工作表的末尾創建一個新工作表。 然後命名由文本的新工作表中輸入D4,E4等

我到目前爲止的代碼是:

Option Explicit 

Sub InsertSupplierSheet() 

Dim ws As Worksheet 
Dim tmpSht As Worksheet 
Dim Lastcol As Integer, i As Integer, j As Integer 
Dim DESCRANGE As Range 

'~~> Change Sheet1 to the sheet which has all the data 
Set ws = ThisWorkbook.Worksheets(1) 

With ws 
    Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column 

    If Lastcol < 4 Then Exit Sub 

    For i = 3 To Lastcol 
     If DoesSheetExist(ActiveSheet.Cells(4 & i).Value) Then 
      Set tmpSht = ActiveSheet.Cells(4 & i).Value 
     Else 
      Sheets.Add After:=Sheets(Sheets.Count) 
      Set tmpSht = ActiveSheet 
      tmpSht.Name = "NEWSHEET" 


     End If 

     .Rows("1:3").Copy tmpSht.Rows(1) 

     For j = 1 To 4 
      tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth 
     Next j 

     .Rows(i).Copy tmpSht.Rows(4) 
    Next 
End With 
End Sub 

Function DoesSheetExist(Sht As String) As Boolean 
Dim ws As Worksheet 

On Error Resume Next 
Set ws = Sheets(ws) 
On Error GoTo 0 

If Not ws Is Nothing Then DoesSheetExist = True 
End Function 

我把「NEWSHEET」,看看,甚至創建了一個新的工作表,但它仍然失敗。我只是不知道我要去哪裏錯了。

歡迎任何幫助/建議。

編輯。

我無法解決,爲什麼。 最後一個col將是H4,因此lastcol將是「8」。 然後爲i = 4到8運行循環。在第4行的每個單元格中都有描述,所以我不明白它爲什麼會在2瞬間工作然後失敗?

我不知道這是否會使它更容易,但我有我希望在單元格C4中創建的工作表的數量,所以我可以使用它而不是查找填充的單元格。所以如果C4是2,那麼我想添加2張名爲D4,E4的內容。如果C4是3,那麼我想添加3張名稱作爲D3,E3,F3的內容。我是否比我需要的更難?

UPDATE 我想通過複製信息影響這個循環。並將代碼修改爲此。

Sub InsertSupplierSheet() 

Dim ws As Worksheet 
Dim tmpSht As Worksheet 
Dim Lastcol As Integer, i As Integer, j As Integer 
Dim DESCRANGE As Range 
Dim sShtName As String 'Dimension sheet name variable 

'~~> Change Sheet1 to the sheet which has all the data 
Set ws = ThisWorkbook.Worksheets(1) 

With ws 
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column 

If Lastcol < 4 Then Exit Sub 

For i = 4 To Lastcol 
    sShtName = ActiveSheet.Cells(4, i).Value2 'Set sShtName to cell value  within loop 
If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function 
    Set tmpSht = Sheets(sShtName) 
Else 
    Sheets.Add After:=Sheets(Sheets.Count) 
    Set tmpSht = ActiveSheet 
    tmpSht.Name = sShtName 'Change name to sShtName 
End If 

    .Rows("1:3").Copy tmpSht.Rows(1) 


    .Rows(13).Copy tmpSht.Rows(4) 
Next 
End With 
End Sub 

Function DoesSheetExist(Sht As String) As Boolean 
Dim ws As Worksheet 

On Error Resume Next 
Set ws = Sheets(Sht) 
On Error GoTo 0 

If Not ws Is Nothing Then DoesSheetExist = True 
End Function 

這是做什麼我想要它做一些小例外。 表單由D1中的單元格命名,然後是E13,F13,G13,H13所以我需要弄清楚信息來自哪裏。 最後一點是由於我在第一張工作表中的條件格式化,我在複製單元中的黑色背景上獲得文本,但那是我最擔心的問題! UPDATE 發現錯誤 -

sShtName = ActiveSheet.Cells(4, i).Value2 

應該

sShtName = Worksheets(1).Cells(4, i).Value2 

回答

0

您不正確調用你的細胞。使用(4, i)而不是(4 & i)

您調用它的方式將其連接到43,從而導致您檢查工作表參考的單元格AQ1(AQ是第43列)。

編輯:我只是經歷了一下,發現了一些其他的錯誤。您需要在「存在」功能中將工作表名稱設置爲sht,並且我假設您要將tmpSht設置爲工作表,因此您需要將其包含在sheets()中。試試這個:

Sub InsertSupplierSheet() 

Dim ws As Worksheet 
Dim tmpSht As Worksheet 
Dim Lastcol As Integer, i As Integer, j As Integer 
Dim DESCRANGE As Range 
Dim sShtName As String 'Dimension sheet name variable 

'~~> Change Sheet1 to the sheet which has all the data 
Set ws = ThisWorkbook.Worksheets(1) 

With ws 
Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column 

If Lastcol < 4 Then Exit Sub 

For i = 4 To Lastcol 
    sShtName = ActiveSheet.Cells(4, i).Value2 'Set sShtName to cell value within loop 
    If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function 
     Set tmpSht = Sheets(sShtName) 
    Else 
     Sheets.Add After:=Sheets(Sheets.Count) 
     Set tmpSht = ActiveSheet 
     tmpSht.Name = sShtName 'Change name to sShtName 
    End If 

    .Rows("1:3").Copy tmpSht.Rows(1) 

    For j = 1 To 4 
     tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth 
    Next j 

    .Rows(i).Copy tmpSht.Rows(4) 
Next 
End With 
End Sub 

Function DoesSheetExist(Sht As String) As Boolean 
Dim ws As Worksheet 

On Error Resume Next 
Set ws = Sheets(Sht) 
On Error GoTo 0 

If Not ws Is Nothing Then DoesSheetExist = True 
End Function 
+0

如果我的回答對您有所幫助,請把它標記爲接受的答案。如果沒有,讓我知道我可以幫助更多。 – JMcD

+0

HI JMcD這是一個偉大的現在我需要解決如何使用單元格D4中的信息重命名工作表,以及用E4中的信息重新編寫信息等。 –

+0

這可能會有所幫助你將該名稱設置爲一個變量,因爲你將在多個地方使用它。我將更新上面的代碼示例以顯示我的意思。 – JMcD

0

,而不是添加新的工作表,然後activesheet設置,你可以使用更短的方式(見下文)tmpsht的。你爲什麼設置ws,如果你不使用它......

Sub InsertSupplierSheet() 

    Dim ws As Worksheet 
    Dim tmpSht As Worksheet 
    Dim Lastcol As Integer, i As Integer, j As Integer 
    Dim DESCRANGE As Range 

    Set ws = ThisWorkbook.Worksheets(1) 

    With ws 
     Lastcol = .Cells(4, .Columns.Count).End(xlToLeft).Column 

     If (Lastcol < 4) Then 
      Exit Sub 
     End If 

     For i = 4 To Lastcol 
      If (DoesSheetExist(.Cells(4, i).Value2) = True) Then 
       Set tmpSht = Sheets(.Cells(4, i).Value) 
      Else 
       Set tmpSht = Sheets.Add After:=Sheets(Sheets.Count) 
       tmpSht.Name = "NEWSHEET" 
      End If 

      .Rows("1:3").Copy tmpSht.Rows(1) 

      For j = 1 To 4 
       tmpSht.Columns(j).ColumnWidth = .Columns(j).ColumnWidth 
      Next j 

      .Rows(i).Copy tmpSht.Rows(4) 
     Next i 
    End With 
End Sub 


Function DoesSheetExist(Sht As String) As Boolean 

    Dim ws As Worksheet 

    On Error Resume Next 
    Set ws = Sheets(Sht) 
    On Error GoTo 0 

    If Not ws Is Nothing Then 
     DoesSheetExist = True 
    Else 
     DoesSheetExist = False 
    End If 

End Function 
+0

謝謝你的halp,但我不能得到這個工作,我有一個syntex錯誤就行了。 Set tmpSht = Sheets.Add After:= Sheets(Sheets.Count) –

+0

好吧,那麼我猜這是行不通的。你可以在你的代碼中使用那個「sheets.add」後面:= Sheets(sheets.count)「」然後「set tmpSht = ActiveSheet」「tmpSht.Name =(.Cells(4,i).Value)」 – Kathara

0

這是我的最終代碼。有一些調整,首先,我在第6行添加了一個公式,將第4行的名稱縮短爲10個字符的名稱,因爲我發現標籤名稱太長(因此命名的代碼引用了第6行。我還添加了一些自定義的文本添加到每個表和一些格式

Option Explicit 


Sub InsertSupplierSheet() 

Dim ws As Worksheet 
Dim tmpSht As Worksheet 
Dim Lastcol As Integer 
Dim i As Integer 
Dim j As Integer 
Dim DESCRANGE As Range 
Dim sShtName As String 'Dimension sheet name variable 

'~~> Change Sheet1 to the sheet which has all the data 
Set ws = ThisWorkbook.Worksheets(1) 

With ws 

Lastcol = ActiveSheet.Cells(4, Columns.Count).End(xlToLeft).Column ' work   with the template sheet 

If Lastcol = 3 Then Exit Sub 'repeat these steps from the first config to the last 

For i = 4 To Lastcol 

sShtName = Worksheets(1).Cells(6, i).Value2 'Set sShtName to cell value within loop 
If DoesSheetExist(sShtName) Then 'Use sShtName to call "Exists" function 
    Set tmpSht = Sheets(sShtName) 
Else 
    Sheets.Add After:=Sheets(Sheets.Count) 
    Set tmpSht = ActiveSheet 
    tmpSht.Name = sShtName 'Change name to sShtName tmpSht.Name = sShtName 
End If 

.Rows("1:3").Copy tmpSht.Rows(1) ' Format the cell width in the new sheet 
.Rows(13).Copy tmpSht.Rows(4) 
tmpSht.Range("A1").Value = Worksheets(1).Cells(4, i).Value2 
Range("A1").ColumnWidth = 30 
Range("B1").ColumnWidth = 0 
Range("C1").ColumnWidth = 30 
Range("D1:K1").ColumnWidth = 10 
Range("D4:J4").Font.Color = vbWhite ' format the colour of the text in the new sheet 
Range("C1") = " " ' Negate info in cell C1 

With Range("A1:M5") ' add borders 

'Clear existing 
.Borders.LineStyle = xlNone 

'Apply new borders 
.BorderAround xlContinuous 
.Borders(xlInsideHorizontal).LineStyle = xlContinuous 
.Borders(xlInsideVertical).LineStyle = xlContinuous 

End With 
With Range("A1:C4") ' set colours for the new sheet 
    .Font.Color = vbBlack 
    .Interior.Color = vbWhite 

End With 
Range("D4:J4").Font.Color = vbWhite ' set colour of the numbers to white to show on black background 
Range("A5") = "Unit cost in " & Worksheets(1).Cells(17, 3).Value2 
Range("A6") = "CUSTOM TEXT ONE." 
Range("A7") = "NOTE if quantity " & Range("D4").Value2 + 5 & " is ordered then total cost will be your unit cost for " & Range("D4").Value2 & " multiplied by " & Range("D4").Value2 + 5 & " .This applies up to the quantity of " & Range("E4").Value2 - 1 
Range("A8") = "CUSTOM TEXT 2" 


Next i 
End With 
End Sub 

Function DoesSheetExist(Sht As String) As Boolean 
Dim ws As Worksheet 

On Error Resume Next 
Set ws = Sheets(Sht) 
On Error GoTo 0 

If Not ws Is Nothing Then DoesSheetExist = True 
End Function