2017-07-17 25 views
0
Sub CostPriceMain() 

Dim SourceWkb As Workbook 
Dim TargetWkb As Workbook 
Dim SourceWksht As Worksheet 

Application.ScreenUpdating = False 
Application.DisplayAlerts = False 

NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel Files   
(*.xlsx; *.xls), (*.xlsx; *.xls), All Files, *.*", FilterIndex:=1) 

If NewFile = False Then Exit Sub 
If NewFile <> False Then 
Set SourceWkb = Workbooks.Open(NewFile) 
End If 

Set TargetWkb = Workbooks.Open("C:\WK24.xlsx") ' warning - XLS file could cause problems - see note 

For Each SourceWksht In SourceWkb.Worksheets 
If SourceWksht.Visible Then 
    SourceWkb.Sheets("Price List").Range("C:E").Copy 
    TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues 
End If 

Next SourceWksht 

TargetWkb.Close False 
SourceWkb.Close False 

Application.ScreenUpdating = True 
Application.DisplayAlerts = True 

Done = MsgBox("Task Complete", vbOKOnly) 

End Sub 

我的主要問題複製數據似乎與表不超過

 SourceWkb.Sheets("Price List").Range("C:E").Copy 
     TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues 

它運行沒有問題,但它實際上不是在複製數據,我似乎無法找出原因 我試圖

TargetWkb.Sheets("Price List").Range("A:A").Value = SourceWkb.Sheets("Price 
    List").Range("A:A") 

位仍然沒有得到的數據,任何想法的同樣的結果?

+1

你是積極的表名是正確的,範圍?它是不是複製數據*或*是聲明的一部分未被解僱?用'F8'來查看宏是否運行該行。我*想*問題可能在於邏輯。您正在循環查看源代碼中的工作表,但不會對可見表單執行任何操作。 – BruceWayne

+0

爲什麼循環瀏覽工作表(例如'For SourceWksht In SourceWkb.Worksheets',如果您要複製特定的工作表數據? – Jeeped

回答

1

你的代碼有一些奇怪的地方。最值得注意的是:

'Here you loop through every worksheet in your source workbook 
    'but you only copy one specific sheet. This is superfluous and 
    'may be causing the issue (although it shouldn't) 
    For Each SourceWksht In SourceWkb.Worksheets 
     If SourceWksht.Visible Then 
      SourceWkb.Sheets("Price List").Range("C:E").Copy 
      TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues 
     End If 
    Next SourceWksht 

給這個快速重寫一個鏡頭,看看問題是否清除。我已經添加了評論,以說明每個代碼塊在做什麼以防止任何誤解。

Sub CostPriceMain() 

    Dim SourceWkb As Workbook 
    Dim TargetWkb As Workbook  

    'shhh 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    'ask user for excel file to source from 
    NewFile = Application.GetOpenFilename(FileFilter:="Microsoft Excel File (*.xlsx; *.xls), (*.xlsx; *.xls), All Files, *.*", FilterIndex:=1) 

    'Did they pick a file? 
    If Not NewFile = False Then 
     Set SourceWkb = Workbooks.Open(NewFile) 
    Else 
     Exit Sub 
    End If 

    'Set up the target workbook 
    Set TargetWkb = Workbooks.Open("C:\WK24.xlsx") ' warning - XLS file could cause problems - see note 

    'Copy the price list from source workbook on the tab called "Price List" 
    'For columns C through E. Copying it to the Target Workbook to the tab 
    'called "Price List" using the same columns, only copying the values. 
    SourceWkb.Sheets("Price List").Range("C:E").Copy 
    TargetWkb.Sheets("Price List").Range("C:E").PasteSpecial Paste:=xlValues 

    'Clean up 
    TargetWkb.Close False 
    SourceWkb.Close False 

    Application.ScreenUpdating = True 
    Application.DisplayAlerts = True 

    'Notify user 
    Done = MsgBox("Task Complete", vbOKOnly) 

End Sub 
+0

這給我一個關於NewFile = Application.GetOpenFilename(FileFilter:=「Microsoft Excel Files –

+0

@JamesPavett對不起,從你的原始代碼複製/粘貼問題,我修復了它 – JNevill

+0

現在它似乎是一種類型不匹配,如果NewFile然後 –