這是我仍然是我的第一個宏,我一直在尋找像一個瘋狂的人試圖讓它工作...而且它越來越接近!更改現有宏以從特定列中複製公式
我已將它設置爲從Active工作簿中將「Pricing_Cost」工作表複製到一個新的工作簿中,然後對其進行操作。我真正需要的是修改該步驟,以便某些列複製值,其他人複製公式。我有A柱:X
列需要被粘貼爲值= A,E,F,H,I,J,K,L,M,N,T,U,V,W,X
列需要到粘貼爲式= B,C,d,G,O,P,Q,R,S
這是CopyRemoveFormSave子
我猜也許我應該複製整個事情內作爲公式,然後剪切並粘貼爲需要轉換爲值的列的值?不知道如何做到這一點與我在這裏的代碼...
Public strFile As String
Sub RunAll()
Call load_csv
Call CopyRemoveFormAndSave
Call Splitbook
End Sub
Sub load_csv()
Dim fStr As String
With Application.FileDialog(msoFileDialogFilePicker)
.Show
If .SelectedItems.Count = 0 Then
MsgBox "Cancel Selected"
Exit Sub
End If
'fStr is the file path and name of the file you selected.
fStr = .SelectedItems(1)
End With
Sheets("Product_Weekly").UsedRange.ClearContents
With ThisWorkbook.Sheets("Product_Weekly").QueryTables.Add(Connection:= _
"TEXT;" & fStr, Destination:=ThisWorkbook.Sheets("Product_Weekly").Range("$A$1"))
.Name = "CAPTURE"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = True
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Declare PtrSafe Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
'~~> Function to get user's temp directoy
Function TempPath() As String
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function
Sub CopyRemoveFormAndSave()
Dim wb As Workbook, wbNew As Workbook
Dim ws As Worksheet
Dim wsName As String, NewName As String
' Dim shp As Shape
Set wb = ThisWorkbook
wsName = ActiveSheet.Name
NewName = wsName & ".xlsm"
wb.SaveCopyAs TempPath & NewName
Set wbNew = Workbooks.Open(TempPath & NewName)
wbNew.Sheets(wsName).UsedRange.Value = wbNew.Sheets(wsName).UsedRange.Value
Application.DisplayAlerts = False
For Each ws In wbNew.Worksheets
If ws.Name <> wsName Then ws.Delete
Next ws
Application.DisplayAlerts = True
' For Each shp In wbNew.Sheets(wsName).Shapes
' If shp.Type = 8 Then shp.Delete
' Next
'
'~~> Do a save as for the new workbook if required.
'
'End Sub
Columns("W:W").Replace "2", "KevinClark", xlWhole
Columns("W:W").Replace "9", "PaulG", xlWhole
Columns("W:W").Replace "O", "KevinClark", xlWhole
Columns("W:W").Replace "I", "KevinClark", xlWhole
Columns("W:W").Replace "4", "PaulG", xlWhole
Columns("W:W").Replace "8", "KevinClark", xlWhole
Columns("W:W").Replace "7", "KevinClark", xlWhole
'Sub SplitData()
Const NameCol = "W"
Const HeaderRow = 3
Const FirstRow = 4
Dim SrcSheet As Worksheet
Dim TrgSheet As Worksheet
Dim SrcRow As Long
Dim LastRow As Long
Dim TrgRow As Long
Dim Buyer As String
Application.ScreenUpdating = False
Set SrcSheet = ActiveSheet
LastRow = SrcSheet.Cells(SrcSheet.Rows.Count, NameCol).End(xlUp).Row
For SrcRow = FirstRow To LastRow
Buyer = SrcSheet.Cells(SrcRow, NameCol).Value
Set TrgSheet = Nothing
On Error Resume Next
Set TrgSheet = Worksheets(Buyer)
On Error GoTo 0
If TrgSheet Is Nothing Then
Set TrgSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
TrgSheet.Name = Buyer
' SrcSheet.Range(HeaderRow).Copy Destination:=TrgSheet.Range(HeaderRow)
SrcSheet.Range("A1:W3").Copy Destination:=TrgSheet.Range("A1:W3")
End If
TrgRow = TrgSheet.Cells(TrgSheet.Rows.Count, NameCol).End(xlUp).Row + 1
SrcSheet.Rows(SrcRow).Copy Destination:=TrgSheet.Rows(TrgRow)
Next SrcRow
Application.ScreenUpdating = True
Dim sht As Worksheet
''AutoFit One Column
' ThisWorkbook.Worksheets("Sheet1").Columns("O:O").EntireColumn.AutoFit
'
''AutoFit Multiple Columns
' ThisWorkbook.Worksheets("Sheet1").Range("I:I,L:L").EntireColumn.AutoFit 'Columns I & L
' ThisWorkbook.Worksheets("Sheet1").Range("I:L").EntireColumn.AutoFit 'Columns I to L
'
''AutoFit All Columns on Worksheet
' ThisWorkbook.Worksheets("Sheet1").Cells.EntireColumn.AutoFit
'AutoFit Every Worksheet Column in a Workbook
For Each sht In wbNew.Worksheets
sht.Cells.EntireColumn.AutoFit
Next sht
End Sub
Sub Splitbook()
'Updateby20140612
Dim xPath As String
xPath = "C:\Users\Jimbo.JAMESP-ACERLT\Documents\For Gary\Output"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ActiveWorkbook.Sheets
If xWs.Name <> "Pricing Cost" Then
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
澄清,wbNew.Sheets(wsName).UsedRange.Value = wbNew.Sheets(wsName).UsedRange.Value是將所有單元格更改爲值的語句? –
這是正確的,所以在循環中可以使用wbNew.Sheets(wsName).Columns(ValArr(x))。Value = wbNew.Sheets(wsName).Columns(ValArr(x))。 。讓我知道事情的後續! –
比我想象的更復雜,我的原始工作表對該工作簿中的另一個工作表有依賴關係。我的代碼現在將原始圖紙值複製到新工作簿中。在將工作表複製到新工作簿之前,我需要將原始列更改爲值,以便我不會丟失數據並獲得#REF ?.我最喜歡的代碼是原始表格保持不變。 我在想,如果我將原始工作表複製爲臨時工作表,運行循環以將ValArr中的列轉換爲值,在新工作簿中保存並打開臨時工作表並從原始工作簿中刪除臨時工作簿並繼續執行宏。 –