私はVBAを初めて使用するので、質問が明確でない場合は申し訳ありません。
以下にいくつかのループを含むコードが含まれており、165 行目の "Next counter" で "next without For" というコンパイル エラーが発生します。
ただし、121行目に「For counter =」があります。そのため、無知で困惑しています。
このコードがあまりにもアマチュアである場合は申し訳ありませんが、私ができる最善のことです。
Sub Macro9()
Dim cardtype, drcr, sheetname, wbkname, merchant As String
Dim counter, prd As Integer
Dim row1, row2, row3, row4, row5 As Integer
Dim col1, col2, col3, col4, col5 As Integer
Dim val1, val2, val3, val4, NFile As Long
Dim rng As Range
Dim wks As Worksheet
Dim WorkBk As Workbook
Dim FolderPath, FileName As String
Dim SelectedFiles() As Variant
prd = InputBox("Input the Period number")
FolderPath = "C:\My Files\Reports\Statements\"
ChDrive FolderPath
ChDir FolderPath
SelectedFiles = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
FileName = SelectedFiles(NFile)
Set WorkBk = Workbooks.Open(FileName)
sheetname = ActiveSheet.name
wbkname = ActiveWorkbook.name
merchant = Left(wbkname, 7)
Cells.Select
Selection.Find(What:="Trans Value", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
col1 = ActiveCell.Column
Cells.Select
Selection.Find(What:="Quantity", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
col2 = ActiveCell.Column
Cells.Select
Selection.Find(What:="charge Desc", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
col3 = ActiveCell.Column
ActiveCell.Offset(1, 0).Range("A1:A200").Select
Selection.SpecialCells(xlCellTypeConstants, 2).Select
row1 = ActiveCell.row
Cells.Select
Selection.Find(What:="Cr/Dr Trans Flag", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
col4 = ActiveCell.Column
Cells.Select
Selection.Find(What:="Balance from last month", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
row2 = ActiveCell.row - 1
Cells.Select
Selection.Find(What:="charge ($)", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
col4 = ActiveCell.Column
ActiveCell.Offset(1, 0).Range("A1:A200").Select
Selection.SpecialCells(xlCellTypeConstants, 1).Select
row4 = ActiveCell.row
Cells.Select
Selection.Find(What:="Store1 Credit", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.FormulaR1C1 = "Web1 Credit"
Cells.Select
Selection.Find(What:="Store2 Credit", After:=ActiveCell, LookIn:=xlFormulas _
, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Select
ActiveCell.FormulaR1C1 = "Web2 Credit"
For Each wks In Worksheets
Set rng = wks.Cells.Find("Store Refund")
If Not rng Is Nothing Then
rng = "Web Refund"
Exit Sub
End If
Next wks
For counter = row1 To row2
Rows(counter).Columns(col3).Select
cardtype = ActiveCell.Value
row3 = ActiveCell.row
drcr = Rows(counter).Columns(col4).Value
val1 = Rows(counter).Columns(col2).Value
If drcr = CR Then
val2 = -Rows(counter).Columns(col1).Value
Else
val2 = Rows(counter).Columns(col1).Value
End If
Windows("Stores 2013.xlsm").Activate
ActiveWorkbook.sheets(prd).Select
Columns("B:B").Select
Selection.Find(What:=cardtype, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
If merchant = 1234567 Then
ActiveCell.Offset(0, 4).Select
val3 = ActiveCell.Value
Else
If merchant = 7654321 Then
ActiveCell.Offset(0, 8).Select
val3 = ActiveCell.Value
Else
If merchant = 1122334 Then
ActiveCell.Offset(0, 12).Select
val3 = ActiveCell.Value
End If
ActiveCell.FormulaR1C1 = val1 + val3
ActiveCell.Offset(0, 1).Select
val4 = ActiveCell.Value
ActiveCell.FormulaR1C1 = val2 + val4
Windows(wbkname).Activate
Next counter
Windows("Stores 2013.xlsm").Activate
ActiveWorkbook.sheets(prd).Select
Columns("B:B").Select
Selection.Find(What:="Total $", After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
If merchant = 1234567 Then
ActiveCell.Offset(0, 6).Select
Else
If merchant = 7654321 Then
ActiveCell.Offset(0, 10).Select
Else
If merchant = 1122334 Then
ActiveCell.Offset(0, 14).Select
End If
ActiveCell.FormulaR1C1 = "='[wbkname]" & sheetname & "'!R" & row4 & "C" & col4
WorkBk.Close savechanges:=False
Next NFile
End Sub
前もって感謝します