Настройка шаблона для различных импортов CSV (Excel VBA) - PullRequest
0 голосов
/ 02 мая 2019

Я работаю с довольно большим файлом Excel, который ежедневно импортирует несколько файлов CSV.Процесс для каждого довольно похож, но есть небольшие различия.Очевидно, что имена файлов и соответствующих листов различаются, и некоторые из них должны включать формулы.На данный момент у меня есть отдельные сценарии для каждого импорта, но я уверен, что есть более эффективный подход.

Если у кого-то есть идеи о том, как потенциально использовать шаблонный скрипт с входными данными или что-то в этом роде, я был бы признателен.Пример ниже.

Sub MyVBAScript()

Application.ScreenUpdating = False

Dim wbCopy As Workbook
Dim wsCopy As Worksheet, wsPaste As Worksheet
Dim rgCopy As Range, rgPaste, StartCell As Range
Dim statsCopy, yearCopy, fileCopy As String
Dim lRowPaste As Long

'*****************************************************************************************************************************'
'Name of the csv file to be imported (also used to identify corresponding paste sheet & named ranges)
statsCopy = "Pitching"

'*****************************************************************************************************************************'
'The year of the data to be imported and the complete csv string
yearCopy = [StatsImportYear]
fileCopy = "C:\Users\Nick\baseball\" & statsCopy & yearCopy & ".csv"

'*****************************************************************************************************************************'
Set wbCopy = Workbooks.Open(fileCopy)
Set wsCopy = wbCopy.Sheets(1)
Set rgCopy = wsCopy.Range("A1").CurrentRegion
Set rgCopy = rgCopy.Resize(rgCopy.Rows.Count).Offset(0, 1)

'*****************************************************************************************************************************'
'Insert any wsCopy formulas below
'-wsCopy formula1-
'-wsCopy formula2-
'-wsCopy formula3-

'*****************************************************************************************************************************'
Set wsPaste = Workbooks("Baseball2019").Sheets(statsCopy & yearCopy)
Set rgPaste = wsPaste.Range("A1")
Set rgPaste = rgPaste.Resize(rgCopy.Rows.Count, rgCopy.Columns.Count - 1)

rgPaste.Value2 = rgCopy.Value2

wbCopy.Close savechanges:=False

'*****************************************************************************************************************************'
'Insert any wsPaste formulas below
'-wsPaste formula1-
'-wsPaste formula2-
'-wsPaste formula3-

'*****************************************************************************************************************************'
'Cell to start wsPaste named range
Set StartCell = wsPaste.Range("A1")

'Find last row/column of wsPaste
LastRow = wsPaste.Cells(wsPaste.Rows.Count, StartCell.Column).End(xlUp).Row
LastCol = wsPaste.Cells(StartCell.Row, wsPaste.Columns.Count).End(xlToLeft).Column

'Name the wsPaste ranges
wsPaste.Range(StartCell, wsPaste.Cells(LastRow, LastCol)).Name = statsCopy & yearCopy
wsPaste.Range(StartCell, wsPaste.Cells(LastRow, StartCell.Column)).Name = statsCopy & yearCopy & "row"
wsPaste.Range(StartCell, wsPaste.Cells(StartCell.Row, LastCol)).Name = statsCopy & yearCopy & "col"

Application.ScreenUpdating = True

End Sub
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...